Matlab的filter2在OpenCV中的等效功能 [英] Equivalent function of Matlab's filter2 in OpenCV

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

问题描述

我需要将以下matlab代码转换为OpenCV,并获得完全相同的结果.

I need to convert the following matlab code into OpenCV and obtain exactly the same result.

在matlab中:

A = [1 2 3];
f = [4 5 6];
result = filter2(f, A);

结果如下:

result = [17    32    23]

在OpenCV中,我尝试了以下几行:

In OpenCV, I tried these lines:

cv::Mat A = (cv::Mat_<float>(1, 3) << 1, 2, 3);
cv::Mat f = (cv::Mat_<float>(1, 3) << 4, 5, 6);
cv::Mat result;
cv::filter2D(A, result, -1, f, cv::Point(-1, -1), 0, cv::BORDER_REPLICATE);

这给了我

result = [21 32 41]

如何获得与Matlab相同的结果?我怀疑OpenCV中的锚点会导致这种差异,但是我不知道如何更改它.提前致谢.

How can I obtain the same result as of Matlab?? I doubt the anchor point in OpenCV causes this difference, but I cannot figure out how to change it. Thanks in advance.

推荐答案

使用cv::BORDER_CONSTANT,它将数组填充为零,而不是复制相邻元素:

Use cv::BORDER_CONSTANT, which pads the array with zero rather than duplicating the neighboring element:

cv::filter2D(A, result, -1, f, cv::Point(-1, -1), 0, cv::BORDER_CONSTANT);

结果是:

result = [17, 32, 23]

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

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