OpenCV:findContours异常 [英] OpenCV: findContours exception

查看:1258
本文介绍了OpenCV:findContours异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的matlab代码是:

my matlab code is:

h = fspecial('average', filterSize);
imageData = imfilter(imageData, h, 'replicate');
bwImg = im2bw(imageData, grayThresh);

cDist=regionprops(bwImg, 'Area');
cDist=[cDist.Area];

opencv代码是:

opencv code is:

cv::blur(dst, dst,cv::Size(filterSize,filterSize));
dst = im2bw(dst, grayThresh);

cv::vector<cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;
cv::findContours(dst,contours,hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);

这里是我的image2blackand白色函数

here is my image2blackand white function

cv::Mat AutomaticMacbethDetection::im2bw(cv::Mat src, double grayThresh)
{
    cv::Mat dst;
    cv::threshold(src, dst, grayThresh, 1, CV_THRESH_BINARY);
    return dst; 
}

我在 findContours > C ++异常:cv ::在内存位置0x0000003F6E09E0A0处的异常。

I'm getting an exception in findContours() C++ exception: cv::Exception at memory location 0x0000003F6E09E0A0.

你能解释我做错了什么。
dst是cv :: Mat,我一直使用它有我的原始值。

Can you please explain what am I doing wrong. dst is cv::Mat and I used it all along it has my original values.

更新这里是我的矩阵写入* .txt文件:
http://www.filedropper.com/gili

Update here is my matrix written into *.txt file: http://www.filedropper.com/gili

更新2:
我添加了 dst.convertTo(dst,CV_8U); 像Micka建议,我不再有异常。

UPDATE 2: I have added dst.convertTo(dst,CV_8U); like Micka suggested, I no longer have an exception. however values are nothing like expected.

推荐答案

看看这个问题,你遇到类似的问题: Matlab和OpenCV计算不同的图像矩m00

Take a look at this question which has a similar problem to what you're encountering: Matlab and OpenCV calculate different image moment m00 for the same image.

基本上,链接帖子中的OP试图为 x 中的 y OpenCV和 regionprops 。在MATLAB中,可以通过区域属性从 regionprops 访问,并从您的MATLAB代码判断,找到相同的数量。

Basically, the OP in the linked post is trying to find the zeroth image moment for both x and y of all closed contours - which is actually just the area, by using findContours in OpenCV and regionprops in MATLAB. In MATLAB, that can be accessed by the Area property from regionprops, and judging from your MATLAB code, you wish to find the same quantity.

从这篇文章中可以看出,OpenCV和MATLAB如何在图像中找到轮廓。这归结为两个平台考虑什么是连接像素的方式。 OpenCV只使用四像素邻域,而MATLAB使用八像素邻域。

From the post, there is most certainly a difference between how OpenCV and MATLAB finds contours in an image. This boils down to the way both platforms consider what is a "connected pixel". OpenCV only uses a four-pixel neighbourhood while MATLAB uses an eight-pixel neighbourhood.

因此,您的实现没有问题,转换为 8UC1 是好的。然而,使用MATLAB和OpenCV发现的两个轮廓之间的区域(最终连接的组件和轮廓本身的总数)是不一样的。获得完全相同结果的唯一方法是,如果您在黑色图像上手动绘制由 findContours 找到的轮廓,并使用 cv: :moments 直接对此图片执行。

As such, there is nothing wrong with your implementation, and converting to 8UC1 is good. However, the areas (and ultimately the total number of connected components and contours themselves) between both contours found with MATLAB and OpenCV are not the same. The only way for you to get exactly the same result is if you manually draw the contours found by findContours on a black image, and using the cv::moments function directly on this image.

但是,由于 cv :: blur() fspecial 使用均匀的平均值掩码,您仍然可能无法在图像的边框中获得相同的结果。如果您的图片边框周围没有重要轮廓,那么希望这会给您正确的结果。

However, because of the differing implementations of cv::blur() in comparison to fspecial with an averaging mask that is even, you still may not be able to get the same results along the borders of the image. If there are no important contours around the borders of your image, then hopefully this will give you the right result.

运气!

这篇关于OpenCV:findContours异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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