cvFindContours()未检测到单独的组件 [英] cvFindContours() not detecting separate components

查看:136
本文介绍了cvFindContours()未检测到单独的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找图像的一小部分中存在的连接组件(如果有).但是,cvFindContours()函数将明显独立的组件分组为一个组件,从而导致进一步的计算不正确.

I'm trying to find connected components present (if any) within a small region of an image. However the cvFindContours() function is grouping visibly separate components into one single component making further calculations incorrect.

如何获得该区域内的独立组件? (该功能检测到的不同组件都有颜色.)

How do I get the separate components within the region? (Colored different components as detected by the function).

代码如下:

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

CvMemStorage *mem;
mem = cvCreateMemStorage(0);
CvSeq *contours = 0;
CvSeq *ptr;
int n_cont = 0;
int n = cvFindContours( src, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

for (ptr = contours; ptr != NULL; ptr = ptr->h_next) 
{
    n_cont++;

    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));        
}

"CEL"被视为单个组件!

"CEL" is considered to be a single component!

推荐答案

我不使用OpenCV,但我通过Mathematica进行了验证,您可能希望指定两个组件只能通过其顶部,底部,左侧,和正确的邻居.如果考虑完整的8个邻居,则所有三个字母都已连接,如下所示:

I don't use OpenCV, but I verified with Mathematica that you probably want to specify that two components should be connected only through their top, bottom, left, and right neighbors. If you consider the full 8 neighbors, then all the three letters are connected, as you showed:

comp = MorphologicalComponents[img, CornerNeighbors -> False];
Colorize[comp]

这篇关于cvFindContours()未检测到单独的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆