标签:凸包 arch length 多边形 center double bsp pre ++
1 Mat src = imread("E:/guida.png", -1); 2 Mat edge; 3 Mat dst = Mat::zeros(src.size(), src.type()); 4 Canny(src, edge, 3, 9, 3);
一。寻找轮廓并画出轮廓
findcontours函数找出轮廓点的二维集合。
1 vector<vector<Point>>contours; 2 vector<Vec4i>hierarchy; 3 findContours(edge, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); 4 drawContours(dst, contours, -1, Scalar(255));
二。根据轮廓点集得到凸包的点集,并绘制
1 //画凸包,需要点集 2 vector<vector<Point>>hull(contours.size()); 3 for (int i = 0; i < contours.size(); i++) { 4 convexHull(Mat(contours[i]), hull[i]); 5 } 6 for (int i = 0; i < contours.size(); i++) { 7 drawContours(dst, contours, i, Scalar(255)); 8 drawContours(dst, hull, i, Scalar(255)); 9 }
三。外接矩形
1 Rect rect = boundingRect(contours[0]); 2 rectangle(edge, rect, Scalar(255));
四。最小包围矩形
1 //最小包围矩形 2 RotatedRect box = minAreaRect(contours[0]); 3 Point2f vertex[4]; 4 box.points(vertex); 5 for (int i = 0; i < 4; i++) { 6 line(edge, vertex[i], vertex[(i + 1)% 4], Scalar(255)); 7 }
五。最小包围圆和三角形
1 //最小包围圆 2 Point2f center; 3 float radius; 4 minEnclosingCircle(contours[0], center, radius); 5 circle(edge, center, radius, Scalar(255)); 6 //最小包围三角形 7 vector<Point2f> triangle; 8 minEnclosingTriangle(contours[1], triangle); 9 for (int i = 0; i < 3; i++) 10 line(edge, triangle[i], triangle[(i + 1) % 3], Scalar(255, 255, 0), 1, LINE_AA);
六。计算点集的逼近多边形,面积,多边形周长
1 vector<Point>contour; 2 contour.push_back(Point2f(0,0)); 3 contour.push_back(Point2f(10, 0)); 4 contour.push_back(Point2f(10, 10)); 5 contour.push_back(Point2f(5, 4)); 6 double area0 = contourArea(contour); 7 vector<Point>approx; 8 approxPolyDP(contour, approx, 5, true); 9 10 double area1 = contourArea(approx); 11 Mat test = Mat::zeros(Size(50, 50), CV_8UC1); 12 Mat test1 = Mat::zeros(Size(50, 50), CV_8UC1); 13 for (int i = 0; i < contour.size(); i++) 14 { 15 line(test, contour[i], contour[(i + 1) % 4], Scalar(255)); 16 } 17 for (int i = 0; i < approx.size(); i++) 18 { 19 line(test1, approx[i], approx[(i + 1) % approx.size()], Scalar(255)); 20 }
double length=arcLength(approx,true);
根据原点集绘制的多边形
根据逼近多边形绘制的
标签:凸包 arch length 多边形 center double bsp pre ++
原文地址:https://www.cnblogs.com/sclu/p/11982052.html