OpenCV中的Matlab梯度等效项 [英] Matlab gradient equivalent in opencv

查看:97
本文介绍了OpenCV中的Matlab梯度等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些代码从Matlab迁移到Opencv,并且需要梯度函数的精确副本.我已经尝试了cv :: Sobel函数,但是由于某种原因,生成的cv :: Mat中的值与Matlab版本中的值不同.我需要将X和Y梯度放在单独的矩阵中以进行进一步的计算.

I am trying to migrate some code from Matlab to Opencv and need an exact replica of the gradient function. I have tried the cv::Sobel function but for some reason the values in the resulting cv::Mat are not the same as the values in the Matlab version. I need the X and Y gradient in separate matrices for further calculations.

任何可以实现此目标的解决方法都很好

Any workaround that could achieve this would be great

推荐答案

Sobel只能计算图像像素的二阶导数,这不是我们想要的.

Sobel can only compute the second derivative of the image pixel which is not what we want.

(f(i + 1,j)+ f(i-1,j)-2f(i,j))/2

(f(i+1,j) + f(i-1,j) - 2f(i,j)) / 2

我们想要的是

(f(i + i,j)-f(i-1,j))/2

(f(i+i,j)-f(i-1,j)) / 2

所以我们需要申请

Mat kernelx = (Mat_<float>(1,3)<<-0.5, 0, 0.5);
Mat kernely = (Mat_<float>(3,1)<<-0.5, 0, 0.5);
filter2D(src, fx, -1, kernelx)
filter2D(src, fy, -1, kernely);

Matlab将边框像素与内部像素区别对待.因此,上面的代码在边框值处是错误的.可以使用BORDER_CONSTANT将边界值扩展为一个常数,不幸的是,OpenCV将该常数设为-1,并且不能将其更改为0(这就是我们想要的).

Matlab treats border pixels differently from inner pixels. So the code above is wrong at the border values. One can use BORDER_CONSTANT to extent the border value out with a constant number, unfortunately the constant number is -1 by OpenCV and can not be changed to 0 (which is what we want).

关于边界值,我对此没有一个很好的答案.只需尝试手动计算一阶导数...

So as to border values, I do not have a very neat answer to it. Just try to compute the first derivative by hand...

这篇关于OpenCV中的Matlab梯度等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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