Laplace matlab opencv differences

Hello I wanted to implement the imfilter with the Laplace option (Sobel option worked perfectly) but I it gives a totally different result. While in the Sobel option both filter2d and the Sobel itself gives me the same result in case of of Laplace the results are different from Matlab For the Laplace implementation I used:

Mat LAP = (Mat_<double>(3,3) << 1/6, 2/3, 1/6, 2/3, -10/3, 2/3, 1/6, 2/3, 1/6);


And then used the 2D filter:

filter2D(red1,LAP_CONV,CV_64F,LAP);


And the results are totally different

Moreover when I use

Laplacian(red1,LAP_CONV,CV_64F);


Only on my ROI the results are pretty much the same as if I used Matlab on the whole image And yes I know that the matrixes for Laplace in OPENCV and Matlab are different

The result was checked by std squared

edit retag close merge delete

take a look at the filter2D docs again (convolution vs. correlation)

( 2018-10-18 03:32:51 -0500 )edit

Yes I need convolution its default option as my imfilter function is:

ILAP = imfilter(lm, h, 'replicate', 'conv');


Where I took the h from

h = fspecial('laplacian');

( 2018-10-18 03:51:47 -0500 )edit

so you need to flip(kernel, -1)

( 2018-10-18 04:34:12 -0500 )edit

Its symmetrical (and try it anyway same answer :) )

( 2018-10-18 04:42:04 -0500 )edit

oh, right missed it ;)

( 2018-10-18 04:45:11 -0500 )edit

Sort by ยป oldest newest most voted

well you already know, that opencv != matlab, but they're using a different kernel than yours:

    float K[2][9] =
{
{ 0, 1, 0, 1, -4, 1, 0, 1, 0 },
{ 2, 0, 2, 0, -8, 0, 2, 0, 2 }
};
Mat kernel(3, 3, CV_32F, K[ksize == 3]);
if( scale != 1 )
kernel *= scale;

filter2D( _src, _dst, ddepth, kernel, Point(-1, -1), delta, borderType );

more

I took my matrix from Matlab:

fspacial('laplacian');

alpha = p2;
alpha = max(0,min(alpha,1));
h1    = alpha/(alpha+1); h2 = (1-alpha)/(alpha+1);
h     = [h1 h2 h1;h2 -4/(alpha+1) h2;h1 h2 h1];


Where p2 by default equals 0.2

( 2018-10-18 07:53:57 -0500 )edit