def el_fitting(mask): padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) if len(contours) != 1: point_num = [0] * len(contours) for i in range(len(contours)): # i=0,1,2... point_num[i] = len(contours[i]) # print(point_num.index(max(point_num[:]))) contours = [contours[point_num.index(max(point_num[:]))]] # 将坐标格式改写成椭圆拟合程序需要的格式 # 也就是[[横坐标],[纵坐标]] for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 h = verts[:, 0] z = verts[:, 1] # width height分别是半长轴和半短轴 data = np.array([h, z]) lsqe = el.LSqEllipse() lsqe.fit(data) center, width, height, phi = lsqe.parameters() a = max(width, height) b = min(width, height) area = math.pi * a * b eccentricity = pow((1 - (b / a)**2), 1 / 2) pore_opening_degree = b / a return a, b, area, eccentricity, pore_opening_degree
def drawImage(image, boxes, masks, class_ids, class_names, scores, show_bbox=False, show_label=False, show_seg=True, show_center=True): N = boxes.shape[0] # Number of instances if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] masked_image = image.astype(np.uint32).copy() car_count = 0 for i in range(N): class_id = class_ids[i] if class_id != 3 and class_id != 6 and class_id != 8: continue car_count = car_count + 1 # Bounding Box. if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 1, cv2.LINE_AA) #p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') if show_center: cv2.circle(image, (x1 + int(math.fabs(float(x2 - x1))/2), y1 + int(math.fabs(float(y2 - y1))/2)), 4, (0, 0, 255), -1) if show_label: score = scores[i] if scores is not None else None label = class_names[class_id] caption = "{} {:.3f}".format(label, score) if score else label font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(image, caption, (x1, y1 + 8), font, 1, (255, 255, 255), 1, cv2.LINE_AA) #ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") if show_seg: # Mask mask = masks[:, :, i] masked_image = visualize.apply_mask(masked_image, mask, (0, 0, 0)) # Mask Polyline # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 cv2.polylines(image, np.int32([verts]), True, (0, 255, 255)) #p = visualize.Polygon(verts, facecolor="none", edgecolor=()) #ax.add_patch(p) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(image, 'CAR: {}'.format(car_count), (5, 32), font, 1, (0, 0, 255), 2, cv2.LINE_AA) pass
def display_siamese_instances(target, image, boxes, masks, class_ids, scores=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(3, 3) ax = plt.subplot(gs[:, 1:]) target_ax = plt.subplot(gs[1, 0]) auto_show = True # Generate random colors colors = colors or visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(title) target_height, target_width = target.shape[:2] target_ax.set_ylim(target_height + 10, -10) target_ax.set_xlim(-10, target_width + 10) target_ax.axis('off') # target_ax.set_title('target') masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_ax.imshow(target.astype(np.uint8)) if auto_show: plt.show() return
def visualize_instances(image, boxes, masks, class_ids, class_names, scores=None, ax=None, file_name=None, colors=None, making_video=False, making_image=False, open_cv=False): # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] if not making_video: # Generate random colors colors = visualize.random_colors(N) fig, ax = get_ax() # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') masked_image = image.astype(np.uint32).copy() for i in range(N): if not making_video: color = colors[i] else: # all objects of a class get same color class_id = class_ids[i] color = colors[class_id - 1] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] # show bbox p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label class_id = class_ids[i] score = scores[i] if scores is not None else None try: label = class_names[class_id] except IndexError: label = 'label_error' x = random.randint(x1, (x1 + x2) // 2) caption = "{} {:.3f}".format(label, score) if score else label ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) # To transform the drawn figure into ndarray X fig.canvas.draw() string = fig.canvas.tostring_rgb() masked = masked_image.astype(np.uint8) X = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') X = X.reshape(fig.canvas.get_width_height()[::-1] + (3, )) # open cv's RGB style: BGR # if open_cv: X = X[..., ::-1] if making_video: plt.close() # return masked_image.astype(np.uint8) # cv2.imshow('frame', X) # cv2.waitKey(1) return X elif making_image: # plt.savefig(file_name) cv2.imwrite(file_name, X)
def display_grid(target_list, image_list, boxes_list, masks_list, class_ids_list, scores_list=None, category_names_list=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None, show_scores=True, target_shift=10, fontsize=14, linewidth=2, save=False): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ if type(target_list) == list: M = int(np.sqrt(len(target_list))) if len(target_list) - M**2 > 1e-3: M = M + 1 else: M = 1 target_list = [target_list] image_list = [image_list] boxes_list = [boxes_list] masks_list = [masks_list] class_ids_list = [class_ids_list] if scores_list is not None: scores_list = [scores_list] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(M, M, hspace=0.1, wspace=0.02, left=0, right=1, bottom=0, top=1) # auto_show = True REMOVE index = 0 for m1 in range(M): for m2 in range(M): ax = plt.subplot(gs[m1, m2]) if index >= len(target_list): continue target = target_list[index] image = image_list[index] boxes = boxes_list[index] masks = masks_list[index] class_ids = class_ids_list[index] scores = scores_list[index] # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # Generate random colors colors = visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height, 0) ax.set_xlim(0, width) ax.axis('off') ax.set_title(title) masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=linewidth, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] if show_scores: ax.text(x1, y1 + 8, caption, color='w', size=int(10 / 14 * fontsize), backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask( masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_height, target_width = target.shape[:2] target_height = target_height // 2 target_width = target_width // 2 target_area = target_height * target_width target_scaling = np.sqrt((192 // 2 * 96 // 2) / target_area) target_height = int(target_height * target_scaling) target_width = int(target_width * target_scaling) ax.imshow(target, extent=[ target_shift, target_shift + target_width * 2, height - target_shift, height - target_shift - target_height * 2 ], zorder=9) rect = visualize.patches.Rectangle( (target_shift, height - target_shift), target_width * 2, -target_height * 2, linewidth=5, edgecolor='white', facecolor='none', zorder=10) ax.add_patch(rect) if category_names_list is not None: plt.title(category_names_list[index], fontsize=fontsize) index = index + 1 if auto_show: plt.show() if save: fig.savefig('grid.pdf', bbox_inches='tight') return
def display_instances(self, image, boxes, masks, class_ids, class_names, scores=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None): # Number of instances N = boxes.shape[0] if not N: logging.info("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] _, ax = plt.subplots(1, figsize=figsize) # Generate random colors colors = colors or visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height) ax.set_xlim(width) ax.axis('off') masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None label = class_names[class_id] caption = "{} {:.3f}".format(label, score) if score else label else: caption = captions[i] ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) return ax