Reputation: 3854
I would like to compute gradient of image I. I have two options that are
[Gx, Gy] = gradient(I);
g = sqrt(Gx.^2+Gy.^2);
and
[g,~] = imgradient(I, 'sobel');
My question are
What is gradient method using in first option?
What is befinet of finding gradient using sobel method ?
Thank all
This is what I tried
I=zeros([128 128]);
I(50:100,50:100)=100;
[Gx, Gy] = gradient(I);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I, 'sobel');
subplot(121);imshow(g1,[]);title('First option')
subplot(122);imshow(g2,[]);title('Second option')
When noisy is added to image, the different will be more clear
I=zeros([128 128]);
I(50:100,50:100)=100;
%% Add noise image;
noiseStd=5;
I_noise = I + (noiseStd * randn([128, 128]));
[Gx, Gy] = gradient(I_noise);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I_noise, 'sobel');
subplot(121);imshow(g1,[]);title('First option')
subplot(122);imshow(g2,[]);title('Second option')
As visualization, the second option provided more brightness value
Upvotes: 6
Views: 1496
Reputation: 25232
gradient
exclusively uses central differences and imgradient
gives you a choice, e.g. 'central'
as well or the default 'sobel'
. Using the first option imgradient
looks the same as gradient
:
I=zeros([128 128]);
I(50:100,50:100)=100;
%% Add noise image;
noiseStd=5;
I_noise = I + (noiseStd * randn([128, 128]));
[Gx, Gy] = gradient(I_noise);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I_noise, 'sobel');
[g3,~] = imgradient(I_noise, 'central');
subplot(131);imshow(g1,[]);title('gradient')
subplot(132);imshow(g2,[]);title('imgradient sobel')
subplot(133);imshow(g3,[]);title('imgradient central')
For imgradient
there are five options available:
While the documentation explaines:
The algorithmic approach taken in
imgradient
for each of the listed gradient methods is to first compute directional gradients,Gx
andGy
, with respect to the x-axis and y-axis. The x-axis is defined along the columns going right and the y-axis is defined along the rows going down. The gradient magnitude and direction are then computed from their orthogonal componentsGx
andGy
.
Upvotes: 8
Reputation: 35525
The difference is the 'Sobel' operator indeed. The sobel operator is a matrix that is convoluted with the image to compute its directional gradients.
The sobel operator is defined as (from wikipedia):
while what gradient
does is just directional differences. You can interpret this as gradient doing
Gx=[ 0 0 0; Gx=[ 0 -1 0;
-1 0 1; and 0 0 0;
0 0 0] 0 1 0];
This difference is because when one has an image, it is know that it is a discretization of a continuous domain. The Sobel operator helps to take into account things that happen "around" the given pixel.
Upvotes: 8