# filter2d vs conv2

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?

obvious failure to read docs .

also, just as a hint, since your input Mat is

smallerthan 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 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?

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

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

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 !

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

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

[-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]

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

try with constant.