@@ -165,3 +165,59 @@ def get_hull_aspect_ratios(self):
165165 ls = min ([float (a ), float (b )]) / max ([float (a ), float (b )])
166166
167167 return ab , ls
168+
169+ def get_angle (self ):
170+ sorted_hull_points = self .get_convex_hull ()
171+ hull_point_array = numpy .asarray (sorted_hull_points )
172+
173+ ellipse_hull = skimage .measure .EllipseModel ()
174+ success = ellipse_hull .estimate (hull_point_array )
175+
176+ if not success :
177+ add_points = 10
178+
179+ while not success and add_points < 100 :
180+ add_points_skip = int (math .floor (len (self .path ) / add_points ))
181+
182+ path = numpy .asarray (self .path )
183+ add_points_arr = path [::add_points_skip ]
184+
185+ new_arr = numpy .append (hull_point_array , add_points_arr , 0 )
186+ hull_point_array = new_arr .copy ()
187+
188+
189+ ellipse_hull = skimage .measure .EllipseModel ()
190+ success = ellipse_hull .estimate (hull_point_array )
191+
192+ add_points += 10
193+
194+ xc , yc , a , b , phi = ellipse_hull .params
195+ # need to calculate the slope of the line relative to the image, not the x,y axis.
196+ a_p1 = xc + a * math .cos (phi ), yc + a * math .sin (phi )
197+ a_p2 = xc - a * math .cos (phi ), yc - a * math .sin (phi )
198+ b_p1 = xc - b * math .sin (phi ), yc + b * math .cos (phi )
199+ b_p2 = xc + b * math .sin (phi ), yc - b * math .cos (phi )
200+
201+ # we need to invert only the apparent y-axis
202+ x_max , y_max = self .image .shape
203+ m = ((y_max - a_p1 [0 ]) - (y_max - a_p2 [0 ])) / (a_p1 [1 ] - a_p2 [1 ])
204+ image_angle = math .atan (m )
205+ image_angle_deg = math .degrees (image_angle )
206+ if image_angle_deg < 0 :
207+ image_angle_deg += 180
208+
209+ #import matplotlib.pyplot
210+
211+ #matplotlib.pyplot.figure(15)
212+ #matplotlib.pyplot.imshow(self.image)
213+ #for hp in hull_point_array:
214+ # r, c = hp
215+ # matplotlib.pyplot.plot(r, c, 'o')
216+ # matplotlib.pyplot.scatter([c], [r], color='#AA3C39', marker=',')
217+
218+ #matplotlib.pyplot.plot([a_p1[1], a_p2[1]], [a_p1[0], a_p2[0]], 'y') #Change y to change color
219+ #matplotlib.pyplot.plot([b_p1[1], b_p2[1]], [b_p1[0], b_p2[0]], 'c') #Change c to change color
220+ #matplotlib.pyplot.savefig('/Users/adamlanda/Documents/JAnaP/data/testfig.png')
221+ #matplotlib.pyplot.close()
222+
223+ return image_angle_deg
0 commit comments