aihot  2017-05-07 07:51:05  OpenCV |   查看评论   
3 rectangle(result, r, Scalar(255), 2); 4 5 // 轮廓表示为一个圆 6 float radius; 7 Point2f center; 8 minEnclosingCircle(Mat(contours[1]), center, radius); 9 circle(result, Point(center), static_cast<int>(radius), Scalar(255), 2); 10 11 // 轮廓表示为一个多边形 12 vector<Point> poly; 13 approxPolyDP(Mat(contours[2]), poly, 5, true); 14 vector<Point>::const_iterator itp = poly.begin(); 15 while (itp != (poly.end() - 1)) 16 { 17 line(result, *itp, *(itp + 1), Scalar(255), 2); 18 ++itp; 19 } 20 line(result, *itp, *(poly.begin()), Scalar(255), 2); 21 // 轮廓表示为凸多边形 22 vector<Point> hull; 23 convexHull(Mat(contours[3]), hull); 24 vector<Point>::const_iterator ith = hull.begin(); 25 while (ith != (hull.end() - 1)) 26 { 27 line(result, *ith, *(ith + 1), Scalar(255), 2); 28 ++ith; 29 } 30 line(result, *ith, *(hull.begin()), Scalar(255), 2);

 

程序中我们依次画了矩形、圆、多边形和凸多边形。最终效果如下:

 

OpenCV成长之路(8):直线、轮廓的提取与描述

对连通区域的分析到此远远没有结束,我们可以进一步计算每一个连通区域的其他属性,比如:重心、中心矩等特征,这些内容以后有机会展开来写。

以下几个函数可以尝试:minAreaRect:计算一个最小面积的外接矩形,contourArea可以计算轮廓内连通区域的面积;pointPolygenTest可以用来判断一个点是否在一个多边形内。mathShapes可以比较两个形状的相似性,相当有用的一个函数。

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV成长之路(8):直线、轮廓的提取与描述

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]