gpt4 book ai didi

c - 如何使用openCV函数计算凸包面积?

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:43 24 4
gpt4 key购买 nike

我找不到有关如何使用 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)));

关于c - 如何使用openCV函数计算凸包面积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com