Ask Your Question
2

Count the pixel (black or white) in real time.

asked 2013-08-22 12:17:55 -0600

lenteken gravatar image

updated 2013-08-26 10:04:01 -0600

I need to count the black and white pixels of the object in REAL TIME. What is the best method to use for this ? In my program, First I get the biggest contour .The next process is to count the pixels(black) of the object that have no mask but I don't know how to do it. Thanks in advance. image description

 #include <opencv2/imgproc/imgproc.hpp>
 #include <opencv2/highgui/highgui.hpp>

 using namespace cv;
 int main(int argc, char** argv)
 {
 vector<Vec4i> hierarchy;
     CvCapture* capture = cvCaptureFromCAM(0);
 cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 270);
 cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 190);

    cv::Mat src;

    while(true) {
        src = cvQueryFrame( capture );

    Mat gray;
    cvtColor(~src, gray, CV_BGR2GRAY);
    //medianBlur(gray,gray,27);
    threshold(gray, gray, 220, 255, THRESH_BINARY);
    vector<vector<Point> > contours;
    findContours( gray.clone(), contours, hierarchy,CV_RETR_EXTERNAL,
    CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );


    for (int i = 0; i < contours.size(); i++)
    {
        double area =contourArea(contours[i]);
        Rect rect = boundingRect(contours[i]);

        if (area >= 30&& abs(1 - ((double)rect.width / (double)rect.height)) <=
            0.1)   
        {

            Mat mask = Mat::zeros(gray.rows, gray.cols, CV_8UC1);
            drawContours( mask, contours, i, CV_RGB(255,255,255), -1, 8,
            vector<Vec4i>(), 0,Point() ); //masking//
            Mat crop(src.rows, src.cols, CV_8UC3);
            crop.setTo(Scalar(0,0,255));
            src.copyTo(crop, mask);


            Mat masked;
            gray.copyTo(masked, mask);
            int count_black = cv::countNonZero(masked == 255);
            printf("count_black = %d\n",count_black);
            printf("ContourArea = %.0f\n\n",
                  ((contourArea(contours[i]))+( arcLength( contours[i],
                    true )/2)));


           imshow("cropped", crop);
           imshow("source", src);
            waitKey(33);
        }


    }



}
}
edit retag flag offensive close merge delete

Comments

5 peso coin cool.

ishralene09 gravatar imageishralene09 ( 2013-11-21 19:55:11 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2013-08-25 21:41:30 -0600

You can use the function cv::countNonZero to count the number of black pixels. If your image only contains black and white pixels, the number of white pixels will be: (image_width * image_height) - black_pixel_count

edit flag offensive delete link more

Comments

I used the countNonZero but I can't get the black pixels. Please look at the code that I have update.

lenteken gravatar imagelenteken ( 2013-08-26 08:48:34 -0600 )edit

What you are trying to achieve with cv::countNonZero(masked == 255)? http://docs.opencv.org/modules/core/doc/operations_on_arrays.html says argument must be array, not some logical comparison.

Tõnu Samuel gravatar imageTõnu Samuel ( 2013-08-26 13:11:21 -0600 )edit

In that part, I count the black pixels but the result is wrong.

lenteken gravatar imagelenteken ( 2013-08-26 13:15:13 -0600 )edit

You do not pass Matrix as argument, do you understand this?

Tõnu Samuel gravatar imageTõnu Samuel ( 2013-08-26 13:45:06 -0600 )edit

sorry but I don't understand. Please edit my code so I can understand it easily.

lenteken gravatar imagelenteken ( 2013-08-26 14:09:34 -0600 )edit

@lenteken have you solved it already? (I assume after 3 months you should have). You only pass the Mat object on the countNonZero function, and it will return the number of nonzero (anything not black) pixels. Subtract that to the total number of pixels, and you get your black pixel count.

ishralene09 gravatar imageishralene09 ( 2013-11-21 20:02:05 -0600 )edit

Question Tools

Stats

Asked: 2013-08-22 12:17:55 -0600

Seen: 19,664 times

Last updated: Aug 26 '13