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