Opencv contourArea轮廓面积检测

contourArea

Calculates a contour area.

double contourArea(InputArray contour, bool oriented=false )

Parameters:
contour – Input vector of 2D points (contour vertices), stored in std::vector or Mat.
oriented – Oriented area flag. If it is true, the function returns a signed area value, depending on the contour orientation (clockwise or counter-clockwise). Using this feature you can determine orientation of a contour by taking the sign of an area. By default, the parameter is false, which means that the absolute value is returned.会有方向的话,就会返回有正负,否则就是绝对值。
例子

vector<Point> contour;
contour.push_back(Point2f(0, 0));
contour.push_back(Point2f(10, 0));
contour.push_back(Point2f(10, 10));
contour.push_back(Point2f(5, 4));

double area0 = contourArea(contour);
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;
结果

可以看出拟合的多边形有误差,面积有5的差距,就是

void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)

中的epslion就是拟合的误差。可以看出,正好误差是5.

所以可以检测轮廓后,然后用contourArea算出面积去掉那些面积很小的轮廓,就可以实现一些差不多的目标检测的目的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 四月,凉爽的微风拂过因为疲惫而湿润的睫毛;凉风习习,就像是刚打开冰箱时迎面的凉意,带着春天的花香和刚被修过的...
    月移山影到窗前阅读 198评论 2 1