2012-08-05 65 views
2

我已經根據教程here編寫了這個教程,但我無法獲得圖像的凸包(我使用的教程中顯示了類似的手形圖像)。我得到的源和邊輸出很好,但應該畫輪廓和凸包線的「繪圖」輸出不顯示任何繪製的東西,而是完全黑色的。任何想法爲什麼這可能是?在opencv中查找對象的凸包?

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

回答

3

你必須做到以下幾點變化:從CV_RETR_LISTcvFindContours功能

  1. 更改參數CV_RETR_EXTERNAL
  2. 更改CV_THRESH_BINARYCV_THRESH_OTSUcvThreshold

這裏的證明(輸入/輸出):

enter image description hereenter image description here

+0

您好,嗯改變它在我的代碼沒有影響?你把我的代碼或你自己試試呢? - 謝謝 – silent 2012-08-06 07:48:10

+1

@silent看更新。 – ArtemStorozhuk 2012-08-06 18:05:18

+0

爲圖像提供工作,但它不適用於這樣的事情的歡呼聲? http://i.imgur.com/1nYN7.jpg有關爲什麼? – silent 2012-08-08 13:42:41