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

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

As a visualization, the second option provided a higher brightness value
source share