Calculate the image gradient

I use the function imgradientxy to get the Gx and Gy firstly. Then obtain the direction of the gradient.
some codes like:
% code
b = zeros(100);
b(1:4:end,:) = 1;
[Gx, Gy] = imgradientxy(b);
theta = atan(Gy./Gx);
The image only has some horizontal lines. So The Gx is all zeros and the theta have a lot NAN. What should I do to get the gradient for this image and for other?

2 comentarios

naser telesi
naser telesi el 1 de Mzo. de 2017
if Gx is zero then let Gx = 1 to avoid divide by zero problem
Matt J
Matt J el 1 de Mzo. de 2017
if Gx is zero then let Gx = 1 to avoid divide by zero problem
But theta will be wrong...

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 23 de Sept. de 2013

0 votos

Use atan2 instead
theta=atan2(Gy,Gx);

2 comentarios

Donghui  Sun
Donghui Sun el 23 de Sept. de 2013
A big thanks.
Image Analyst
Image Analyst el 23 de Sept. de 2013
Like I said, imgradient() already gives that to you as Gdir - see the help. No need to do it yourself with atan2.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 23 de Sept. de 2013

1 voto

Why do it yourself? Why not just use imgradient()?

1 comentario

Donghui  Sun
Donghui Sun el 23 de Sept. de 2013
Haha, I just want to try another method. In addition, the gradient direction in imgradient() contains angles in degrees within the range [-180 180]. I need the angle in radians.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 23 de Sept. de 2013

Comentada:

el 1 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by