使用OpenCV计算对象的面积 [英] Calculate the area of an object with OpenCV

查看:406
本文介绍了使用OpenCV计算对象的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 OpenCV 来计算灰度图片中blob /对象的面积(使用Mat,而不是IplImage)。
我认为这是一个好主意,获得边缘的坐标(边缘数目改变对象到对象)或获得轮廓的所有坐标,然后使用 contourArea()来计算我的对象的面积。

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.

我删除了所有的噪音,并通过使用 findContours )

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());

现在我明白了param contoururs 已经拥有我的对象的所有轮廓的坐标。

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?

感谢andvance!

Thanks in andvance!

推荐答案

轮廓实际上定义为

vector<vector<Point> > contours;

现在我认为很清楚如何访问它的点。

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

轮廓区域是由一个很好地称为 contourArea()的函数计算的:

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;
}

这篇关于使用OpenCV计算对象的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆