def generate_skeleton_img(skeleton, i_keep, shape, dtype=np.uint8): max_value = np.iinfo(dtype).max skeleton_img = np.zeros(shape, dtype=dtype) for i in i_keep: px_coords = skeleton.path_coordinates(i).astype(int) for px_coord in px_coords: skeleton_img[px_coord[0], px_coord[1]] = max_value ## closing kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) img = skeleton_img img = cv2.dilate(skeleton_img, kernel, iterations=1) img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) return bin2img(skeletonize(img2bin(img)))
def update_skeleton(skeleton_img, skeleton, i_remove): dtype = skeleton_img.dtype max_value = np.iinfo(dtype).max skeleton_prune_img = skeleton_img.copy() for i in i_remove: px_coords = skeleton.path_coordinates(i).astype(int) rows = px_coords[:, 0] cols = px_coords[:, 1] skeleton_prune_img[rows, cols] = 0 ## closing kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) img = cv2.dilate(skeleton_prune_img, kernel, iterations=1) img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) img = remove_blobs(img) return bin2img(skeletonize(img / max_value))
def skeletonize_img(img): return bin2img(skeletonize(img2bin(img)))
def label2img(labels, label, dim): data = labels.ravel() == label img = data.reshape(dim) return bin2img(img)
def segment_truss(img_hue, img_a=None, save="False", name="", pwd="", my_settings=None): if my_settings is None: my_settings = settings.segment_image() n = 3 centers_prior_hue = { 'tomato': np.deg2rad(0), # [rad] 'peduncle': np.deg2rad(90), # [rad] 'background': np.deg2rad(240) } # [rad] centers_prior_a = {'tomato': 1.0, 'peduncle': 0.5, 'background': 0.0} centers_prior = {'hue': centers_prior_hue, 'a': centers_prior_a} centers = { 'hue': centers_prior['hue'].values(), 'a': centers_prior['a'].values() } if img_a is None: centers, labels = k_means_hue(img_hue, n, centers=centers) # centers else: a_min = np.min(img_a) a_max = np.max(img_a) img_a_norm = normalize_image(img_a) centers, _ = k_means_hue_a(img_hue, img_a_norm, n, my_settings, centers=centers) # centers labels = assign_labels(img_hue, centers, hue_radius=my_settings['hue_radius'], img_a=img_a) # determine which center corresponds to which segment lbl = {} lbl["tomato"] = np.argmin( angular_difference(centers['hue'], centers_prior['hue']['tomato'])) lbl["background"] = np.argmin( angular_difference(centers['hue'], centers_prior['hue']['background'])) lbl["peduncle"] = list(set(range(0, n)) - set(lbl.values()))[0] # compute masks dim = img_hue.shape tomato = label2img(labels, lbl["tomato"], dim) if np.abs(centers['hue'][lbl["background"]] - centers['hue'][lbl["peduncle"]]) < np.deg2rad(10): print "did not detect a peduncle" peduncle = bin2img(np.zeros(dim)) background = cv2.bitwise_not(tomato) else: peduncle = label2img(labels, lbl["peduncle"], dim) background = label2img(labels, lbl["background"], dim) if save: both_hist(img_hue, img_a_norm, centers, lbl, a_bins=a_max - a_min + 1, pwd=pwd, name=name, hue_radius=my_settings['hue_radius']) hue_hist(img_hue, np.rad2deg(centers['hue']), lbl, name, pwd) if img_a is not None: a_hist(img_a_norm, centers['a'], lbl, bins=a_max - a_min + 1, name=name, pwd=pwd) return background, tomato, peduncle