Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reduce the number of target contour points predicted by YOLOv8-Sseg #13802

Closed
1 task done
jiangxiaobaichunniang opened this issue Jun 19, 2024 · 3 comments
Closed
1 task done
Labels
question Further information is requested

Comments

@jiangxiaobaichunniang
Copy link

Search before asking

Question

We saved the predicted results of yolov8-seg as a .txt file, but we found that the contour points of each target were too dense. We want to reduce the number of contour points for each target in the .txt file.
1718803895245

Additional

No response

@jiangxiaobaichunniang jiangxiaobaichunniang added the question Further information is requested label Jun 19, 2024
@glenn-jocher
Copy link
Member

@jiangxiaobaichunniang hello,

Thank you for reaching out with your question! To reduce the number of contour points predicted by YOLOv8-Seg, you can post-process the segmentation results to simplify the contours. This can be achieved using techniques such as contour approximation or polygon simplification.

Here's a concise example using OpenCV's approxPolyDP function, which approximates a contour shape to another shape with fewer vertices:

import cv2
import numpy as np

# Function to simplify contours
def simplify_contours(contours, epsilon=0.01):
    simplified_contours = []
    for contour in contours:
        # Approximate contour
        approx = cv2.approxPolyDP(contour, epsilon * cv2.arcLength(contour, True), True)
        simplified_contours.append(approx)
    return simplified_contours

# Example usage
# Assuming `contours` is a list of contours obtained from YOLOv8-Seg predictions
# contours = [np.array([...]), np.array([...]), ...]

simplified_contours = simplify_contours(contours)

# Save simplified contours to .txt file
with open('simplified_contours.txt', 'w') as f:
    for contour in simplified_contours:
        for point in contour:
            f.write(f"{point[0][0]},{point[0][1]} ")
        f.write("\n")

In this example, epsilon is a parameter that controls the approximation accuracy. A smaller value of epsilon results in a contour closer to the original, while a larger value simplifies the contour more aggressively.

Please ensure you are using the latest versions of torch and ultralytics to avoid any compatibility issues. If you encounter any further problems, providing a minimum reproducible code example would help us investigate the issue more effectively. You can find guidelines for creating one here.

Feel free to reach out if you have any more questions. Happy coding! 😊

@jiangxiaobaichunniang
Copy link
Author

Thank you for your valuable help!

@glenn-jocher
Copy link
Member

Hello @jiangxiaobaichunniang,

You're welcome! To reduce the number of contour points predicted by YOLOv8-Seg, you can use post-processing techniques like contour approximation. Here's a quick example using OpenCV's approxPolyDP function:

import cv2
import numpy as np

# Function to simplify contours
def simplify_contours(contours, epsilon=0.01):
    simplified_contours = []
    for contour in contours:
        # Approximate contour
        approx = cv2.approxPolyDP(contour, epsilon * cv2.arcLength(contour, True), True)
        simplified_contours.append(approx)
    return simplified_contours

# Example usage
# Assuming `contours` is a list of contours obtained from YOLOv8-Seg predictions
# contours = [np.array([...]), np.array([...]), ...]

simplified_contours = simplify_contours(contours)

# Save simplified contours to .txt file
with open('simplified_contours.txt', 'w') as f:
    for contour in simplified_contours:
        for point in contour:
            f.write(f"{point[0][0]},{point[0][1]} ")
        f.write("\n")

This approach uses the epsilon parameter to control the approximation accuracy. Adjust epsilon to find the balance between simplification and accuracy that suits your needs.

If you encounter any issues, please ensure you're using the latest versions of torch and ultralytics. If the problem persists, providing a minimum reproducible code example would help us investigate further. You can find guidelines for creating one here.

Feel free to reach out if you have any more questions. Happy coding! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants