Matlab的Conv2等价于OpenCV [英] Matlab's Conv2 equivalent in OpenCV
问题描述
我一直在尝试使用OpenCV进行2D矩阵的卷积。我实际上通过了此代码 http:// blog.timmlinder.com/2011/07/opencv-equivalent-to-matlabs-conv2-function/#respond ,但它只在肯定的情况下产生正确的答案。在Matlab中有一个简单的函数如conv2在OpenCV或C ++?
I have been trying to do Convolution of a 2D Matrix using OpenCV. I actually went through this code http://blog.timmlinder.com/2011/07/opencv-equivalent-to-matlabs-conv2-function/#respond but it yields correct answers only in positive cases. Is there a simple function like conv2 in Matlab for OpenCV or C++?
以下是一个示例:
A= [
1 -2
3 4
]
[ - 0.707 0.707]
由MATLAB的conv2产生的结果是
And the result as by conv2 from Matlab is
-0.7071 2.1213 -1.4142
-2.1213 -0.7071 2.8284
有些函数在OpenCV或C ++中计算此输出?
Some function to compute this output in OpenCV or C++? I will be grateful for a response.
推荐答案
如果您想要一个独占的OpenCV解决方案,请使用 cv2.filter2D <强>功能。但是如果你想得到和matlab一样的正确输出,你应该调整borderType标志。
If you want an exclusive OpenCV solution, use cv2.filter2D function. But you should adjust the borderType flag if you want to get the correct output as that of matlab.
>>> A = np.array([ [1,-2],[3,4] ]).astype('float32')
>>> A
array([[ 1., -2.],
[ 3., 4.]], dtype=float32)
>>> B = np.array([[ 0.707,-0.707]])
>>> B
array([[ 0.707, -0.707]])
>>> cv2.filter2D(A2,-1,B,borderType = cv2.BORDER_CONSTANT)
array([[-0.70700002, 2.12100005, -1.41400003],
[-2.12100005, -0.70700002, 2.82800007]], dtype=float32)
borderType很重要。要找到卷积,您需要在数组外部的值。如果你想得到matlab的输出,你需要传递cv2.BORDER_CONSTANT。请参阅输出大小大于输入。
borderType is important. To find the convolution you need values outside the array. If you want to get matlab like output, you need to pass cv2.BORDER_CONSTANT. See output is greater in size than input.
这篇关于Matlab的Conv2等价于OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!