问题
can anyone explain why I get such dramatically different results for the Laplace operator in Matlab when I use
laplacian = del2(image);
versus
[x, y] = gradient(image);
[xx, xy] = gradient(x);
[yx, yy] = gradient(y);
laplacian = xx + yy;
Shouldn't these come to the same thing? They get particularly divergent when one includes a dx term.
Putting my example up here in case it helps: I have a test field consisting of
[5; 2.5+2.5i; 5i; -2.5+2.5i; -5; -2.5-2.5i; -5i; 2.5-2.5i]
times its transpose (I can post the whole matrix if it helps). The inner block (3:6, 3:6) of the del2() of this field is:
[-2.5 -0.625-0.625i -2.5i 0.625-0.625i ;
-0.625+0.625i 0 -0.625+0.625i 0 ;
2.5i -0.625+0.625i -2.5 -0.625+0.625i ;
0.625+0.625i 0 -0.625+0.625i 0 ]
while the inner block (3:6, 3:6) of the xx + yy is:
[-5 -2.5-2.5i -5i -2.5-2.5i ;
-2.5+2.5i -2.5 -2.5-2.5i -2.5i ;
5i -2.5+2.5i -5 -2.5-2.5i ;
2.5+2.5i 2.5i -2.5+2.5i -2.5 ]
which as you can see will make a dramatic difference in any further equations. Might anyone have an explanation, thanks very much!
回答1:
If you look closely at Matlab's documentation, the the laplacian of f at (x,y), del2(f(x,y)) is computed using only (x,y) and its nearest neighbours: x+1, x-1, y+1, y-1.
The same goes for the gradient function (and the divergence, which explicitly uses the gradient function). Computing the gradient twice involves the nearest neighbours of the nearest neighbours. Therefore div(grad(f(x,y)) is actually computed using (x,y) and x+2, x-2, y+2, y-2. Hence the difference.
The greater the grid spacing, the greater the discrepancy between these two calculations will be.
回答2:
As you can see on the documentation of del2, it differs a factor of 1/4
with the gradient method you compared it with.
This partly explains that factor 4 in your example. I blame the rest on edge effects :p
来源:https://stackoverflow.com/questions/12353087/discrepancy-between-matlab-del2-and-matlab-gradient-of-gradient