1

I want to calculate area of detected object (that blue marker I used) inside actual ROI. I mean one of those two rectangles that are my Regions Of Interest in Threshold image (which is black and white).

How to calculate area of object (I think - sum of "recognized" cluster of pixels, which are white color) inside ROI?

enter image description here

I just want to consider only those pixels that are inside of concrete ROI (in below example - the left one). So all pixels beyond left ROI will not be taken into calculations.

enter image description here

ROIs are created like this:

rectangle( imgOriginal, Point( 20, 100 ), Point( 170, 250), Scalar( 0, 0, 255 ), +5, 4 );
rectangle( imgThresholded, Point( 20, 100 ), Point( 170, 250), Scalar( 255, 255, 255 ), +5, 4 );
rectangle( imgOriginal, Point( 450, 100 ), Point( 600, 250), Scalar( 0, 0, 255 ), +5, 4 );
rectangle( imgThresholded, Point( 450, 100 ), Point( 600, 250), Scalar( 255, 255, 255 ), +5, 4 );
ScriptyChris
  • 639
  • 4
  • 16
  • 48

3 Answers3

3

You may use cv::countNonZero function to count non-zero pixels inside ROI in imgThresholded image. This is exactly what you need.

cv::Rect leftROI(cv::Point(20, 100), cv::Point(170, 250));
int leftArea = cv::countNonZero(imgThresholded(leftROI));
akarsakov
  • 2,134
  • 4
  • 21
  • 26
  • Can I calculate like this all content of Threshold image by typing `cv::Rect leftROI(cv::Point(0, 0), cv::Point(640, 480));` so at `Point`s i enter full size (width and height) of binary image? And after that could I substract sum of white pixels in full binary image from sum of white pixels in chosen ROI? I want to calibrate detection of marker color by calculating that whole area white pixels (except of ROI) is decreasing and, in the same time, amount white pixels in ROI are increasing. So then, more white pixels is visible/detected in ROI area - so object is detected correctly. – ScriptyChris Mar 24 '16 at 15:48
  • 1
    Of course you can. May it would better to find ratio of sum of white pixels in chosen ROI to sum of white pixels in full binary image. Moreover, you can count white pixels on full binary image using `cv::countNonZero(imgThresholded)` – akarsakov Mar 24 '16 at 15:58
  • This code works, except I have problem with subtracting **leftArea** from **imgTresholded**. I am trying like this `cv::Rect leftROI(cv::Point(25, 105), cv::Point(165, 245));int leftArea = cv::countNonZero(imgThresholded(leftROI)); int excluded; subtract(cv::countNonZero(imgThresholded), leftArea, excluded);` - and i got error **"no suitable constructor extsts to convert from 'int' to 'cv::OutputArray'"**. Why `int excluded` is not correct type, when rest of them can be `int`? I can do it with `Mat excluded`. However, if image doesn't contain white pixels, it still shows some number instead 0. – ScriptyChris Mar 24 '16 at 16:40
  • 1
    If I understand correctly you don't need to use `cv::substract`, just calculate like: `int excluded = cv::countNonZero(imgThresholded) - leftArea;`. – akarsakov Mar 24 '16 at 16:56
  • so simple. Thank you very much! – ScriptyChris Mar 24 '16 at 17:11
0

So technically you found the "largest contour" based on some color filtering, drew a rectangle around it and trying now to get its area. To get its area, OpenCv provided the function for you (How did it find that it's the largest?) so here is the function: double cv::contourArea(InputArray contour)

Here is the main page because I think you can send the contour as it is but I am not sure but check this basic example and :

vector<Point> contour;
contour.push_back(Point2f(0, 0)); // push whatever points you want
contour.push_back(Point2f(10, 0));
contour.push_back(Point2f(10, 10));
contour.push_back(Point2f(5, 4));
double area0 = contourArea(contour); // retrieve the area

vector<Point> approx;
approxPolyDP(contour, approx, 5, true);
double area1 = contourArea(approx);
cout << "area0 =" << area0 << endl <<
        "area1 =" << area1 << endl <<
        "approx poly vertices" << approx.size() << endl;
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
0

For specific ROI on a binary image, sweep through pixel values and count the white (255) ones. If you have more ROIs, and want to avoid those, not containing white pixels, then simply skip those, that does not contain any white pixels...

j.kaspar
  • 751
  • 1
  • 11
  • 29