Gaussian gradient vs derivative

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
Image Analyst le 6 Juin 2015
What is your image variable? Where does it come into play in this code?
Marcus D.
Marcus D. le 6 Juin 2015
my image is a grayscale image and I will use Gx and Gy like this: Cx=imfilter(double(Image), Gx, 'conv', 'replicate') and Cy=imfilter(double(Image), Gy, 'conv', 'replicate') and then use Cx^2+Cy^2.
Image Analyst
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.
Marcus D. le 6 Juin 2015
In method (2) Gx and Gy are the derivative. If you take the derivative for the G as I have written it then dG/dx=-x.*G_norm/(sd^2). I thought the two ways calculating the derivative (one with gradient and the other with taking the derivative of the formula) would be the same.
Image Analyst
Image Analyst le 6 Juin 2015
Why not use imgradient()?
Or, if you want a DOG filter, see the attached demo.
David Young
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.
Marcus D. le 6 Juin 2015
Thank you for the comments and the code submission,it is very useful. I edited my question to show the differences between the two methods. The reason I want this method is so i can use it to measure the focus of an image too so I want to create the mask for the Gaussian derivative. I assumed that these two ways would be the same that is why i could not understand why they are different. Would you say one of the two ways is preferable for the calculation of the focus of an image?
David Young
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.
Marcus D. le 6 Juin 2015
Thank you!

Connectez-vous pour commenter.

Réponses (0)

Question posée :

le 5 Juin 2015

Commenté :

le 6 Juin 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by