opencv countour area returns zero

半世苍凉 提交于 2019-12-25 02:55:41

问题


I'm learning opencv, by using the book with the same name. I'd like to calculate the area of a contour but it always return 0. The contours are painted as closed polygons, so this seems to be correct.

There are some samples out there, but they are using vector<vector<Point>> contours. My code below is based on a book sample. The reference image which I'm using is a grayscale one.

So my question is: What am I missing to get the area != 0?

#include <opencv\cv.h>
#include <opencv\highgui.h>

#define CVX_RED CV_RGB(0xff,0x00,0x00)
#define CVX_BLUE CV_RGB(0x00,0x00,0xff)

int main(int argc, char* argv[]) {

 cvNamedWindow( argv[0], 1 );

IplImage* img_8uc1 = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
IplImage* img_edge = cvCreateImage( cvGetSize(img_8uc1), 8, 1 );
IplImage* img_8uc3 = cvCreateImage( cvGetSize(img_8uc1), 8, 3 );

cvThreshold( img_8uc1, img_edge, 128, 255, CV_THRESH_BINARY );
CvMemStorage* storage = cvCreateMemStorage();

CvSeq* contours = NULL;
int num_contours = cvFindContours(img_edge, storage, &contours, sizeof(CvContour),
    CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0, 0));
printf("Total Contours Detected: %d\n", num_contours );

int n=0;
for(CvSeq* current_contour = contours; current_contour != NULL;  current_contour=current_contour->h_next ) {

    printf("Contour #%d\n", n);
    int point_cnt = current_contour->total;
    printf(" %d elements\n", point_cnt );

    if(point_cnt < 20){
        continue;
    }

    double area = fabs(cvContourArea(current_contour, CV_WHOLE_SEQ, 0));
    printf(" area: %d\n", area );

    cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);
    cvDrawContours(img_8uc3, current_contour, CVX_RED, CVX_BLUE, 0, 2, 8);

    cvShowImage(argv[0], img_8uc3);

    cvWaitKey(0);
    n++;
 }

 printf("Finished contours.\n");

 cvCvtColor( img_8uc1, img_8uc3, CV_GRAY2BGR );

 cvShowImage( argv[0], img_8uc3 );
 cvWaitKey(0);
 cvDestroyWindow( argv[0] );
     cvReleaseImage( &img_8uc1 );
 cvReleaseImage( &img_8uc3 );
 cvReleaseImage( &img_edge );

 return 0;
}

回答1:


This happened not because the 'area' is 0 but because you used printf with flag %d (integer) instead of %f (double). If you use appropriate flag you will see real value of 'area'. For this reason I am always using cout instead of printf. This saves a lot problems of this kind.

On the side note. You are learning here C interface of OpenCV. I would recommend you to learn its C++ interface instead (it was added to OpenCV since version 2.0). First, C interface is deprecated and most likely will be removed completely from next version of OpenCV. Second, it is more complicated than C++ interface. In case of cvFindContours it is MUCH more complicated. Here you can find the required documentation for all the interfaces.



来源:https://stackoverflow.com/questions/21443436/opencv-countour-area-returns-zero

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!