How to extract thin lines as separate loops / connected components using OpenCV?

I used thinning the distance between images on the image. Now let's try to extract each connected component separately - 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, randomly colored, like a different path / component. However, it discovers closed forms as contours. How to define lines as components?

Output Image: Input image

Input image: enter image description here

Red flags indicate sample parts that should be detected as components. But only closed forms are found.

+3
source share
1 answer

/ , findContours thin_image ( → → ).

+2

Source: https://habr.com/ru/post/1790203/


All Articles