• Status: Solved
• Priority: Medium
• Security: Public
• Views: 801

# normilise atan2 between 0 and pi

i am doing a gradient diffusion to extract the skeleton:

I am computing the gradient orientation: atan2(Dy,Dx). and i want to normilse it between 0 and pi.
how can i normilise it?? i am not getting good results. i tried mod(direction,pi) but i am not sure if it's the right way to do it.

source code + image test1.png the  *.m files are *.txt and the main file is skeletonize.m(txt)

thank you!
show.zip
0
• 8
• 3
2 Solutions

Commented:
In what way are your results not good?
could you use atan2(Dy,Dx) for Dy > 0 and atan2(-Dy,-Dx) for Dy < 0
0

Author Commented:
i'll post the images:
the original image is in the zip file. I am extracting the right skeleton of the image. and i can't find the error
res.png
0

Author Commented:
i am NOT extracting*
0

Author Commented:
it's really confusing i double checked the code with the algorithm i still can't find what i am doing wrong.
0

Commented:
Your problem might actually reside in the cmax() function, and might involve issues of operator precedence and left-to-right reading order:
function D= cmax(R,V,B)

if abs(R) > abs(V) && abs(R) > abs(B)
D = R;
elseif abs(V) > abs(R) && abs(V) > abs(B)
D = V;
else
D = B;
end

function D= cmax(R,V,B)

if (abs(R) > abs(V))  && (abs(R) > abs(B))
D = R;
elseif (abs(V) > abs(R)) && (abs(V) > abs(B))
D = V;
else
D = B;
end
0

Author Commented:
hello Mike i modified the code. and i received the same image. in the algorithm they ask that the angle be between [0 and 180]. i don't know if it's this part
i'll post the algorithm picture:
in the SSM:
SS = MAX { abs(Angle(k) - abs( angle(h))}; h ~= k.
where andle(h) and angle(k) are the gradient orientations of the symetric pair of adjacent gradient vectors in a 8-connected neighborhood.
alg.bmp
0

Commented:
Sorry that was not more helpful.
Perhaps if you started with a manufactured "toy" image, then it would be easier to trace the source of the error.  For example, maybe the image could be a simple X-shaped cross.
0

Commented:
Err ... "Sorry that my post was not more helpful"
0

Author Commented:
That's the result for the image Mike. on the crossing it's normal to have the result. but i don't know for the rest.
res1.png
0

Author Commented:
I still can't explain these results.  if it's the direction of how i am computing the regularization and smoothing of the gradient. it seems that there i a problem in the vertical direction of the gradient Dy. hmmm
0

Author Commented:
modification of the angle:
deltap = zeros(size(I));
for i = 1:size(Dy,1)
for j = 1:size(Dy,2)
if Dy(i,j) < 0
deltap(i,j) = atan2(-Dy(i,j),Dx(i,j));
elseif Dy(i,j) > 0
deltap(i,j) = atan2(Dy(i,j),Dx(i,j));
end
end
end
res3.png
0

Author Commented:
interesting suggestions lol let's try python now looooooooooool
0

## Featured Post

• 8
• 3
Tackle projects and never again get stuck behind a technical roadblock.