我是根据教程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;
}
答案 0 :(得分:3)
您必须进行以下更改:
CV_RETR_LIST
函数中将参数从CV_RETR_EXTERNAL
更改为cvFindContours
。CV_THRESH_BINARY
中的CV_THRESH_OTSU
更改为cvThreshold
。这是证明(输入/输出):
答案 1 :(得分:0)
有两种方法,
http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/hull/hull.html
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);
HTH