OpenCV 距离变换输出与输入图像完全相同的图像

发布于 2024-12-27 20:12:43 字数 1152 浏览 0 评论 0原文

我正在使用 OpenCV 做一些检测工作,我需要使用距离变换。除了 opencv 中的距离变换函数给我一个与我用作源的图像完全相同的图像。有人知道我做错了什么吗?这是我的代码部分:

cvSetData(depthImage, m_rgbWk, depthImage->widthStep);

//gotten openCV image in "depthImage"           

IplImage *single_channel_depthImage = cvCreateImage(cvSize(320, 240), 8, 1);
cvSplit(depthImage, single_channel_depthImage, NULL, NULL, NULL);

//smoothing
IplImage *smoothed_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvSmooth(single_channel_depthImage, smoothed_image, CV_MEDIAN, 9, 9, 0, 0);

//do canny edge detector
IplImage *edges_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvCanny(smoothed_image, edges_image, 100, 200);

//invert values
IplImage *inverted_edges_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvNot(edges_image, inverted_edges_image);

//calculate the distance transform
IplImage *distance_image = cvCreateImage(cvSize(320, 240), IPL_DEPTH_32F, 1);
cvZero(distance_image);

cvDistTransform(inverted_edges_image, distance_image, CV_DIST_L2, CV_DIST_MASK_PRECISE, NULL, NULL);

简而言之,我对来自 kinect 的图像进行分级,将其转换为单通道图像,对其进行平滑处理,运行精明的边缘检测器,反转值,然后进行距离变换。但转换后的图像看起来与输入图像完全相同。怎么了?

谢谢!

I am doing some detection work using OpenCV, and I need to use the distance transform. Except the distance transform function in opencv gives me an image that is exactly the same as the image I use as source. Anyone know what I am doing wrong? Here is the portion of my code:

cvSetData(depthImage, m_rgbWk, depthImage->widthStep);

//gotten openCV image in "depthImage"           

IplImage *single_channel_depthImage = cvCreateImage(cvSize(320, 240), 8, 1);
cvSplit(depthImage, single_channel_depthImage, NULL, NULL, NULL);

//smoothing
IplImage *smoothed_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvSmooth(single_channel_depthImage, smoothed_image, CV_MEDIAN, 9, 9, 0, 0);

//do canny edge detector
IplImage *edges_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvCanny(smoothed_image, edges_image, 100, 200);

//invert values
IplImage *inverted_edges_image = cvCreateImage(cvSize(320, 240), 8, 1);
cvNot(edges_image, inverted_edges_image);

//calculate the distance transform
IplImage *distance_image = cvCreateImage(cvSize(320, 240), IPL_DEPTH_32F, 1);
cvZero(distance_image);

cvDistTransform(inverted_edges_image, distance_image, CV_DIST_L2, CV_DIST_MASK_PRECISE, NULL, NULL);

In a nutshell, I grad the image from the kinect, turn it into a one channel image, smooth it, run the canny edge detector, invert the values, and then I do the distance transform. But the transformed image looks exactly the same as the input image. What's wrong?

Thanks!

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

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

发布评论

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

评论(4

仙女山的月亮 2025-01-03 20:12:43

我相信这里的关键是它们看起来相同。这是我编写的一个小程序,用于显示差异:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat before = imread("qrcode.png", 0);

    Mat dist;
    distanceTransform(before, dist, CV_DIST_L2, 3);

    imshow("before", before);
    imshow("non-normalized", dist);

    normalize(dist, dist, 0.0, 1.0, NORM_MINMAX);
    imshow("normalized", dist);
    waitKey();
    return 0;
}

在非标准化图像中,您会看到以下内容:
在此处输入图像描述

看起来并没有真正改变任何内容,但与整体相比,距离步长非常小值范围 [0, 255](由于 imshow 将图像从 32 位浮点数转换为 8 位进行显示),我们看不到差异,所以让我们对其进行标准化...

现在我们得到这个:
在此处输入图像描述

值本身应该是正确的,但在显示时,您需要对图像进行标准化才能看到差异。

编辑:
这是来自 dist 矩阵左上角的一个 10x10 小样本,表明这些值实际上是不同的:

[10.954346, 10.540054, 10.125763, 9.7114716, 9.2971802, 8.8828888, 8.4685974, 8.054306, 7.6400146, 7.6400146;
  10.540054, 9.5850525, 9.1707611, 8.7564697, 8.3421783, 7.927887, 7.5135956, 7.0993042, 6.6850128, 6.6850128;
  10.125763, 9.1707611, 8.2157593, 7.8014679, 7.3871765, 6.9728851, 6.5585938, 6.1443024, 5.730011, 5.730011;
  9.7114716, 8.7564697, 7.8014679, 6.8464661, 6.4321747, 6.0178833, 5.6035919, 5.1893005, 4.7750092, 4.7750092;
  9.2971802, 8.3421783, 7.3871765, 6.4321747, 5.4771729, 5.0628815, 4.6485901, 4.2342987, 3.8200073, 3.8200073;
  8.8828888, 7.927887, 6.9728851, 6.0178833, 5.0628815, 4.1078796, 3.6935883, 3.2792969, 2.8650055, 2.8650055;
  8.4685974, 7.5135956, 6.5585938, 5.6035919, 4.6485901, 3.6935883, 2.7385864, 2.324295, 1.9100037, 1.9100037;
  8.054306, 7.0993042, 6.1443024, 5.1893005, 4.2342987, 3.2792969, 2.324295, 1.3692932, 0.95500183, 0.95500183;
  7.6400146, 6.6850128, 5.730011, 4.7750092, 3.8200073, 2.8650055, 1.9100037, 0.95500183, 0, 0;
  7.6400146, 6.6850128, 5.730011, 4.7750092, 3.8200073, 2.8650055, 1.9100037, 0.95500183, 0, 0]

I believe the key here is that they look the same. Here is a small program I wrote to show the difference:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat before = imread("qrcode.png", 0);

    Mat dist;
    distanceTransform(before, dist, CV_DIST_L2, 3);

    imshow("before", before);
    imshow("non-normalized", dist);

    normalize(dist, dist, 0.0, 1.0, NORM_MINMAX);
    imshow("normalized", dist);
    waitKey();
    return 0;
}

In the non-normalized image, you see this:
enter image description here

which doesn't really look like it changed anything, but the distance steps are very small compared to the overall range of values [0, 255] (due to imshow converting the image from 32-bit float to 8-bits for display), we can't see the differences, so let's normalize it...

Now we get this:
enter image description here

The values themselves should be correct, but when displayed you will need to normalize the image to see the difference.

EDIT :
Here is a small 10x10 sample from the upper-left corner of the dist matrix show that the values are in fact different:

[10.954346, 10.540054, 10.125763, 9.7114716, 9.2971802, 8.8828888, 8.4685974, 8.054306, 7.6400146, 7.6400146;
  10.540054, 9.5850525, 9.1707611, 8.7564697, 8.3421783, 7.927887, 7.5135956, 7.0993042, 6.6850128, 6.6850128;
  10.125763, 9.1707611, 8.2157593, 7.8014679, 7.3871765, 6.9728851, 6.5585938, 6.1443024, 5.730011, 5.730011;
  9.7114716, 8.7564697, 7.8014679, 6.8464661, 6.4321747, 6.0178833, 5.6035919, 5.1893005, 4.7750092, 4.7750092;
  9.2971802, 8.3421783, 7.3871765, 6.4321747, 5.4771729, 5.0628815, 4.6485901, 4.2342987, 3.8200073, 3.8200073;
  8.8828888, 7.927887, 6.9728851, 6.0178833, 5.0628815, 4.1078796, 3.6935883, 3.2792969, 2.8650055, 2.8650055;
  8.4685974, 7.5135956, 6.5585938, 5.6035919, 4.6485901, 3.6935883, 2.7385864, 2.324295, 1.9100037, 1.9100037;
  8.054306, 7.0993042, 6.1443024, 5.1893005, 4.2342987, 3.2792969, 2.324295, 1.3692932, 0.95500183, 0.95500183;
  7.6400146, 6.6850128, 5.730011, 4.7750092, 3.8200073, 2.8650055, 1.9100037, 0.95500183, 0, 0;
  7.6400146, 6.6850128, 5.730011, 4.7750092, 3.8200073, 2.8650055, 1.9100037, 0.95500183, 0, 0]
权谋诡计 2025-01-03 20:12:43

我刚刚想出了这个。
OpenCV distanceTransform

计算每个像素到最近零像素的距离
源图像。

所以它期望你的边缘图像是负的。

您需要做的就是否定边缘图像:

edges = 255 - edges;

I just figured this one out.
The OpenCV distanceTransform

Calculates the distance to the closest zero pixel for each pixel of
the source image.

and so it expects your edges image to be negative.

All you need to do is to negate your edges image:

edges = 255 - edges;
云淡月浅 2025-01-03 20:12:43

您可以在标准化函数之前使用以下代码打印该值:

for(int x=0; x<10;x++)
  { 
     cout<<endl;
     for(int y=0; y<10;y++)
         cout<<std::setw(10)<<dist.at<float>(x, y);
  }

You can print this values using this code before normalize function:

for(int x=0; x<10;x++)
  { 
     cout<<endl;
     for(int y=0; y<10;y++)
         cout<<std::setw(10)<<dist.at<float>(x, y);
  }
夜雨飘雪 2025-01-03 20:12:43

Mat 格式

  • 输入:CV_8U
  • Dist:CV_32F
  • 归一化:CV_8U

normalize(Mat_dist, Mat_norm, 0, 255, NORM_MINMAX, CV_8U);

如果要可视化结果,则需要将归一化缩放为 0 。 .. 255 而不是 0 ... 1,否则一切都会显得黑色。在缩放为 0 ... 1 的图像上使用 imshow(); 可以工作,但可能会导致后续处理步骤出现问题。至少对我来说是这样。

Mat formats

  • Input: CV_8U
  • Dist: CV_32F
  • Normalized: CV_8U

normalize(Mat_dist, Mat_norm, 0, 255, NORM_MINMAX, CV_8U);

If you want to visualize the result, you need to scale the normalization to 0 ... 255 and not to 0 ... 1 or everything will seem black. Using imshow(); on a scaled to 0 ... 1 image will work but may cause problmes in the next processing steps. Al least it did in my case.

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