opencv countour区域返回零 [英] opencv countour area returns zero

查看:289
本文介绍了opencv countour区域返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习opencv,使用同名的书。我想计算一个轮廓的面积,但它总是返回0.轮廓画成封闭的多边形,所以这似乎是正确的。

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.

所以我的问题是:我缺少什么来获得该区域?= 0?

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;
}


推荐答案

area'是0,但因为你使用printf与标志%d(整数)而不是%f(双)。如果你使用适当的标志,你会看到'面积'的真正价值。为此,我总是使用cout而不是printf。这节省了很多这样的问题。

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.

在旁注。你在这里学习OpenCV的C接口。我建议你学习它的C ++接口(它是从2.0版本添加到OpenCV)。首先,C接口已弃用,很可能将从下一版本的OpenCV中完全删除。二,比C ++界面更复杂。在cvFindContours的情况下它是更复杂。 此处您可以找到所有界面所需的文档。

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.

这篇关于opencv countour区域返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆