为什么Contour区域只返回0?

时间:2014-05-14 03:24:07

标签: opencv

我试图找到轮廓区域,但轮廓区域仅返回0 无论轮廓是什么。 没有错误显示。

void CMFC_DEMODlg::OnBnClickedRun()
{
    CString str;

    IplImage* src1    = cvLoadImage("onlyone001.JPG",1);
    IplImage* src2    = cvLoadImage("b004.JPG",1);
    IplImage* grey    = cvCreateImage( cvGetSize(src2), 8, 1 );
    IplImage* dst    = cvCreateImage( cvGetSize(src2), 8, 3 );
    IplImage* F        = cvCreateImage( cvGetSize(src2), 8, 1 );
    IplImage* W        = cvCreateImage( cvGetSize(src2), 8, 1 );

    cvCvtColor( src2, grey, CV_BGR2GRAY);
    cvThreshold( grey, W,200,255,CV_THRESH_BINARY);

    CvSeq* c;
    CvSeq* contour;
    CvSeq* result;

    CvMemStorage* storage = cvCreateMemStorage(0);

    int Nc = cvFindContours(
    W,
    storage,
    &contour,
    sizeof(CvContour),
    CV_RETR_LIST,
    CV_CHAIN_APPROX_SIMPLE,
    cvPoint(0,0));

    double Area;

    result = cvApproxPoly(contour, sizeof(CvContour), storage,
        CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);

    Area = fabs(cvContourArea(result, CV_WHOLE_SEQ));

    str.Format("Area: %d\n", Area);
    GetDlgItem(IDC_STATIC02)->SetWindowText(str);

    str.Format("Nc: %d\n", Nc);
    GetDlgItem(IDC_STATIC01)->SetWindowText(str);


    m_CvvImage.CopyOf(src2,1);
    m_CvvImage.DrawToHDC(Disp_hDC1,Disp_Rect);

    m_CvvImage.CopyOf(src1,1);
    m_CvvImage.DrawToHDC(Disp_hDC2,Disp_Rect);

    m_CvvImage.CopyOf(W,1);
    m_CvvImage.DrawToHDC(Disp_hDC3,Disp_Rect);
}

我只想计算图像中每个轮廓的面积。我做错了什么?

1 个答案:

答案 0 :(得分:0)

这些点应按顺时针顺序放置在向量中。 以一个正方形为例:

corners2.push_back(Point(0,0));
corners2.push_back(Point(src.rows-1,0));
corners2.push_back(Point(src.rows-1,src.cols-1));
corners2.push_back(Point(1,src.cols-1));