# Find the number of white pixels in contour

I have an image and I'm trying to find the filled circles inside image (MCQ's). I have found all contours and when drawing its drawing the circles but now I don't know how to find the number of white pixels inside contour. Image is in binary pealse suggest something. Thanks

java.util.List<MatOfPoint> contours_crop = new ArrayList<MatOfPoint>();
Imgproc.findContours(crop.clone(), contours_crop, new Mat() ,Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);

//Imgproc.cvtColor(crop, crop, Imgproc.COLOR_GRAY2BGR);
double noPixels = Core.countNonZero(contours_crop.get(3));
Toast.makeText(MainActivity.this, "Pixels:"+noPixels, 10000).show();

edit retag close merge delete

1

nope, you can't use countNonZero with contours, only with images.

so what @berak is trying to say

1. Based on your contour image, black out all pixels outside the countour.
2. Then apply a countNonZero on the given image.
3

i added the image from the crossposted SO question for clarity. again, it seems to be about finding the filled circles. so my proposal would be either:

• get the boundingRect()
• countNonZero() on the roi of that

or:

• find the center of the contour (either from boundingRect() or from moments())
• check pixel value at that point

(this will only work on convex objects, like the circles, but probably fail on e.g. the '1' there)

1

Getting the boundingrect() and then counting non zero helped :)

Hello Zain can you share the solution here also so that even I can get some help out of it Thanks

Sort by » oldest newest most voted

You can use pointPolygonTest() (java version) to determine which image pixels are inside a contour. Then you can count how many inner pixels are white.

To speed up the process you dont need to test all image pixels, only those inside the contour's bounding box.

more

If contourArea doesn't give you expected result, you can follow this approach:

1. Copy the contour in a new empty Mat
2. Apply countNonZero applied on the new submatrix and you will get the number of non-black pixel

    //1. Copy the contour in a new empty Mat
Rect cRect = 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.

more

Don't you have a method to retrieve size of the array list in Java? Here is how its done in C++:

    std::vector<cv::Vec4i> hierarchy;
std::vector<std::vector<cv::Point> > contours;
cv::Mat frame = something;
cv::findContours(frame, contoursInt, hierarchyObj, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

for (unsigned int n = 0; n < contours.size(); n++)
{
int numOfPixels = countoursInt[n].size();
}


numOfPixels has the number of pixels of each contour. Just do the equivalent in Java.

more

Well the problem here is that you will just get the number of pixels, while he wants to see whether the pixel value is white or not to see if a dot is filled.

1

cv::contourArea gives you ..... the area of a given contour

more

1

The area is calculated in pixels, so this would give you the number of pixels. But as far as I know, the cv::contourArea is just an approximation.

Official site

GitHub

Wiki

Documentation