3

Input Image -- https://i.stack.imgur.com/hIqEd.png Output Image with Convex Hull of contours drawn -- https://i.stack.imgur.com/pEKPP.png

Also any help on how to get one contour will be much appreciated?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
user662239
  • 145
  • 2
  • 6
  • Show some code. How are you calling FindCountours? This can be probably solved by calling the function with the right parameters :) – karlphillip Sep 02 '11 at 21:01

1 Answers1

2

The trick to retrieve the image as one contour seems to be processing the image with Canny before executing cvFindContours.

IplImage* src = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);

IplImage* cc_img = cvCreateImage( cvGetSize(src), src->depth, 3 );
cvSetZero(cc_img);
CvScalar(ext_color);

CvMemStorage *mem;
mem = cvCreateMemStorage(0);
CvSeq *contours = 0;

// edges returned by Canny might have small gaps between them, which causes some problems during contour detection
// Simplest way to solve this s to "dilate" the image.
cvCanny(src, src, 10, 50, 3); 
cvShowImage("Tutorial", src);
cvWaitKey(0);

int n = cvFindContours( src, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

CvSeq* ptr = 0;
for (ptr = contours; ptr != NULL; ptr = ptr->h_next)
{   
    ext_color = CV_RGB( rand()&255, rand()&255, rand()&255 ); //randomly coloring different contours
    cvDrawContours(cc_img, ptr, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}   

cvNamedWindow("Tutorial");
cvShowImage("Tutorial", cc_img);
//cvSaveImage("out.png", cc_img);

cvWaitKey(0);

Outputs:

enter image description here

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Hi, Thanks for the reply. Im using opencv android. I m performing a bilateral Filter(Imgproc.bilateralFilter(inputMatrix, bilateralFilteredMatrix, 3, 50, 50)) and MorphologyEx operations (MORPH_OPEN and MORPH_CLOSE) initially, then performing a Canny on the image. I see that Opencv returns me 2 contours after that. But if I dont do the initial noise reduction and do only Canny as per your approach, it returns me one contour?! But for other realtime images where there is noise, I need to do apply these filters. Why does FindContours return me 2 contours after applying filters? – user662239 Sep 03 '11 at 06:00
  • One of the filters might be breaking the image apart, creating a gap in the drawing of the symbol, small enough for ` cvContours` to think it's dealing with 2 separate components. Either that, or there might be some other problem in the implementation of your logic but we can't help you on that since there's no code on this question. – karlphillip Sep 03 '11 at 16:09