image processing - How do I interpret the orientation of the gradient when using imgradient in MATLAB? -
i finding gradient of image. using 5 x 5 image. more interested in finding direction of gradient not getting results manually on paper them using matlab function imgradient
. please refer following images know more input images , sobel filter used here find gradient of image. 1 of 3 x 3 sobel operator used here 1 using function
f1 = fspecial('sobel');
and other 1 obtained transposing f1
.
please note trying find direction of 1 pixel here rounded red color. here in first 2 cases result matches obtain using imgradient
function in third case imgradient
gives -135 degree whereas getting -45. please me find error.
also please explain how interpret following gradient directions shown in follwing image.
your calculations correct highly recommended don't use atan(y/x)
definition because calculation not cognizant of quadrant angle of gradient resides in. doing atan(y/x)
components falsely report angle -45 degrees when isn't correct. should use atan2
instead.
now internals of imgradient
quite straight forward. i'd point out angle reported imgradient
assuming y
coordinate increasing bottom top. in addition, imgradient
should report angle of orientation pointing greatest rate of change. in case of images, points in direction progress dark pixels light pixels.
first call imgradientxy
called , call fspecial('sobel')
made if provide sobel
flag imgradient
. in fact, portion of imgradientxy
important remember (starting @ line 75: matlab r2015a):
case 'sobel' h = -fspecial('sobel'); %// align mask correctly along x- , y- axes gx = imfilter(i,h','replicate'); %' if nargout > 1 gy = imfilter(i,h,'replicate'); end
notice negative of output of fspecial
performed comment provided @ line. ensure mask detect horizontal edges (i.e. gy
) y-down (as commonly known in computer graphics). specifically, origin of image @ top-left corner , not bottom left.
this pictorial representation of how coordinate system laid out in y
-down:
source: wikipedia - rotation matrix
as such, when finding orientation there additional requirement ensure angle of orientation of gradient respect y-up coordinate system we're used to. therefore when finding angle of orientation of gradient, need negate y
coordinate before calculating angle angle respect standard convention instead.
pursuing definition of gradient seek conventional system of y
coordinate increasing bottom top. negation required , in fact if examine source code imgradient
, precisely being done @ line 127 of code (version r2015a):
gdir = atan2(-gy,gx)*180/pi; %// radians degrees
you may asking why there need negate mask , again negate y
coordinate after find orientation. reason why because modified mask required capture magnitude of gradient , negate mask once , find gradient magnitude , negate y
coordinate can find angle respect conventional coordinate system.
in case, given gx = 765
, gy = -765
, substituting these quantities above equation yields:
>> gy = 765; >> gx = -765; >> gdir = atan2(-gy,gx)*180/pi gdir = -135
this makes sense because gradient direction corresponds direction towards greatest rate of change. -135 degrees means we're pointing south west make sense progressing dark pixels light pixels.
now if consult third example image, angles reported imgradient
indeed correct. draw line dark area light area , see angle makes x
axis aligned columns increasing towards right. first angle of +90 degrees makes sense moving bottom top follow dark area , light. similar situation situation image reversed. third situation have seen before , fourth situation third situation rotated 180 degrees , naturally angle of orientation dark light +45 degrees opposed -135 degrees previously.
Comments
Post a Comment