def _augment(self, img, _): img = np.copy(img) orig_ann = img[..., 0] # instance ID map fixed_ann = self._fix_mirror_padding(orig_ann) # re-cropping with fixed instance id map crop_ann = cropping_center(fixed_ann, self.crop_shape) orig_dst = np.zeros(orig_ann.shape, dtype=np.float32) inst_list = list(np.unique(crop_ann)) inst_list.remove(0) # 0 is background for inst_id in inst_list: inst_map = np.array(fixed_ann == inst_id, np.uint8) inst_box = bounding_box(inst_map) # expand the box by 2px inst_box[0] -= 2 inst_box[2] -= 2 inst_box[1] += 2 inst_box[3] += 2 inst_map = inst_map[inst_box[0]:inst_box[1], inst_box[2]:inst_box[3]] if inst_map.shape[0] < 2 or \ inst_map.shape[1] < 2: continue # chessboard distance map generation # normalize distance to 0-1 inst_dst = distance_transform_cdt(inst_map) inst_dst = inst_dst.astype('float32') if self.inst_norm: max_value = np.amax(inst_dst) if max_value <= 0: continue # HACK: temporay patch for divide 0 i.e no nuclei (how?) inst_dst = (inst_dst / np.amax(inst_dst)) #### dst_map_box = orig_dst[inst_box[0]:inst_box[1], inst_box[2]:inst_box[3]] dst_map_box[inst_map > 0] = inst_dst[inst_map > 0] # img = img.astype('float32') img = np.dstack([img, orig_dst]) return img
def _augment(self, img, _): img = np.copy(img) orig_ann = img[..., 0] # instance ID map fixed_ann = self._fix_mirror_padding(orig_ann) # re-cropping with fixed instance id map crop_ann = cropping_center(fixed_ann, self.crop_shape) # TODO: deal with 1 label warning crop_ann = morph.remove_small_objects(crop_ann, min_size=30) x_map = np.zeros(orig_ann.shape[:2], dtype=np.float32) y_map = np.zeros(orig_ann.shape[:2], dtype=np.float32) inst_list = list(np.unique(crop_ann)) inst_list.remove(0) # 0 is background for inst_id in inst_list: inst_map = np.array(fixed_ann == inst_id, np.uint8) inst_box = bounding_box(inst_map) # expand the box by 2px # Because we first pad the ann at line 207, the bboxes # will remain valid after expansion inst_box[0] -= 2 inst_box[2] -= 2 inst_box[1] += 2 inst_box[3] += 2 inst_map = inst_map[inst_box[0]:inst_box[1], inst_box[2]:inst_box[3]] if inst_map.shape[0] < 2 or \ inst_map.shape[1] < 2: continue # instance center of mass, rounded to nearest pixel inst_com = list(measurements.center_of_mass(inst_map)) inst_com[0] = int(inst_com[0] + 0.5) inst_com[1] = int(inst_com[1] + 0.5) inst_x_range = np.arange(1, inst_map.shape[1] + 1) inst_y_range = np.arange(1, inst_map.shape[0] + 1) # shifting center of pixels grid to instance center of mass inst_x_range -= inst_com[1] inst_y_range -= inst_com[0] inst_x, inst_y = np.meshgrid(inst_x_range, inst_y_range) # remove coord outside of instance inst_x[inst_map == 0] = 0 inst_y[inst_map == 0] = 0 inst_x = inst_x.astype('float32') inst_y = inst_y.astype('float32') # normalize min into -1 scale if np.min(inst_x) < 0: inst_x[inst_x < 0] /= (-np.amin(inst_x[inst_x < 0])) if np.min(inst_y) < 0: inst_y[inst_y < 0] /= (-np.amin(inst_y[inst_y < 0])) # normalize max into +1 scale if np.max(inst_x) > 0: inst_x[inst_x > 0] /= (np.amax(inst_x[inst_x > 0])) if np.max(inst_y) > 0: inst_y[inst_y > 0] /= (np.amax(inst_y[inst_y > 0])) #### x_map_box = x_map[inst_box[0]:inst_box[1], inst_box[2]:inst_box[3]] x_map_box[inst_map > 0] = inst_x[inst_map > 0] y_map_box = y_map[inst_box[0]:inst_box[1], inst_box[2]:inst_box[3]] y_map_box[inst_map > 0] = inst_y[inst_map > 0] img = img.astype('float32') img = np.dstack([img, x_map, y_map]) return img
interpolation=cv2.INTER_NEAREST) instance_map = np.float32(instance_map) type_map = np.float32(type_map) #cv2.imwrite('{}/{}_inst.png'.format(save_dir, 'sample'), instance_map) #cv2.imwrite('{}/{}_type.png'.format(save_dir, 'sample'), type_map * 60) instance_map = cv2.resize(instance_map, (px, py), interpolation=cv2.INTER_NEAREST) type_map = cv2.resize(type_map, (px, py), interpolation=cv2.INTER_NEAREST) instance_map = np.int32(instance_map) type_map = np.int32(type_map) instances, instcounts = np.unique(instance_map, return_counts=True) for i in range(1, len(instances)): inst_size[instances[i] - 1] = instcounts[i] inst_map = np.array(instance_map == instances[i], np.uint8) y1, y2, x1, x2 = bounding_box(inst_map) y1 = y1 - 2 if y1 - 2 >= 0 else y1 x1 = x1 - 2 if x1 - 2 >= 0 else x1 x2 = x2 + 2 if x2 + 2 <= inst_map.shape[1] - 1 else x2 y2 = y2 + 2 if y2 + 2 <= inst_map.shape[0] - 1 else y2 inst_map = inst_map[y1:y2, x1:x2] contours = cv2.findContours(inst_map, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #inst_size[instances[i]-1] = cv2.contourArea(contours[0]) inst_length[instances[i] - 1] = cv2.arcLength(contours[0][0], True) indexer = np.logical_and(inst_type2 != 0, inst_size != 0) inst_sizes = np.append(inst_sizes, inst_size[indexer]) inst_lengths = np.append(inst_lengths, inst_length[indexer]) inst_types = np.append(inst_types, inst_type2[indexer]) basename = '{}_{}_{}_{}_{}'.format(