是opencv中的任何函数,相当于matlab conv2 [英] is There any function in opencv which is equivalent to matlab conv2

查看:245
本文介绍了是opencv中的任何函数,相当于matlab conv2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matlab函数conv2有没有直接的opencv函数?我尝试使用cvFilter2D(),但似乎给我的结果与conv2()不同。

Is there any direct opencv function for matlab function conv2? I tried using cvFilter2D(), but it seems to be giving me different results than conv2().

例如:

CvMat * Aa = cvCreateMat(2, 2, CV_32FC1);
CvMat * Bb = cvCreateMat(2, 2, CV_32FC1);
CvMat * Cc = cvCreateMat(2, 2, CV_32FC1);
cvSetReal2D(Aa, 0, 0, 1);
cvSetReal2D(Aa, 0, 1, 2);
cvSetReal2D(Aa, 1, 0, 3);
cvSetReal2D(Aa, 1, 1, 4);
cvSetReal2D(Bb, 0, 0, 5);
cvSetReal2D(Bb, 0, 1, 5);
cvSetReal2D(Bb, 1, 0, 5);
cvSetReal2D(Bb, 1, 1, 5);
cvFilter2D(Aa, Cc, Bb);

This produces the matrix [20 30; 40 50]


In MATLAB:
>> A=[1 2; 3 4]
A =
1 2
3 4

>> B=[5 5; 5 5]
B =
5 5
5 5

>> conv2(A,B,'shape')
ans =
50 30
35 20

请帮帮我。对我非常有用。谢谢。

Please Help me.its very much useful for me.Thank you.

问候

Arangarajan。

Arangarajan.

推荐答案

如果使用卷积,矩阵边缘会出现问题。卷积掩模需要在矩阵之外的值。 OpenCV和matlab的算法使用不同的策略来解决这个问题。 OpenCV只复制边框的像素,而matlab只假设所有这些像素都为零。

If you are using convolution, there is problem at the edge of the matrix. The convolution mask needs values which are outside of the matrix. The algorithms from OpenCV and matlab use different strategies to cope with this problem. OpenCV just replicates the pixels of the border whereas matlab just assumes that all this pixels are zero.

因此,如果你想在OpenCV中模拟matlab的行为,你可以添加这个零填充手动。甚至还有专门的功能。让我举个例子说明如何修改代码:

So if you want to emulate the behaviour of matlab in OpenCV you can add this zero padding manually. There even is a dedicated function for this. Let me give you an example of how your code could be modified:

CvMat * Ccb = cvCreateMat(3, 3, CV_32FC1);
CvMat * Aab = cvCreateMat(3, 3, CV_32FC1);
cvCopyMakeBorder(Aa,Aab, cvPoint(0,0),IPL_BORDER_CONSTANT, cvScalarAll(0));
cvFilter2D(Aab, Ccb, Bb);

结果如下:

20.000   30.000   20.000 
40.000   50.000   30.000 
30.000   35.000   20.000 

要获得预期结果,您只需删除第一列和第一行即可删除我们添加的边框引入的其他数据。

To get your intended result you just need to delete the first column and row to get rid of the additional data introduced by the border we added.

这篇关于是opencv中的任何函数,相当于matlab conv2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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