Gaussian gradient vs derivative
Afficher commentaires plus anciens
I am trying to find the edges of an image using the derivative of a Gaussian.I tried two ways: the one using the gradient and one calculating the derivative but the results look different from each other.
1: Gradient of filter
w=7,sd=3% window,st.dev
f = fspecial('gaussian', [w w], sd);
[Gx,Gy] = gradient(f);
2: Derivative of Gaussian
w=7,sd=3% window,st.dev
[x,y] = meshgrid(-floor(w/2):floor(w/2), -floor(w/2):floor(w/2));
G = exp(-(x.^2+y.^2)/(2*sd^2))/(2*pi*(sd^2));
G_norm=G/sum(G(:));
Gx = -x.*G_norm/(sd^2);
Gy = -y.*G_nrom/(sd^2)
I was expecting Gx and Gy to be the same in the two methods but it is not. I have two questions: 1.Why don't these two methods give the same result? 2.Is there a preferred way from these two (or a completely different one) to create the Gaussian derivative mask for edge detection? Thank you in advance.
Edit: For the first method I find Gx =
0.0058 0.0040 0 -0.0040 -0.0058
0.0068 0.0047 0 -0.0047 -0.0068
0.0072 0.0049 0 -0.0049 -0.0072
0.0068 0.0047 0 -0.0047 -0.0068
0.0058 0.0040 0 -0.0040 -0.0058
For the second method i find Gx =
0.0071 0.0042 0 -0.0042 -0.0071
0.0083 0.0049 0 -0.0049 -0.0083
0.0088 0.0052 0 -0.0052 -0.0088
0.0083 0.0049 0 -0.0049 -0.0083
0.0071 0.0042 0 -0.0042 -0.0071
9 commentaires
Image Analyst
le 6 Juin 2015
What is your image variable? Where does it come into play in this code?
Marcus D.
le 6 Juin 2015
Image Analyst
le 6 Juin 2015
Why not use imgradient()? I'm having trouble following what you're doing. Why not just use imfilter or conv2 with G itself? Why are you multiplying x by G?
Marcus D.
le 6 Juin 2015
Image Analyst
le 6 Juin 2015
Why not use imgradient()?
Or, if you want a DOG filter, see the attached demo.
David Young
le 6 Juin 2015
Alternatively, for a function that computes the x and y components of the gradient rather than magnitude and direction, and which has some additional options, see my fex submission.
Your two ways of getting the derivative of a filter should be roughly equivalent if you have implemented them correctly. If there are small differences, it's because the finite differencing done by gradient is only an approximation to the analytical derivative. If there are large differences, it's because you have made a mistake in the implementation. Can you say what the nature of the differences is?
Marcus D.
le 6 Juin 2015
David Young
le 6 Juin 2015
I think the differences are simply due to approximating the derivative by finite differences in your first method. It will not make a big difference which you use; I guess the second method is a little more accurate. I don't know a better method.
Marcus D.
le 6 Juin 2015
Réponses (0)
Catégories
En savoir plus sur Image Category Classification dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!