def imshow_segms(img_or_path, segms=None, labels=None, scores=None, score_threshold=0.0, colors='red', thickness=3, show=True, win_name='', wait_time=0, out_file=None, return_img=False, draw_contours=False): grayscale_image = img_or_path.astype(np.float64) max_value = np.max(grayscale_image) min_value = np.min(grayscale_image) grayscale_image = 255 * (grayscale_image - min_value) / (max_value - min_value) grayscale_image = grayscale_image.astype(np.uint8) grayscale_image = np.pad(grayscale_image, ((25, 25), (25, 25)), 'constant', constant_values = (0, 0)) # grayscale_image[grayscale_image < 128] = 0 ret, grayscale_image = cv2.threshold(grayscale_image, 128, 255, 0) nLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(grayscale_image.astype(np.uint8), connectivity=4) det = [] mapper = [] for k in range(1, nLabels): # size filtering size = stats[k, cv2.CC_STAT_AREA] if size < 10: continue x, y = stats[k, cv2.CC_STAT_LEFT], stats[k, cv2.CC_STAT_TOP] w, h = stats[k, cv2.CC_STAT_WIDTH], stats[k, cv2.CC_STAT_HEIGHT] niter = int(np.sqrt(size * min(w, h) / (w * h)) * 2) kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1 + niter, 1 + niter)) grayscale_image = cv2.dilate(grayscale_image, kernel) # make box np_contours = np.roll(np.array(np.where(grayscale_image!=0)),1,axis=0).transpose().reshape(-1,2) rect = cv2.minAreaRect(np_contours) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(grayscale_image, [box], 0, (0, 0, 255), 3) x, y, w, h, theta = rect[0][0], rect[0][1], rect[1][0], rect[1][1], rect[2] theta = theta * np.pi / 180.0 thetaobb = [x, y, w, h, theta] print(thetaobb) pointobb = thetaobb2pointobb(thetaobb) print(pointobb) imshow_rbboxes(grayscale_image, pointobb, win_name='demo')
def __hrsc_parse__(self, hrsc_label_file, hrsc_image_file): objects = [] if self.groundtruth: tree = ET.parse(hrsc_label_file) root = tree.getroot() objects = [] hrsc_object = root.find('HRSC_Objects') for hrsc_sub_object in hrsc_object.findall('HRSC_Object'): obj_struct = {} xmin = float(hrsc_sub_object.find('box_xmin').text) ymin = float(hrsc_sub_object.find('box_ymin').text) xmax = float(hrsc_sub_object.find('box_xmax').text) ymax = float(hrsc_sub_object.find('box_ymax').text) bbox_w = xmax - xmin bbox_h = ymax - ymin cx = float(hrsc_sub_object.find('mbox_cx').text) cy = float(hrsc_sub_object.find('mbox_cy').text) rbbox_w = float(hrsc_sub_object.find('mbox_w').text) rbbox_h = float(hrsc_sub_object.find('mbox_h').text) angle = float(hrsc_sub_object.find('mbox_ang').text) # angle = angle * 180.0 / np.pi obj_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h] obj_struct['thetaobb'] = [cx, cy, rbbox_w, rbbox_h, angle] obj_struct['segmentation'] = thetaobb2pointobb( obj_struct['thetaobb']) obj_struct['pointobb'] = pointobb_sort_function[ pointobb_sort_method](obj_struct['segmentation']) obj_struct['keypoints'] = obj_struct['pointobb'][:] for idx in [2, 5, 8, 11]: obj_struct['keypoints'].insert(idx, 2) obj_struct['hobb'] = thetaobb2hobb( obj_struct['thetaobb'], pointobb_sort_function[pointobb_sort_method]) obj_struct['label'] = 1 objects.append(obj_struct) else: obj_struct = {} obj_struct['segmentation'] = [0, 0, 0, 0, 0, 0, 0, 0] obj_struct['keypoint'] = [0, 0, 0, 0, 0, 0, 0, 0] obj_struct['pointobb'] = [0, 0, 0, 0, 0, 0, 0, 0] obj_struct['thetaobb'] = [0, 0, 0, 0, 0] obj_struct['hobb'] = [0, 0, 0, 0, 0] obj_struct['bbox'] = [0, 0, 0, 0] obj_struct['label'] = 0 objects.append(obj_struct) return objects
def imshow_rbboxes(img_or_path, rbboxes, labels=None, scores=None, score_threshold=0.0, colors='red', show_label=False, show_score=False, thickness=3, show=True, win_name='', wait_time=0, out_file=None, return_img=False): """ Draw oriented bounding boxes on image Args: img (str or ndarray): The image to be displayed. rbboxes (list or ndarray): A ndarray of shape (N, 8) labels (list or ndarray): A ndarray of shape (N, 1) scores (list or ndarray): A ndarray of shape (N, 1) """ if is_str(img_or_path): img = cv2.imread(img_or_path) else: img = img_or_path if rbboxes == []: return if isinstance(rbboxes, list): rbboxes = np.array(rbboxes) if rbboxes.shape[1] == 5: rbboxes_ = [] for rbbox in rbboxes: rbboxes_.append(thetaobb2pointobb(rbbox)) rbboxes = np.array(rbboxes_) if rbboxes.ndim == 1: rbboxes = np.array([rbboxes]) if labels is None: labels_vis = np.array(['ins'] * rbboxes.shape[0]) else: labels_vis = np.array(labels) if labels_vis.ndim == 0: labels_vis = np.array([labels_vis]) if scores is None: scores_vis = np.array([1.0] * rbboxes.shape[0]) else: scores_vis = np.array(scores) if scores_vis.ndim == 0: scores_vis = np.array([scores_vis]) if labels is None: colors = dict() colors[colors] = color_val(colors) else: max_label = max(labels) colors = [color_val(_) for _ in range(max_label + 1)] for rbbox, label, score in zip(rbboxes, labels_vis, scores_vis): if score < score_threshold: continue if len(rbbox) == 5: rbbox = np.array(thetaobb2pointobb(rbbox)) rbbox = rbbox.astype(np.int32) cx = np.mean(rbbox[::2]) cy = np.mean(rbbox[1::2]) current_color = colors[label] for idx in range(-1, 3, 1): cv2.line( img, (int(rbbox[idx * 2]), int(rbbox[idx * 2 + 1])), (int(rbbox[(idx + 1) * 2]), int(rbbox[(idx + 1) * 2 + 1])), current_color, thickness=thickness) if show_label: cv2.putText(img, label, (cx, cy), cv2.FONT_HERSHEY_COMPLEX_SMALL, fontScale=1.0, color=current_color, thickness=2, lineType=8) if show_score: cv2.putText(img, "{:.2f}".format(score), (cx, cy), cv2.FONT_HERSHEY_COMPLEX_SMALL, fontScale=1.0, color=current_color, thickness=2, lineType=8) if show: cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) cv2.imshow(win_name, img) cv2.waitKey(wait_time) if out_file is not None: dir_name = osp.abspath(osp.dirname(out_file)) mkdir_or_exist(dir_name) cv2.imwrite(out_file, img) if return_img: return img
], [ 57.678192138671875, 259.0009765625, 83.1702880859375, 179.4698944091797, -0.4243928624890527 ], [ 264.6916809082031, 741.2076416015625, 82.70415496826172, 145.08529663085938, -0.42366891543622837 ], [ 373.6033630371094, 713.7134399414062, 81.72265625, 179.64755249023438, -0.4245613407175139 ]] for thetaobb in thetaobbs: pointobb = thetaobb2pointobb(thetaobb) pointobbs.append(pointobb) pseudomasks = np.zeros((height, width), dtype=np.int32) for pointobb in pointobbs: transformed, mask_location = pointobb2pseudomask(pointobb, anchor_image, host_height=height, host_width=width) # show_grayscale_as_heatmap(transformed) transformed = transformed.astype(np.int32) pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]] = np.where( transformed > pseudomasks[mask_location[1]:mask_location[3], mask_location[0]:mask_location[2]],