Jame
Jame

Reputation: 3854

What is the difference between gradient and imgradient?

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

  1. What is gradient method using in first option?

  2. 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')

enter image description here

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')

enter image description here

As visualization, the second option provided more brightness value

Upvotes: 6

Views: 1496

Answers (2)

Robert Seifert
Robert Seifert

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')

enter image description here


For imgradient there are five options available:

  • 'sobel' Sobel gradient operator (default)
  • 'prewitt' Prewitt gradient operator
  • 'central' Central difference gradient: dI/dx = (I(x+1) - I(x-1))/2
  • 'intermediate'Intermediate difference gradient: dI/dx = I(x+1) - I(x)
  • ‘roberts' Roberts gradient operator

While the documentation explaines:

The algorithmic approach taken in imgradient for each of the listed gradient methods is to first compute directional gradients, Gx and Gy, 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 components Gx and Gy.

Upvotes: 8

Ander Biguri
Ander Biguri

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):

enter image description here

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

Related Questions