OpenCV distanceTransform 返回意外结果(空 Mat 或原始图像)
我无法在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个调用是正确的,但由于距离较远,因此它不会存储为 uchar。当你想显示它时,OpenCV 会将这些浮点(我认为)转换为 uchar。结果看起来是黑色的。
找到输出中的最大值,然后将其缩放以适合灰度图像
编辑
第一个想法是正确的,但您实际上并没有尝试找到 maxVal!你说的是看图片,而不是实际提取它。胜利与失败的区别。
因此,使用精确算法计算 dist 变换,然后
编辑 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
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
Edit 2
And you must put the setTo(0) there.