What is the difference between gradient and imgradient?

I would like to calculate the gradient of the image I. I have two options that

[Gx, Gy] = gradient(I); g = sqrt(Gx.^2+Gy.^2); 

and

 [g,~] = imgradient(I, 'sobel'); 

My question

  • What is the gradient method in the first embodiment?

  • What does gradient search using the sobel method mean?

Thank you all

Here 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 noise is added to the image, different ones will be clearer.

 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 a visualization, the second option provided a higher brightness value

+5
source share
2 answers

The difference is the Sobel operator. The sobel operator is a matrix that is convoluted with the image to calculate its directional gradients.

The sobel operator is defined as (from wikipedia):

enter image description here

while gradient are just directional differences. You can interpret this as a gradient, performing

 Gx=[ 0 0 0; Gx=[ 0 -1 0; -1 0 1; and 0 0 0; 0 0 0] 0 1 0]; 

This difference is that when someone has an image, it is known that this is a discretization of a continuous region. The Sobel operator helps you keep track of things that happen β€œaround” a given pixel.

+8
source

gradient uses exclusively central differences and imgradient gives you a choice, for example 'central' or default 'sobel' . Using the first imgradient option 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


There are five options available for imgradient :

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

The documentation explains:

The algorithmic approach adopted by imgradient for each of the listed gradient methods is to first calculate directional gradients, Gx and Gy , relative to the x axis and y axis. The x axis is defined along the columns going to the right, and the y axis is defined along the rows going down. Then the gradient value and direction are calculated from their orthogonal components Gx and Gy .

+8
source

Source: https://habr.com/ru/post/1244736/


All Articles