Reputation: 5148
I've used the distance tranform thinning on an image. Now trying to extract each connected component separtely - if there are two thin lines, then it should detect three such separate lines and components.
/*finding contours*/
IplImage *cc_color;
cc_color = cvCreateImage(cvGetSize(thin_img), IPL_DEPTH_8U, 3);
CvMemStorage *mem;
mem = cvCreateMemStorage(0);
int count = 0;
char* ch = new char [2];
CvSeq *contours = 0;
CvSeq *ptr;
/*finding contours of morphed image*/
cvFindContours(thin_img, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
/*all contours on one image - random coloring*/
for (ptr = contours; ptr != NULL; ptr = ptr->h_next)
{
CvScalar ext_color = CV_RGB( rand()&255, rand()&255, rand()&255 ); /*randomly coloring different contours*/
cvDrawContours(cc_color, ptr, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}
thin_image is the input. the output should have each line colored randomly as a different contour/component. However it's only detecting closed shapes as contours. How do i detect lines as components??
Output image:
Input image:
The red boxes indicate example parts that should be detected as components. But only closed-shapes are detected.
Upvotes: 3
Views: 3173
Reputation: 17141
If you are looking for the lines/borders as components instead of the inner regions, you should invert the thin_image
(black -> white and white -> black) before applying findContours
.
Upvotes: 3