filter2d vs conv2

asked 2018-10-13 05:49:45 -0600

tinahom gravatar image

updated 2018-10-13 08:51:12 -0600

The result I get from conversion of conv2 function in matlab is very different from the result I get in opencv using filter2d function. In opencv in the position of (0,0) I get 1.1175871e-08 while the result I get in matlab is -0.9639

In Matlab:

A = [1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6]; 
k = [-0.014, -0.45, 0, 0.45, 0.014];
conv2(A, k, 'same');

In opencv/c++:

cv::Mat dst; 
float K[5] = {-0.014, -0.45, 0, 0.45, 0.014}; 
cv::Mat kernel(1, 5, CV_32F, K);
Mat kernel-p(1,5, CV_32F);
cv::flip(kernel, kernel-p, -1);

float test[6][6] = {{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6}};
cv::Mat myMat(3, 4, CV_32FC1, &test);

filter2D(myMat, dst, -1 , kernel , Point( -1, -1 ), 0, BORDER_DEFAULT);

What am I doing wrong in the opencv code?

edit retag flag offensive close merge delete

Comments

obvious failure to read docs .

also, just as a hint, since your input Mat is smaller than the kernel, you have to deal with border modes here !

all applied correctly, i get -0.94199997 from opencv

(and noone even knows, if your matlab results are correct !)

berak gravatar imageberak ( 2018-10-13 06:23:20 -0600 )edit

@berak Yes, the matlab code is correct. I increased the size of the Mat to make it bigger than the kernel but I still do not get the correct result. The numbers are all "e" number. How did you get the correct result but not me? Did not you modify the filter2d function?

tinahom gravatar imagetinahom ( 2018-10-13 07:10:46 -0600 )edit

no, i won't tell you, left as an exercise ;)

berak gravatar imageberak ( 2018-10-13 07:13:06 -0600 )edit

I do not want to flip the kernel as it says in the link. I want the kernel to be applied horizontally.

tinahom gravatar imagetinahom ( 2018-10-13 07:28:24 -0600 )edit

you still have to flip it horizontally (and flipping it vertically is a no-op, think of it ;)

what is all of it meant to do, in the end ? this clearly lacks any context !

berak gravatar imageberak ( 2018-10-13 07:43:02 -0600 )edit
1

I fliped the kernel. Now I do get better result. But in column 0 and 5 I still get e number.

tinahom gravatar imagetinahom ( 2018-10-13 08:50:02 -0600 )edit

ok. (well done !) now fix the border mode

berak gravatar imageberak ( 2018-10-13 08:50:54 -0600 )edit

[-7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09; -7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09; -7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09; -7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09; -7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09; -7.4505806e-09, -0.92799991, -0.95599997, -0.95600009, -0.92799979, -7.4505806e-09]

tinahom gravatar imagetinahom ( 2018-10-13 08:57:04 -0600 )edit

I do not understand the problem with the boarder. Everywhere says BORDER_DEFAULT should work. If Not, do I need to use borderInterpolate() function?

tinahom gravatar imagetinahom ( 2018-10-13 09:34:14 -0600 )edit

try with constant.

berak gravatar imageberak ( 2018-10-13 09:42:25 -0600 )edit