Как рассчитать площадь выпуклой оболочки с помощью функций openCV? - PullRequest
4 голосов
/ 24 июня 2011

Я не могу найти рабочий пример того, как рассчитать площадь выпуклой оболочки с помощью 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 );

Ответы [ 4 ]

12 голосов
/ 26 июня 2011
    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)));
6 голосов
/ 25 июня 2011

На самом деле вычислить площадь 2D выпуклой оболочки довольно просто.Вы интегрируете область ниже каждой точки, идущей по часовой стрелке.Вот простой код, который делает это.(Несколько первых строк - определение и расчет выпуклой оболочки).

vector<Point2f> originalPoints;   // Your original points
vector<Point2f> ch;  // Convex hull points

// Calculate convex hull of original points (which points positioned on the boundary)
cv::convexHull(Mat(originalPoints),ch,false);
// false parameter is used to organize the points in clockwise direction

// Now calculate the area of sonvex hull 'ch':
double area = 0;
for (int i = 0; i < ch.size(); i++){
    int next_i = (i+1)%(ch.size());
    double dX   = ch[next_i].x - ch[i].x;
    double avgY = (ch[next_i].y + ch[i].y)/2;
    area += dX*avgY;  // This is the integration step.
}

area = abs (area);// Область может быть отрицательной, если интеграция началась справа налево.

4 голосов
/ 23 марта 2012

использование: CvSeq* convex_hull=cvConvexHull2( &point_mat, &hull_mat, CV_CLOCKWISE, **1** ); // !!

мой код:

 CvMemStorage* storage = cvCreateMemStorage(0);
 CvSeq* contours = 0;
 cvFindContours(bordImage, storage, &contours, sizeof(CvContour),
            CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

 for(CvSeq* seq = contours; seq != 0; seq = seq->h_next){
     cvDrawContours(dstImage, seq, CV_RGB(255,216,0), CV_RGB(0,0,250), 0, 1, 8);
 }

CvMemStorage* hullStorage = cvCreateMemStorage(0);
for(CvSeq* seq = contours; seq != 0; seq = seq->h_next){
    CvSeq *hulls = cvConvexHull2(seq, hullStorage, CV_CLOCKWISE, 1);
    cvDrawContours(dstImage, hulls, CV_RGB(255, 0, 0), CV_RGB(100, 0, 0), 0, 1, 8);
    for (int i = 0; i < hulls->total; i++) {
        CvPoint* p = (CvPoint*)cvGetSeqElem ( hulls, i );
         // ...
    }
    cvClearMemStorage(hullStorage);
}
1 голос
/ 29 марта 2015

В OpenCV 2.4.9:

double computeConvexHullArea(vector<Point> originalPoints)
{
    vector<Point> hull;
    convexHull(originalPoints, hull);

    return contourArea(hull);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...