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
dadadudeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
dadadudeAuthor 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
dadadudeAuthor Commented:
i am NOT extracting*
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

dadadudeAuthor Commented:
it's really confusing i double checked the code with the algorithm i still can't find what i am doing wrong.
0
MikeAThonCommented:
Your problem might actually reside in the cmax() function, and might involve issues of operator precedence and left-to-right reading order:
Your code:
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
 
Try this instead, with added parentheses:
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
dadadudeAuthor 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
MikeAThonCommented:
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
MikeAThonCommented:
Err ... "Sorry that my post was not more helpful"
0
dadadudeAuthor 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
dadadudeAuthor 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
dadadudeAuthor 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
dadadudeAuthor Commented:
interesting suggestions lol let's try python now looooooooooool
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.