OpenCV findContours allways为每个对象找到两个轮廓 [英] OpenCV findContours allways finds two contours for every object

查看:168
本文介绍了OpenCV findContours allways为每个对象找到两个轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了一个小程序,它在颜色阈值二进制图像中搜索轮廓,在阈值处理之后用canny处理,但不知何故,它总是为图像中的每个对象找到两个轮廓。

I just wrote a little program which searches for contours in a color thresholded binary image which is processed with canny after thresholding, but somehow it always finds two contours for every object in the image.

在右下角绘制找到的轮廓的图像中,您可以看到最大的轮廓绘制两次,在两个轮廓之间有一点偏移。下一张图片显示了此图片的详细视图。

In the bottom right image where the found contours are drawn, you can see that the biggest contour is drawn two times with a little offset between the two contours. The next image shows a detailed view of this image.

http://img831.imageshack.us/img831/3641/doubleframe2.png

这里只有最大的轮廓被绘制两次,但它对于每个其他轮廓随机发生。
我只想为每个对象一个轮廓,我怎么能实现呢? :/

Here just the biggest contour is drawn two times, but it happens randomly for every other one of the contours. I just want one contours for every object, how can i achieve that? :/

更新:


由findContours方法填充的轮廓向量的大小大小为8,大小只有4个。

Update:

The size of the contours vector which is filled by the findContours method has a size of 8 where it should have a size of just 4.

更新2:
这里是rgb输入来自kinect的图片
http://img405.imageshack.us/img405/9761 /inputimage.jpg
颜色阈值我使用以下方法

update 2:

Here is the rgb input image from the kinect http://img405.imageshack.us/img405/9761/inputimage.jpg for the color Threshold i used the following approach

cv::cvtColor(in, out, CV_BGR2HSV);
cv::inRange(out,
            cv::Scalar(25, 131, 97),
            cv::Scalar(35, 220, 217),
            out);

后跟一个尺寸为1的rect元素的侵蚀+膨胀。

followed by an erosion + dilation with an rect element with a size of 1.

推荐答案

我认为问题很可能是因为当你找到边缘,然后找到这些边缘的轮廓,有一个外边界和它的内边界。所以openCV采取两个作为轮廓和绘制它。

I think the problem is most likely because when you find the edges and then find the contours of those edges, there is an outer boundary and inner boundary for it. So openCV take both as contours and draw it.

当我计算其面积时,他们对'3'的值进行了非常小的更改(例如,两个轮廓与区域 9105.5和9108.5 ),这是最可能发生的,由于我提到的原因。 (所以他们很难区分)

When I calculated its areas, they have a very minor changes of a value of around '3' ( for example, biggest square has detected two contours with areas 9105.5 and 9108.5 ), which is most likely to happen due to the reason I mentioned. (So they are very difficult to distinguish)

如果你发现轮廓与找到canny边缘,问题可以解决。即直接从阈值图像中找到轮廓。

The problem can be solved if you find contours with out finding canny edges. ie directly find the contours from thresholded image. It will give you more accurate answer.

在第一种情况下(在canny之后),我有36个轮廓,而在第二种情况下,我只有22个轮廓。

In first case(after canny), I got 36 contours while in second case, i got only 22 contours.

尝试一下,希望它能解决问题!

Try it and hope it solves the problem !!!

这篇关于OpenCV findContours allways为每个对象找到两个轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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