Orientation

256 views
Skip to first unread message

Amir Asgharsharghi

unread,
Feb 5, 2016, 4:33:25 AM2/5/16
to Bonsai Users
Dear All,

It occurred to me that in "Large Binary Region" the source orientation value is varying from -pi/2 : pi/2 and ignoring the direction of the object. is there any way to include the direction, meaning having values -pi : pi or 0 : pi ?
I am using this for tracking and finding orientation of swimming zebrafish.

Thank you,
Amir

goncaloclopes

unread,
Feb 5, 2016, 7:40:53 PM2/5/16
to Bonsai Users
Hi Amir and welcome to the forums!

You can compute the full orientation by considering the "head" to be the direction with more pixels (i.e. more mass).

At the end of the thread you can find the code for a PythonTransform that computes the full angle.

I hope this helps!
Best,
Gonçalo

Amir Asgharsharghi

unread,
Feb 8, 2016, 7:36:38 AM2/8/16
to Bonsai Users
Hi Gonçalo,

Thanks a lot for the answer.I did try the python code, it works good but some times I get wrong angle. 
Fish turns from -50 degree toward -/+180 degree ( changes direction in couple of frames) while the measured angle is -50 but then 0 !
do you have any further suggestion? 
as you mentioned maybe trying the arrow shape solution?

Thanks,
Amir

goncaloclopes

unread,
Feb 8, 2016, 7:16:48 PM2/8/16
to Bonsai Users
I've converted Adam's answer from the previous thread to Python and it seems to work well. Try this script inside a PythonTransform and let me know how it works (the output of this is still a ConnectedComponent:

import clr
import math
clr
.AddReference("Bonsai.Vision")
from Bonsai.Vision import ConnectedComponent


@returns(ConnectedComponent)
def process(value):
  pi
= math.pi
  centroid
= value.Centroid
  contour
= value.Contour
 
if contour is not None:
    boxW
= contour.Rect.Width
    boxH
= contour.Rect.Height
    boxX
= contour.Rect.X
    boxY
= contour.Rect.Y
    X
= boxX + (boxW / 2.0) - centroid.X
    Y
= boxY + (boxH / 2.0) - centroid.Y
   
OrientRad = value.Orientation
   
# Orientation: 0 is right, -pi/2 is up, pi/2 is down
   
# Heading: 0 rad is right, left is pi, down is pi/2, up is 3pi/2 (+ is CW, - is CCW)
   
if (OrientRad >= 0.0) and (OrientRad < pi/4.0):
     
if (X < 0.0):
        value
.Orientation = OrientRad
     
else:
        value
.Orientation = OrientRad + pi
   
elif (OrientRad >= pi/4.0) and (OrientRad <= pi/2.0):
     
if (Y < 0.0):
        value
.Orientation = OrientRad
     
else:
        value
.Orientation = OrientRad + pi
   
elif (OrientRad < 0.0) and (OrientRad > -1.0*pi/4.0):
     
if (X < 0.0):
        value
.Orientation = OrientRad + (2 * pi)
     
else:
        value
.Orientation = OrientRad + pi
   
elif (OrientRad <= -1.0*pi/4.0) and (OrientRad >= -1.0*pi / 2.0):
     
if (Y > 0.0):
        value
.Orientation = OrientRad + (2 * pi)
     
else:
        value
.Orientation = OrientRad + pi
 
return value

Best,

Amir Asgharsharghi

unread,
Feb 9, 2016, 10:56:19 AM2/9/16
to Bonsai Users
It's working very nice and robust ... thanks for the help Goncalo :)

<< We really should start packing these useful nodes into a Bonsai.Fish package to include in the Bonsai.Neuro repository.

I agree it would be very useful and really cool to have it ... hoping for that :)

Cheers,
Amir
Reply all
Reply to author
Forward
0 new messages