Ask Your Question
0

Laplace matlab opencv differences

asked 2018-10-18 03:29:00 -0500

ilia gravatar image

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

Thanks in advanced for all your help

edit retag flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 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');
ilia gravatar imageilia ( 2018-10-18 03:51:47 -0500 )edit

so you need to flip(kernel, -1)

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

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

ilia gravatar imageilia ( 2018-10-18 04:42:04 -0500 )edit

oh, right missed it ;)

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

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-18 07:31:46 -0500

berak gravatar image

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 );
edit flag offensive delete link more

Comments

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

ilia gravatar imageilia ( 2018-10-18 07:53:57 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-10-18 03:29:00 -0500

Seen: 72 times

Last updated: Oct 18 '18