Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the area of an object with OpenCV

I need to calculate the area of a blob/an object in a grayscale picture (loading it as Mat, not as IplImage) using OpenCV. I thought it would be a good idea to get the coordinates of the edges (number of edges change form object to object) or to get all coordinates of the contour and then use contourArea() to calculate the area of my object.

I deleted all noise and got some nice and satisfying contours by using findContours() (programming in C++).

findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy,int mode, int method, Point offset=Point());

Now I got to understand that param contours already owns the coordinates of all contours of my object. Did I get that right?

If yes, it there a way to access them?

And if no, how do I get the coordinates of the contour anyway?

like image 495
Temer Avatar asked Jul 24 '12 13:07

Temer


People also ask

How do we measure items on drawing with OpenCV?

The most common way is to perform a checkerboard camera calibration using OpenCV. Doing so will remove radial distortion and tangential distortion, both of which impact the output image, and therefore the output measurement of objects in the image.

How do you find the size of an object in a picture?

To work out the size of the object on the sensor, work out it's height in pixels, divide by the image height in pixels and multiply by the physical height of the sensor.


1 Answers

contours is actually defined as

vector<vector<Point> > contours;

And now I think it's clear how to access its points.

The contour area is calculated by a function nicely called contourArea():

for (unsigned int i = 0;  i < contours.size();  i++)
{
     std::cout << "# of contour points: " << contours[i].size() << std::endl;

     for (unsigned int j=0;  j<contours[i].size();  j++)
     {
         std::cout << "Point(x,y)=" << contours[i][j] << std::endl;
     }

     std::cout << " Area: " << contourArea(contours[i]) << std::endl;
}
like image 70
Sam Avatar answered Sep 23 '22 21:09

Sam