ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.2k stars 16.2k forks source link

Area extraction from masks output of instance segmentation model #11331

Closed vimala-ds closed 1 year ago

vimala-ds commented 1 year ago

Hi, I want to get area of the mask obtained from the YOLOV5 instance segmentation model. Tried with cv2.contourArea, but end up with the below issue: File "segment/predict.py", line 204, in run area = cv2.contourArea(masks.numpy()) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'

Can anyone please let me know, where I'm going wrong.

github-actions[bot] commented 1 year ago

👋 Hello @vimala-ds, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 year ago

@vimala-ds the error message suggests that there is a problem with the input arguments to the cv2.contourArea() function. The masks.numpy() function likely returns a numpy array of masks, but cv2.contourArea() expects a single mask as input, so you may need to loop through the masks and calculate the area of each one individually.

Here is one example of how you could modify your code to calculate the area of each mask:

masks_np = masks.numpy()
areas = []
for mask in masks_np:
    contours, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    area = cv2.contourArea(contours[0])
    areas.append(area)

This code loops through each mask in the array masks_np and calculates the area using cv2.findContours() to extract the contours of the mask, and then cv2.contourArea() to calculate the area of the contours. It stores the area of each mask in a list called areas.

I hope this helps! Let me know if you have any further questions.

vimala-ds commented 1 year ago

@vimala-ds the error message suggests that there is a problem with the input arguments to the cv2.contourArea() function. The masks.numpy() function likely returns a numpy array of masks, but cv2.contourArea() expects a single mask as input, so you may need to loop through the masks and calculate the area of each one individually.

Here is one example of how you could modify your code to calculate the area of each mask:

masks_np = masks.numpy()
areas = []
for mask in masks_np:
    contours, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    area = cv2.contourArea(contours[0])
    areas.append(area)

This code loops through each mask in the array masks_np and calculates the area using cv2.findContours() to extract the contours of the mask, and then cv2.contourArea() to calculate the area of the contours. It stores the area of each mask in a list called areas.

I hope this helps! Let me know if you have any further questions.

It worked. Thanks a lot Glenn!!

glenn-jocher commented 8 months ago

You're welcome, @vimala-ds! I'm glad to hear that the solution worked for you. If you have any more questions or need further assistance as you continue working with YOLOv5, don't hesitate to reach out. Happy coding! 😊👍

kmanjari commented 8 months ago

@vimala-ds the error message suggests that there is a problem with the input arguments to the cv2.contourArea() function. The masks.numpy() function likely returns a numpy array of masks, but cv2.contourArea() expects a single mask as input, so you may need to loop through the masks and calculate the area of each one individually. Here is one example of how you could modify your code to calculate the area of each mask:

masks_np = masks.numpy()
areas = []
for mask in masks_np:
    contours, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    area = cv2.contourArea(contours[0])
    areas.append(area)

This code loops through each mask in the array masks_np and calculates the area using cv2.findContours() to extract the contours of the mask, and then cv2.contourArea() to calculate the area of the contours. It stores the area of each mask in a list called areas. I hope this helps! Let me know if you have any further questions.

It worked. Thanks a lot Glenn!!

Hello @glenn-jocher I used your example code to calculate the area of segmented mask but getting this error

Screenshot 2024-02-02 at 4 57 13 PM

Basically, I want to calculate the area of black and white pixels inside the segmented area and knoww the difference

glenn-jocher commented 8 months ago

@kmanjari i'm glad to hear you've made progress! The error in the screenshot you're referring to seems to be missing, but if you're looking to calculate the area of black and white pixels within a segmented mask, you can do so by counting the number of pixels that correspond to each category.

Here's a simple way to calculate the areas:

import numpy as np

# Assuming 'mask' is a binary mask with white pixels for the object (value 255) and black pixels for the background (value 0)
white_area = np.sum(mask == 255)
black_area = np.sum(mask == 0)

# If you need the area in terms of the number of pixels
print(f"White area (in pixels): {white_area}")
print(f"Black area (in pixels): {black_area}")

# If you need to calculate the difference
area_difference = abs(white_area - black_area)
print(f"Area difference (in pixels): {area_difference}")

Make sure that your mask is a binary image where the object is represented by white pixels (255) and the background is black (0). If your mask uses different values, you'll need to adjust the comparison in np.sum() accordingly.

If you encounter any specific errors, please provide the error message text, and I'll be happy to help you troubleshoot further.