本文介绍了如何使用openCV函数计算凸包面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到如何使用OpenCV计算凸包面积的有效示例.我看到了一个使用cvApproxPoly和cvContourArea的示例,但是我无法使其工作.我有以下代码.
I'm unable to find a working example of how to calculate the area of a convex hull using OpenCV. I saw one example that uses cvApproxPoly and cvContourArea, but I couldn't make it work. I have the following code.
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
int i, count = rand()%100 + 1;
CvPoint pt0;
CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
int* hull = (int*)malloc( count * sizeof(hull[0]));
CvMat point_mat = cvMat( 1, count, CV_32SC2, points );
CvMat hull_mat = cvMat( 1, count, CV_32SC1, hull );
for( i = 0; i < count; i++ )
{
pt0.x = rand() % (img->width/2) + img->width/4;
pt0.y = rand() % (img->height/2) + img->height/4;
points[i] = pt0;
}
CvSeq* convex_hull=cvConvexHull2( &point_mat, &hull_mat, CV_CLOCKWISE, 0 );
推荐答案
vector<Point2f> originalPoints; // Your original points
vector<Point2f> convexHull; // Convex hull points
vector<Point2f> contour; // Convex hull contour points
double epsilon = 0.001; // Contour approximation accuracy
// Calculate convex hull of original points (which points positioned on the boundary)
convexHull(Mat(originalPoints),convexHull,false);
// Approximating polygonal curve to convex hull
approxPolyDP(Mat(convexHull), contour, 0.001, true);
cout << fabs(contourArea(Mat(contour)));
这篇关于如何使用openCV函数计算凸包面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!