在opencv中查找对象的凸包?

时间:2012-08-05 01:44:53

标签: c++ c opencv convex-hull

我是根据教程here编写的,但是我无法获得图像的凸包(我正在使用教程中显示的类似手形图像)。我得到的源和边输出很好,但绘制轮廓和凸包线的“绘图”输出不显示任何绘制的内容,而是完全黑色。关于为什么会这样的任何想法?

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>

int main(int argc,char **argv)
{
    cvNamedWindow( "Source", 1 );
    cvNamedWindow( "edges window", 1 );
    cvNamedWindow( "Drawings", 1 );

    IplImage* src = cvLoadImage( "img.jpg", 0 );
    IplImage* edges = cvCreateImage( cvGetSize(src), 8, 1 );

    // Finding edges
    cvThreshold( src, edges, 150, 255, CV_THRESH_BINARY );

    CvMemStorage* storage = cvCreateMemStorage();
    CvSeq* first_contour = NULL;

    int Nc = cvFindContours(
        edges,
        storage,
        &first_contour,
        sizeof(CvContour),
        CV_RETR_LIST );

    // Finding convex Hull
    CvMemStorage* hull_storage = cvCreateMemStorage();
    CvSeq* retHulls = NULL;

    for(CvSeq* i = first_contour; i != 0; i = i->h_next){
    // note h_next is next sequence.
    retHulls = cvConvexHull2(first_contour,hull_storage,CV_CLOCKWISE,1);

    }

    // drawing contours and hull
    IplImage* draw = cvCreateImage(cvGetSize(edges), 8, 3 );

    for(CvSeq* i = first_contour; i != 0; i = i->h_next){
        cvDrawContours(draw,first_contour,cvScalar(255,0,0,0),cvScalar(255,0,0,0),0,1,8);
        cvDrawContours(draw,retHulls,cvScalar(255,0,0,0),cvScalar(255,0,0,0),0,1,8);

    }

    cvShowImage( "Source", src );
    cvShowImage( "edges window", edges );
    cvShowImage( "Drawings", draw );
    cvWaitKey();

    cvDestroyAllWindows();

    cvReleaseImage( &src );
    cvReleaseImage( &edges );
    cvReleaseImage( &draw );

    return 0;
}

2 个答案:

答案 0 :(得分:3)

您必须进行以下更改:

  1. CV_RETR_LIST函数中将参数从CV_RETR_EXTERNAL更改为cvFindContours
  2. CV_THRESH_BINARY中的CV_THRESH_OTSU更改为cvThreshold
  3. 这是证明(输入/输出):

    enter image description here enter image description here

答案 1 :(得分:0)