def make_prototype(triangles, spot_based=True): trans_triangles = [] for t in triangles: # Centralize the triangle in the plane t += np.array([250.0, 250.0]) - geometry.centroid(t) # If non-spot-based pototype is requested, swap the vertices around so that vertex 1 is # the pointiest one. if spot_based == False: angles = [geometry.angle(t,1), geometry.angle(t,2), geometry.angle(t,3)] min_angle = angles.index(min(angles)) if min_angle == 0: t = np.array([t[0], t[1], t[2]]) elif min_angle == 1: t = np.array([t[1], t[2], t[0]]) elif min_angle == 2: t = np.array([t[2], t[0], t[1]]) # Rotate the triangle around its centroid so that vertex 1 points North t = geometry.rotate(t) # Ensure that vertex 2 is to the left of vertex 3 to prevent cancelling out if t[1,0] > t[2,0]: t = np.array([t[0], t[2], t[1]]) trans_triangles.append(t) # Reformat as Numpy array and take the mean of the coordinates to form the prototype trans_triangles = np.asarray(trans_triangles, dtype=float) prototype = trans_triangles.mean(axis=0) # Shift the prototype such that its bounding box is vertically centralized in the plane prototype[:, 1] += ((500.0 - (max([prototype[1,1], prototype[2,1]]) - prototype[0,1])) / 2.0) - prototype[0,1] return prototype
def location_distance(t1, t2): return geometry.ED(geometry.centroid(t1), geometry.centroid(t2))
if i == 0: coords_last = np.nan else: coords_last = coords_current # Create a list for the objects in the current frame coords_current = [None] * len(indices) # Loop over all shapes in this frame for j, shape in enumerate(indices): # Convert coordinates of annotation to numpy array point_coords = np.asarray(annotation['shapes'][shape]['points']) # Compute centroid coords_current[j] = geometry.centroid(point_coords) # Plot outline of current object annotation on the frame plt.fill(point_coords[:,0], point_coords[:,1], \ facecolor='none', edgecolor=colors[shape]) # Plot the centroid plt.plot(coords_current[j][0], coords_current[j][1], \ marker='o', color=colors[shape], \ label=annotation['shapes'][shape]['label']) # Don't show axes plt.axis('off') # Show the legend plt.legend()