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