OpenCV distanceTransform 返回意外结果(空 Mat 或原始图像)

发布于 2024-12-13 02:47:49 字数 526 浏览 4 评论 0原文

我无法在 ubuntu 上使用 OpenCV (2.3) 成功计算距离变换。输出要么是黑色,要么是原始图像的副本,但永远不会像预期的那样是带有渐变的灰度图像。

我的代码:

Mat input(Size(100,100), CV_8UC1);
circle(input, Point(50,50), 10 Scalar(255,255,255), 15, 0, 0));

Mat output(Size(100,100), CV_32FC1);
distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_3); //Output is completely black
//distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_PRECISE); //Output is a "copy" of input


imshow("in", input);
imshow("out", output);

有什么建议吗?

I cannot successfully calculate the distance transformation using OpenCV (2.3) on an ubuntu. The output is either black or a copy of the orignial image, but never as expected an greyscale image with gradients.

My code:

Mat input(Size(100,100), CV_8UC1);
circle(input, Point(50,50), 10 Scalar(255,255,255), 15, 0, 0));

Mat output(Size(100,100), CV_32FC1);
distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_3); //Output is completely black
//distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_PRECISE); //Output is a "copy" of input


imshow("in", input);
imshow("out", output);

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-12-20 02:47:49

第一个调用是正确的,但由于距离较远,因此它不会存储为 uchar。当你想显示它时,OpenCV 会将这些浮点(我认为)转换为 uchar。结果看起来是黑色的。

找到输出中的最大值,然后将其缩放以适合灰度图像

double maxVal = findMaxDistanceSomehow(); 

output.convertTo( displayBuffer, CV_8UC1, 255./maxVal,0);

imshow("dist", displayBuffer);

编辑

第一个想法是正确的,但您实际上并没有尝试找到 maxVal!你说的是看图片,而不是实际提取它。胜利与失败的区别。

因此,使用精确算法计算 dist 变换,然后

output.convertTo( displayBuffer, CV_8UC1, 10 ,0);

编辑 2

并且您必须将 setTo(0) 放在那里。

The first call is correct, but being a distance, it's not stored as uchar. When you want to display it, OpenCV converts those float (i think) into uchars. And the result seems black.

Find the max value in the output, and then scale it to fit a grayscale image

double maxVal = findMaxDistanceSomehow(); 

output.convertTo( displayBuffer, CV_8UC1, 255./maxVal,0);

imshow("dist", displayBuffer);

EDIT

The first idea was correct, but you actually did not try to find maxVal! You said that by looking at the picture, instead of actually extracting it. Difference between win and fail.

So, calculate the dist transform using the precise algoritm, and then

output.convertTo( displayBuffer, CV_8UC1, 10 ,0);

Edit 2

And you must put the setTo(0) there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文