首页 文章

如何使用OpenCV将细线提取为单独的轮廓/连接组件?

提问于
浏览
3

我已经在图像上使用了距离变换细化 . 现在尝试分别提取每个连接的组件 - 如果有两条细线,那么它应该检测三个这样的单独的线和组件 .

/*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是输入 . 输出应使每条线随机着色为不同的轮廓/组件 . 然而,它仅检测闭合形状作为轮廓 . 我如何检测线条作为组件?

输出图像:
Input image

输入图片:
enter image description here

红色框表示应作为组件检测的示例部件 . 但只检测到闭合形状 .

1 回答

  • 3

    如果您正在寻找线条/边框作为组件而不是内部区域,则应在应用 findContours 之前反转 thin_image (黑色 - >白色和白色 - >黑色) .

相关问题