1 | initial version |
If contourArea doesn't give you expected result, you can follow this approach:
Then countNonZero applied on the new submatrix will give you the number of non-black pixel
//1. Copy the contour in a new empty Mat
Rect cRect = boundingRect(*it);
Mat subImg = dilatedImg(cRect);
double cArea = countNonZero(subImg);
Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.
2 | No.2 Revision |
If contourArea doesn't give you expected result, you can follow this approach:
Then countNonZero applied on the new submatrix will give you the number of non-black pixel
//1. Copy the contour in a new empty Mat
Rect cRect = boundingRect(*it);
Mat subImg = dilatedImg(cRect);
double cArea = countNonZero(subImg);
Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.
3 | No.3 Revision |
If contourArea doesn't give you expected result, you can follow this approach:
Then Apply countNonZero applied on the new submatrix and you will give you get the number of non-black pixel
//1. Copy the contour in a new empty Mat
Rect cRect = boundingRect(*it);
boundingRect(*it); //it is an iterator for your contours vector
Mat subImg = dilatedImg(cRect);
double cArea = countNonZero(subImg);
Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.