Search code examples
matlabimage-processinggradientlaplacian

How to calculate laplacian form gradient?


I am dealing with shadow removal. First I have to calculate gradient from the image in both directions. g(x,y) = -2*f(x,y) + f(x+1,y) + f(x,y+1). Afterwards, I do some calculation on the gradient and modify it a bit.

The problem comes when I have to calculate Laplacian (second derivative) from the modified gradient.

I know that the Laplacian filter has a matrix: [0,1,0: 1,-4,1; 0,1,0]. But the questions is how to calculate the Laplacian from the gradient, if the cells are already modified?

Calculation of gradient:

[Gx,Gy] = imgradientxy(img_G,'intermediate');
greenGradient = Gx + Gy;

Thanks!


Solution

  • When calculating gradient using imgradientxy(I,'intermediate'):

    GX(j, i)  = I(j, i+1) - I(j, i)
    GY(j, i)  = I(j+1, i) - I(j, i)
    

    And the Laplacian:

    L(j, i)   = I(j, i-1) + I(j, i+1) + I(j-1, i) + I(j+1, i) - 4*I(j, i)
    

    Now if we calculate gradient of GX and GY:

    GGX(j, i) = GX(j, i) - GX(j, i-1)
              = I(j, i+1) - I(j, i) - I(j, i) + I(j, i-1)
              = I(j, i-1) + I(j, i+1) - 2*I(j, i)
    GGY(j, i) = I(j-1, i) + I(j+1, i) - 2*I(j, i)
    

    So

    L(j, i) = GGX(j, i) + GGY(j, i)
    

    Note that there is on pixel offset between methods used to find gradient of I and gradient of GX and GY.

    I =   im2double(imread('coins.png'));
    [GX, GY] = imgradientxy(I,'intermediate');
    L =   imfilter(I, [0 1 0; 1 -4 1; 0 1 0], 'replicate');
    GGX = imfilter(GX, [0 0 0; -1 1 0; 0 0 0], 'replicate');
    GGY = imfilter(GY, [0 -1 0; 0 1 0; 0 0 0], 'replicate');
    L2 =  GGX+GGY;
    E =   (L2-L).^2;
    

    enter image description here