def process(sortedRegions): global base, centroidLoc, centroidVel vSortRegionList = list( Enumerable.OrderByDescending( sortedRegions, lambda x: FindBasePoint(x.Contour.ToArray[Point] ()).Y)) if len(vSortRegionList) >= 3: blob1Index = 2 newbase = Point2f( FindTipPoint(vSortRegionList[blob1Index].Contour.ToArray[Point]())) return newbase #vSortRegionList[blob1Index].Orientation else: return NaNPoint #float(0)
import clr clr.AddReference("System.Core") clr.AddReference("OpenCV.Net") clr.AddReference("Bonsai.Vision") from System import Tuple, Math, Single, Array import math from System.Linq import Enumerable from OpenCV.Net import Point, Point2f from Bonsai.Vision import ConnectedComponentCollection noWhisker = True firstPass = True NaNPoint = Point2f(float.NaN, float.NaN) baseDistThd = 20 centroidDistThd = 100 numWhiskers = 0 def FindBasePoint(inputPointArray): pointList = Enumerable.OrderBy( inputPointArray, lambda x: x.X) #X point for vertical head position firstPoint = Enumerable.First(pointList) return firstPoint def DistBetweenPoints(pt1, pt2): dx = abs(pt2.X - pt1.X) dy = abs(pt2.Y - pt1.Y) # distance vector return math.sqrt(dx**2 + dy**2)
def process(sortedRegions): vSortRegionList = list( Enumerable.OrderByDescending( sortedRegions, lambda x: FindBasePoint(x.Contour.ToArray[Point] ()).Y)) if len(vSortRegionList) >= 3: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[2].Contour.ToArray[Point]()))) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), Point2f(vSortRegionList[2].Centroid.X, vSortRegionList[2].Centroid.Y)) tipList = Tuple.Create( Point2f(FindTipPoint(vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindTipPoint(vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindTipPoint(vSortRegionList[2].Contour.ToArray[Point]()))) elif len(vSortRegionList) == 2: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), NaNPoint) tipList = Tuple.Create( Point2f(FindTipPoint(vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindTipPoint(vSortRegionList[1].Contour.ToArray[Point]())), NaNPoint) elif len(vSortRegionList) == 1: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), NaNPoint, NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), NaNPoint, NaNPoint) tipList = Tuple.Create( Point2f(FindTipPoint(vSortRegionList[0].Contour.ToArray[Point]())), NaNPoint, NaNPoint) else: baseList = Tuple.Create(NaNPoint, NaNPoint, NaNPoint) centroidList = Tuple.Create(NaNPoint, NaNPoint, NaNPoint) tipList = Tuple.Create(NaNPoint, NaNPoint, NaNPoint) return Tuple.Create(baseList, centroidList, tipList)
def process(sortedRegions): # Order component list by decreasing area size #largest = list(Enumerable.OrderByDescending(sortedRegions, lambda x:x.Area)) # Order component list by centroid position #hSortRegionList = list(Enumerable.OrderBy(sortedRegions, lambda x:x.Centroid.X)) vSortRegionList = list( Enumerable.OrderByDescending( sortedRegions, lambda x: FindBasePoint(x.Contour.ToArray[Point] ()).Y)) if len(vSortRegionList) >= 6: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[2].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[3].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[4].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[5].Contour.ToArray[Point]()))) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), Point2f(vSortRegionList[2].Centroid.X, vSortRegionList[2].Centroid.Y), Point2f(vSortRegionList[3].Centroid.X, vSortRegionList[3].Centroid.Y), Point2f(vSortRegionList[4].Centroid.X, vSortRegionList[4].Centroid.Y), Point2f(vSortRegionList[5].Centroid.X, vSortRegionList[5].Centroid.Y)) elif len(vSortRegionList) == 5: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[2].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[3].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[4].Contour.ToArray[Point]())), NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), Point2f(vSortRegionList[2].Centroid.X, vSortRegionList[2].Centroid.Y), Point2f(vSortRegionList[3].Centroid.X, vSortRegionList[3].Centroid.Y), Point2f(vSortRegionList[4].Centroid.X, vSortRegionList[4].Centroid.Y), NaNPoint) elif len(vSortRegionList) == 4: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[2].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[3].Contour.ToArray[Point]())), NaNPoint, NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), Point2f(vSortRegionList[2].Centroid.X, vSortRegionList[2].Centroid.Y), Point2f(vSortRegionList[3].Centroid.X, vSortRegionList[3].Centroid.Y), NaNPoint, NaNPoint) elif len(vSortRegionList) == 3: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[2].Contour.ToArray[Point]())), NaNPoint, NaNPoint, NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), Point2f(vSortRegionList[2].Centroid.X, vSortRegionList[2].Centroid.Y), NaNPoint, NaNPoint, NaNPoint) elif len(vSortRegionList) == 2: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), Point2f(FindBasePoint( vSortRegionList[1].Contour.ToArray[Point]())), NaNPoint, NaNPoint, NaNPoint, NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), Point2f(vSortRegionList[1].Centroid.X, vSortRegionList[1].Centroid.Y), NaNPoint, NaNPoint, NaNPoint, NaNPoint) elif len(vSortRegionList) == 1: baseList = Tuple.Create( Point2f(FindBasePoint( vSortRegionList[0].Contour.ToArray[Point]())), NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint) centroidList = Tuple.Create( Point2f(vSortRegionList[0].Centroid.X, vSortRegionList[0].Centroid.Y), NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint) else: baseList = Tuple.Create(NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint) centroidList = Tuple.Create(NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint, NaNPoint) return Tuple.Create(baseList, centroidList)
import clr clr.AddReference("System.Core") clr.AddReference("OpenCV.Net") clr.AddReference("Bonsai.Vision") from System import Tuple, Math, Single, Array import math from System.Linq import Enumerable from OpenCV.Net import Point, Point2f from Bonsai.Vision import ConnectedComponentCollection base = None nanpoint = Point2f(float.NaN,float.NaN) centroidLoc = nanpoint centroidVel = nanpoint def distfromlastframe(pt1,pt2): dx = (pt2.X - pt1.X) dy = (pt2.Y - pt1.Y) return dx + dy def pointvelocity(pt1,pt2): dx = (pt2.X - pt1.X) dy = (pt2.Y - pt1.Y) return Point(dx,dy) def bisector(pt1,pt2): dx = abs(pt2.X - pt1.X) dy = abs(pt2.Y - pt1.Y) if dx+dy !=0: #if abs(pt2.X) + abs(pt2.Y) < 20: # return 0