Ask Your Question
1

Find the number of white pixels in contour

asked 2015-04-18 06:16:21 -0600

Zain ul abdeen gravatar image

updated 2015-04-20 05:07:57 -0600

berak gravatar image

I have an image and I'm trying to find the filled circles inside image (MCQ's).

image description

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 flag offensive close merge delete

Comments

1

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

berak gravatar imageberak ( 2015-04-18 07:28:16 -0600 )edit

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.
StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-20 05:01:19 -0600 )edit
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)

berak gravatar imageberak ( 2015-04-20 05:09:15 -0600 )edit
1

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

Zain ul abdeen gravatar imageZain ul abdeen ( 2015-04-20 14:06:19 -0600 )edit

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

Abhishek100 gravatar imageAbhishek100 ( 2017-07-01 23:43:10 -0600 )edit

4 answers

Sort by ยป oldest newest most voted
1

answered 2015-04-20 05:24:52 -0600

sgarrido gravatar image

updated 2015-04-20 05:59:56 -0600

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.

edit flag offensive delete link more
0

answered 2016-06-10 04:39:37 -0600

AsparaJoe gravatar image

updated 2016-06-10 04:43:37 -0600

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.

edit flag offensive delete link more
0

answered 2015-04-20 10:12:02 -0600

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.

edit flag offensive delete link more

Comments

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-21 01:54:32 -0600 )edit
1

It seems I totally misunderstood the question, then

Pedro Batista gravatar imagePedro Batista ( 2015-04-21 04:13:55 -0600 )edit
0

answered 2015-04-18 07:20:56 -0600

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

http://docs.opencv.org/modules/imgpro...

edit flag offensive delete link more

Comments

I dont want area. I want pixels inside the contour

Zain ul abdeen gravatar imageZain ul abdeen ( 2015-04-18 08:14:50 -0600 )edit
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.

matman gravatar imagematman ( 2015-04-20 13:59:09 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2015-04-18 06:16:21 -0600

Seen: 8,630 times

Last updated: Jun 10 '16