我找不到如何使用 OpenCV 计算凸包面积的工作示例。我看到了一个使用 cvApproxPoly 和 cvContourArea 的例子,但我无法让它工作。我有以下代码。

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 - 如何使用openCV函数计算凸包面积?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6471023/

10-13 03:30