2

我已经根据这里的教程编写了这个,但我无法获得图像的凸包(我使用的是类似教程中所示的手形图像)。我得到源和边缘输出很好,但应该绘制轮廓和凸包线的“绘图”输出没有显示任何绘制,而是完全黑色。关于为什么会这样的任何想法?

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

2 回答 2

3

您必须进行以下更改:

  1. 在函数中将参数从 更改CV_RETR_LIST为。CV_RETR_EXTERNALcvFindContours
  2. 更改CV_THRESH_BINARY为. CV_THRESH_OTSU_cvThreshold

这是证明(输入/输出):

在此处输入图像描述 在此处输入图像描述

于 2012-08-05T10:42:03.307 回答
0

有2种方法,

  1. opencv凸包

http://docs.opencv.org/doc/tutorials/imgproc/shapeescriptors/hull/hull.html

http://code.opencv.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ShapeDescriptors/hull_demo.cpp

  1. cvblob 凸包

https://code.google.com/p/cvblob/

https://code.google.com/p/cvblob/source/browse/trunk/cvBlob/cvblob.h?r=398

CvContourPolygon *cvPolygonContourConvexHull(CvContourPolygon const *p);

高温高压

于 2012-08-05T05:38:14.253 回答