Note that there are some explanatory texts on larger screens.

plurals
  1. POfinding convexity defects in opencv? [crashes depending on the given input image..]
    primarykey
    data
    text
    <p>I have a program that calculates the convex hull of an image. I'm trying to use this information in order to count the number of <em>fingers</em> that are present in an input image. From some surfing I found out that the way to do this (count fingers) is by</p> <ol> <li>Finding contours</li> <li>Convex Hull</li> <li>Convexity defects</li> </ol> <p>But I'm having trouble using the convexity defects function. It compiles fine but at runtime the program crashes with certain input images but not with others and I can't seem to figure out why. </p> <p>These are the input images</p> <ol> <li><a href="http://i50.tinypic.com/28mps1k.jpg" rel="nofollow">this</a> image causes a crash</li> <li>but <a href="http://i46.tinypic.com/x1dlvq.jpg" rel="nofollow">this</a> does not.</li> <li><a href="http://i50.tinypic.com/15q6sdi.jpg" rel="nofollow">this</a> also causes a crash even though its similar to the above</li> </ol> <p>code..</p> <pre><code>#include &lt;opencv/cv.h&gt; #include &lt;opencv/highgui.h&gt; #include &lt;opencv/cxcore.h&gt; #include &lt;stdio.h&gt; #define CVX_RED CV_RGB(0xff,0x00,0x00) #define CVX_GREEN CV_RGB(0x00,0xff,0x00) #define CVX_BLUE CV_RGB(0x00,0x00,0xff) int main(int argc, char* argv[]) { cvNamedWindow( "original", 1 ); cvNamedWindow( "contours", 1 ); cvNamedWindow( "hull", 1 ); IplImage* original_img = NULL; original_img = cvLoadImage("img.jpg", CV_LOAD_IMAGE_GRAYSCALE ); IplImage* img_edge = cvCreateImage( cvGetSize(original_img), 8, 1 ); IplImage* contour_img = cvCreateImage( cvGetSize(original_img), 8, 3 ); IplImage* hull_img = cvCreateImage( cvGetSize(original_img), 8, 3 ); cvThreshold( original_img, img_edge, 128, 255, CV_THRESH_BINARY ); CvMemStorage* storage = cvCreateMemStorage(); CvSeq* first_contour = NULL; int Nc = cvFindContours( img_edge, storage, &amp;first_contour, sizeof(CvContour), CV_RETR_LIST // Try all four values and see what happens ); for( CvSeq* c=first_contour; c!=NULL; c=c-&gt;h_next ) { cvCvtColor( original_img, contour_img, CV_GRAY2BGR ); cvDrawContours( contour_img, c, CVX_RED, CVX_BLUE, 0, 2, 8 ); } //----------------------------------------------------------------------Convex Hull CvMemStorage* hull_storage = cvCreateMemStorage(); CvSeq* retHulls = NULL; for(CvSeq* i = first_contour; i != NULL; i = i-&gt;h_next){ retHulls = cvConvexHull2(i,hull_storage,CV_CLOCKWISE,0); // with 1 it draws the Hull image but not with 0..? // however it needs to be 0 for convexitydefects to work? } printf(" %d elements:\n", retHulls-&gt;total ); // drawing hull for( CvSeq* j=retHulls; j!=NULL; j=j-&gt;h_next ) { cvCvtColor( original_img, hull_img, CV_GRAY2BGR ); cvDrawContours( hull_img, j, CVX_RED, CVX_BLUE, 0, 2, 8 ); } //----------------------------------------------------------------------Convexity Defects?? CvMemStorage* convexStorage = cvCreateMemStorage(); CvSeq* defect = NULL; defect = cvConvexityDefects(first_contour,retHulls, convexStorage); printf(" %d defect:\n", defect-&gt;total ); cvShowImage( "contours", contour_img ); cvShowImage( "original", original_img ); cvShowImage( "hull", hull_img ); cvWaitKey(0); cvDestroyWindow( "contours" ); cvDestroyWindow( "original" ); cvDestroyWindow( "hull" ); cvReleaseImage( &amp;original_img ); cvReleaseImage( &amp;contour_img ); cvReleaseImage( &amp;hull_img ); cvReleaseImage( &amp;img_edge ); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload