Ask Your Question
2

Count pixels of a black and white image (OpenCV / C++ )

asked 2015-02-16 16:48:07 -0600

jefferson-sep gravatar image

updated 2015-02-17 19:14:35 -0600

theodore gravatar image

I am a beginner to opencv / C ++ and would like your help with a problem that seems simple. As an example, I have this image:

image description

... And would disregard the background, which will always be white, and the image is always black and white, leaving only the cloud to be able to count three things:

  1. The number of pixels of the figure (cloud only, disregarding the background).
  2. The number of white pixels. (cloud only)
  3. The number of black pixels. (cloud only)

I know that to achieve 2, with a subtraction with it the 3rd.

Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2015-02-16 18:05:12 -0600

theodore gravatar image

updated 2016-02-01 10:05:33 -0600

You just need to play with the findContours() and findNonZero() functions.

Have a look, on the code below:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    // Load source image
    string filename = "cloud.png";
    Mat src = imread(filename);

    // Check if image is loaded fine
    if(!src.data)
        cerr << "Problem loading image!!!" << endl;

    // Show source image
    imshow("src", src);

    // Transform source image to gray if it is not
    Mat gray;

    if (src.channels() == 3)
    {
        cvtColor(src, gray, CV_BGR2GRAY);
    }
    else
    {
        gray = src;
    }

    // Show gray image
    imshow("gray", gray);

    // Transform it to binary and invert it. White on black is needed.
    Mat bw;
    threshold(gray, bw, 40, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    vector<Point> black_pixels;   // output, locations of non-zero pixels
    cv::findNonZero(bw, black_pixels);

    cout << "Cloud all black pixels: " << black_pixels.size() << endl; // amount of black pixels is returned from the size

    // Show binary image
    imshow("binary", bw);

    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    // extract only the external blob
    findContours(bw.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    Mat mask = Mat::zeros(bw.size(), CV_8UC1);

    // draw the contours as a solid blob, and create a mask of the cloud
    for(size_t i = 0; i < contours.size(); i++)
        drawContours(mask, contours, i, Scalar(255, 255, 255), CV_FILLED, 8, hierarchy, 0, Point());

    imshow("mask", mask);

    vector<Point> all_pixels;   // output, locations of non-zero pixels
    cv::findNonZero(mask, all_pixels);

    cout << "Cloud all pixels: " << all_pixels.size() << endl; // amount of all pixels is returned from the size

    int white_pixels = all_pixels.size() - black_pixels.size(); // amount of white pixels is returned from the subtraction of all - black ;-)

    cout << "Cloud all white pixels: " << white_pixels << endl;

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

and if you also want to extract the coordinates for the white pixels within the cloud just add the following code:

    Mat white = ~bw - ~mask;
    vector<Point> whitee_pixels;   // output, locations of non-zero pixels
    cv::findNonZero(white, whitee_pixels);

    cout << "Cloud all white pixels: " << whitee_pixels.size() << endl;
theodore gravatar imagetheodore ( 2015-02-16 19:01:45 -0600 )edit

Thanks for the help!!!

jefferson-sep gravatar imagejefferson-sep ( 2015-02-16 21:38:01 -0600 )edit

I am getting error while calculating number of white and black pixel OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1DataType<_Tp>::channels) < (unsigned)(size.p[1]channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv2.4.13\opencv\build\include\opencv2\core\mat.hpp, line 539

monikadeshmukh72 gravatar imagemonikadeshmukh72 ( 2017-02-23 01:51:57 -0600 )edit

why are you using the 2.4 version? I would suggest you to use the latest build, otherwise it is not easy to debug.

theodore gravatar imagetheodore ( 2017-02-26 17:44:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-16 16:48:07 -0600

Seen: 17,198 times

Last updated: Feb 01 '16