Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Short answer : Matlab probably isn't doing what you want. If you look at the documentation for imfilter, it defaults to using a 0-padded border which is nonsense in most scenarios. You can specify the border type to filter2D in OpenCV by passing the last argument, borderType. BORDER_DEFAULT is BORDER_WRAP_101 (which you can read about in the documentation)

Long answer is you do this:

Point anchor( 0 ,0 );
double delta = 0;
int ddepth = -1;

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);
Mat dst = Mat(src.size(), src.type());

Ptr<FilterEngine> fe =  createLinearFilter(src.type(), ker.type(), ker, anchor,
        delta, BORDER_CONSTANT, BORDER_CONSTANT, Scalar(0));
fe->apply(src, dst);
cout << dst << endl;

The problem is you can pass BORDER_CONSTANT to filter2D, but it doesnt take the value the constant should be, So instead you have to create a FilterEngine object. Additionally, Matlab anchors its kernel in the top-left corner, unlike the OpenCV's default, in the center

click to hide/show revision 2
removed unused variable

Short answer : Matlab probably isn't doing what you want. If you look at the documentation for imfilter, it defaults to using a 0-padded border which is nonsense in most scenarios. You can specify the border type to filter2D in OpenCV by passing the last argument, borderType. BORDER_DEFAULT is BORDER_WRAP_101 (which you can read about in the documentation)

Long answer is you do this:

Point anchor( 0 ,0 );
double delta = 0;
int ddepth = -1;

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);
Mat dst = Mat(src.size(), src.type());

Ptr<FilterEngine> fe =  createLinearFilter(src.type(), ker.type(), ker, anchor,
        delta, BORDER_CONSTANT, BORDER_CONSTANT, Scalar(0));
fe->apply(src, dst);
cout << dst << endl;

The problem is you can pass BORDER_CONSTANT to filter2D, but it doesnt take the value the constant should be, So instead you have to create a FilterEngine object. Additionally, Matlab anchors its kernel in the top-left corner, unlike the OpenCV's default, in the center