def prev_layer(self): bbox1 = self.stack.get_active_bbox() if self.stack.prev_layer(): bbox2 = self.stack.get_active_bbox() combined_bbox = combine_bbox(bbox1, bbox2) if combined_bbox is not None: self.paper.invalidate_bbox( self.paper.get_paper_bbox(combined_bbox)) self.update_layer_label() self.update_frame_label()
def draw_selectbox(self, rect): x, y, w, h = rect x, y, x1, y1 = utils.make_bbox((x, y, x+w, y+h)) w, h = x1-x, y1-y if x+h<self.get_xlim() and y+h<self.get_ylim(): startx, starty = self.get_img_coord(x, y) endx, endy = self.get_img_coord(x+w, y+h) px0, py0, px1, py1 = self.get_paper_bbox((startx-1, starty-1, endx+1, endy+1)) self.invalidate_bbox(utils.combine_bbox(self.selectionrect, (px0, py0, px1, py1))) print "setting selectinrect", (px0, py0, px1, py1) self.selectionrect = (px0, py0, px1, py1)
def change_layer(self, layer): print "changing to layer %s"%self.stack.layers.index(layer) bbox1 = self.stack.get_active_bbox() self.stack.get_layer().box.get_children()[0].set_active(False) self.stack.set_active_layer(self.stack.layers.index(layer) + 1) layer.box.get_children()[0].set_active(True) bbox2 = self.stack.get_active_bbox() combined_bbox = combine_bbox(bbox1, bbox2) if combined_bbox is not None: self.paper.invalidate_bbox( self.paper.get_paper_bbox(combined_bbox)) self.update_layer_label() self.update_frame_label()
def draw_ellipse(self, color, bbox, fill=None, transient=False): if not transient: old_bbox = self.last_rect_bbox else: old_bbox = self.last_rect_bbox bbox = make_bbox(bbox) new_bbox = (bbox[0], bbox[1], bbox[2] + 1, bbox[3] + 1) if old_bbox is not None: self.scratch.image.erase(old_bbox) total_bbox = combine_bbox(old_bbox, new_bbox) else: total_bbox = new_bbox self.scratch.image.draw_ellipse(color, bbox, fill) self.last_rect_bbox = new_bbox return total_bbox
def infer_order_sup(model, image, inmodal, bboxes, input_size=256, use_rgb=True): num = inmodal.shape[0] order_matrix = np.zeros((num, num), dtype=np.int) for i in range(num): for j in range(i + 1, num): if bordering(inmodal[i], inmodal[j]): bbox = utils.combine_bbox(bboxes[(i, j), :]) centerx = bbox[0] + bbox[2] / 2. centery = bbox[1] + bbox[3] / 2. size = max([ np.sqrt(bbox[2] * bbox[3] * 2.), bbox[2] * 1.1, bbox[3] * 1.1 ]) new_bbox = [int(centerx - size / 2.), int(centery - size / 2.), \ int(size), int(size)] image_patch = cv2.resize(utils.crop_padding(image, new_bbox, pad_value=(0, 0, 0)), (input_size, input_size), interpolation=cv2.INTER_CUBIC) modal_i_patch = resize_mask( utils.crop_padding(inmodal[i], new_bbox, pad_value=(0, )), input_size, 'nearest') modal_j_patch = resize_mask( utils.crop_padding(inmodal[j], new_bbox, pad_value=(0, )), input_size, 'nearest') if np.random.rand() > 0.5: # randomize the input order j_over_i = net_forward_ordernet(model, image_patch, modal_j_patch, modal_i_patch, use_rgb) else: j_over_i = not net_forward_ordernet( model, image_patch, modal_i_patch, modal_j_patch, use_rgb) if j_over_i: order_matrix[i, j] = -1 order_matrix[j, i] = 1 else: order_matrix[i, j] = 1 order_matrix[j, i] = -1 return order_matrix
def draw_line(self, color, width, points, transient=False): if not transient: self.scratch.image.draw_line(color, width, points) bbox = make_bbox(points) w = int(round(width + 0.5)) return (bbox[0] - w, bbox[1] - w, bbox[2] + w, bbox[3] + w) else: old_bbox = self.last_rect_bbox bbox = make_bbox(points) w = int(round(width + 0.5)) bbox = (bbox[0] - w, bbox[1] - w, bbox[2] + w, bbox[3] + w) if old_bbox is not None: self.scratch.image.erase(old_bbox) total_bbox = combine_bbox(old_bbox, bbox) else: total_bbox = bbox self.scratch.image.draw_line(color, width, points) self.last_rect_bbox = bbox return total_bbox
def _get_pair(self, modal, bboxes, idx1, idx2, imgfn, load_rgb=False, randshift=False): bbox = utils.combine_bbox(bboxes[(idx1, idx2), :] ) centerx = bbox[0] + bbox[2] / 2. centery = bbox[1] + bbox[3] / 2. size = max([np.sqrt(bbox[2] * bbox[3] * 2.), bbox[2] * 1.1, bbox[3] * 1.1]) # shift & scale aug if self.phase == 'train': if randshift: centerx += np.random.uniform(*self.config['base_aug']['shift']) * size centery += np.random.uniform(*self.config['base_aug']['shift']) * size size /= np.random.uniform(*self.config['base_aug']['scale']) # crop new_bbox = [int(centerx - size / 2.), int(centery - size / 2.), int(size), int(size)] modal1 = cv2.resize(utils.crop_padding(modal[idx1], new_bbox, pad_value=(0,)), (self.sz, self.sz), interpolation=cv2.INTER_NEAREST) modal2 = cv2.resize(utils.crop_padding(modal[idx2], new_bbox, pad_value=(0,)), (self.sz, self.sz), interpolation=cv2.INTER_NEAREST) # flip if self.config['base_aug']['flip'] and np.random.rand() > 0.5: flip = True modal1 = modal1[:, ::-1] modal2 = modal2[:, ::-1] else: flip = False if load_rgb: rgb = np.array(self._load_image(os.path.join( self.config['{}_image_root'.format(self.phase)], imgfn))) # uint8 rgb = cv2.resize(utils.crop_padding(rgb, new_bbox, pad_value=(0,0,0)), (self.sz, self.sz), interpolation=cv2.INTER_CUBIC) if flip: rgb = rgb[:, ::-1, :] rgb = torch.from_numpy(rgb.astype(np.float32).transpose((2, 0, 1)) / 255.) rgb = self.img_transform(rgb) # CHW if load_rgb: return modal1, modal2, rgb else: return modal1, modal2, None
def draw_brush(self, brush, color, pos, transient=False): #print "drawing brush at:", pos #print brush pos=(pos[0] + 1, pos[1] + 1) #tweak to make brush end up in the right place... w,h = int(brush.size[0] / 2), int(brush.size[1] / 2) if self.last_brush_bbox is not None and transient: self.scratch.image.erase(self.last_brush_bbox) if color is None: print "no color" self.scratch.image.paste(brush.data, (pos[0] - w, pos[1] - h), mask=brush.get_mask()) else: self.scratch.image.paint(color, (pos[0] - w, pos[1] - h), mask=brush.get_mask()) if self.last_brush_bbox is None: new = ((pos[0]-w, pos[1]-h, pos[0] + w + 1, pos[1] + h + 1)) self.last_brush_bbox = new return new else: new = ((pos[0]-w, pos[1]-h, pos[0] + w + 1, pos[1] + h + 1)) last = self.last_brush_bbox self.last_brush_bbox = new return combine_bbox(new, last)