Implement imfilter(matlab) with OpenCV
Hello,
I am trying to port matlab code to c++ with opencv. I could import most source code but I had serious problems with the imfilter function. I was trying to use the filter2d function for c++, but It doesn't get same results between themselves. What is the difference? How I could get the same result? The source code is very simple.....
//src_gray has one channel
//C++ code
filter2D(src_gray, dst,-1, kernel);
//Matlab code
dst = imfilter(src_gray,kernel)
Inputs: src_gray=
11 11 11 11 11
11 11 11 11 11
kernel =
2 2
2 2
output:
Matlab:
ans =
88 88 88 88 44
44 44 44 44 22
C++:
[88 88 88 88 88; 88 88 88 88 88]
My c++ code:
/// Initialize arguments for the filter
Point anchor( -1, -1 );
double delta = 0;
int ddepth = -1;
Mat dst;
float data[2][5] = {{11,11,11,11,11},{11,11,11,11,11}};
float kernel[2][2] = {{2,2},{2,2}};
Mat src = Mat(2, 5, CV_32FC1, &data);
Mat ker = Mat(2, 2, CV_32FC1, &kernel);
filter2D(src, dst, ddepth , ker,anchor);
cout << dst << endl;
MATLAB seems to add some padding, I guess. Try a bigger Matrix and implement imfilter and filter2D on that.