def test_ray_features_circle_down_edge(self): seg = np.zeros((400, 600), dtype=bool) x, y = draw.circle(200, 250, 150, shape=seg.shape) seg[x, y] = True points = [(200, 250), (150, 200), (250, 200), (250, 300)] for i, point in enumerate(points): ray_dist_raw = seg_fts.compute_ray_features_segm_2d( seg, point, angle_step=ANGULAR_STEP, edge='down') ray_dist, shift = seg_fts.shift_ray_features(ray_dist_raw) points = seg_fts.reconstruct_ray_features_2d( point, ray_dist, shift) p_fig = export_ray_results(seg, point, points, ray_dist_raw, ray_dist, 'circle_e-down-A-%i.png' % i) self.assertTrue(os.path.exists(p_fig)) x, y = draw.circle(200, 250, 120, shape=seg.shape) seg[x, y] = False for i, point in enumerate(points): ray_dist_raw = seg_fts.compute_ray_features_segm_2d( seg, point, angle_step=ANGULAR_STEP, edge='down') ray_dist, shift = seg_fts.shift_ray_features(ray_dist_raw) points = seg_fts.reconstruct_ray_features_2d( point, ray_dist, shift) p_fig = export_ray_results(seg, point, points, ray_dist_raw, ray_dist, 'circle_e-down-B-%i.png' % i) self.assertTrue(os.path.exists(p_fig))
def test_ray_features_circle_down_edge(self): seg = np.zeros((400, 600), dtype=bool) x, y = draw.circle(200, 250, 150, shape=seg.shape) seg[x, y] = True points = [(200, 250), (150, 200), (250, 200), (250, 300)] for i, point in enumerate(points): ray_dist_raw = compute_ray_features_segm_2d( seg, point, edge='down', angle_step=ANGULAR_STEP) ray_dist, shift = shift_ray_features(ray_dist_raw) points_rt = reconstruct_ray_features_2d(point, ray_dist, shift) p_fig = export_ray_results(seg, point, points_rt, ray_dist_raw, ray_dist, 'circle-full_edge-down-%i.png' % i) self.assertTrue(os.path.isfile(p_fig)) # insert white interior x, y = draw.circle(200, 250, 120, shape=seg.shape) seg[x, y] = False for i, point in enumerate(points): ray_dist_raw = compute_ray_features_segm_2d( seg, point, edge='down', angle_step=ANGULAR_STEP) ray_dist, shift = shift_ray_features(ray_dist_raw) points_rt = reconstruct_ray_features_2d(point, ray_dist, shift) p_fig = export_ray_results(seg, point, points_rt, ray_dist_raw, ray_dist, 'circle-inter_edge-down-%i.png' % i) self.assertTrue(os.path.isfile(p_fig))
def prepare_boundary_points_ray_join(seg, centers, close_points=5, min_diam=MIN_ELLIPSE_DAIM, sel_bg=STRUC_ELEM_BG, sel_fg=STRUC_ELEM_FG): """ extract some point around foreground boundaries :param ndarray seg: input segmentation :param [(int, int)] centers: list of centers :param float close_points: remove closest point then a given threshold :param int min_diam: minimal size of expected objest :param int sel_bg: smoothing background with morphological operation :param int sel_fg: smoothing foreground with morphological operation :return [ndarray]: >>> seg = np.zeros((10, 20), dtype=int) >>> ell_params = 5, 10, 4, 6, np.deg2rad(30) >>> seg = add_overlap_ellipse(seg, ell_params, 1) >>> pts = prepare_boundary_points_ray_join(seg, [(4, 9)], 5, 3, ... sel_bg=1, sel_fg=0) >>> np.round(pts).tolist() # doctest: +NORMALIZE_WHITESPACE [[[4.0, 16.0], [7.0, 10.0], [9.0, 6.0], [1.0, 9.0], [4.0, 16.0], [7.0, 10.0], [1.0, 9.0]]] """ seg_bg, seg_fg = split_segm_background_foreground(seg, sel_bg, sel_fg) points_centers = [] for center in centers: ray_bg = seg_fts.compute_ray_features_segm_2d(seg_bg, center) ray_bg[ray_bg < min_diam] = min_diam points_bg = seg_fts.reconstruct_ray_features_2d(center, ray_bg) points_bg = seg_fts.reduce_close_points(points_bg, close_points) ray_fc = seg_fts.compute_ray_features_segm_2d(seg_fg, center, edge='down') ray_fc[ray_fc < min_diam] = min_diam points_fc = seg_fts.reconstruct_ray_features_2d(center, ray_fc) points_fc = seg_fts.reduce_close_points(points_fc, close_points) points_both = np.vstack((points_bg, points_fc)) points_centers.append(points_both) return points_centers
def prepare_boundary_points_ray_mean( seg, centers, close_points=5, min_diam=MIN_ELLIPSE_DAIM, sel_bg=STRUC_ELEM_BG, sel_fg=STRUC_ELEM_FG, ): """ extract some point around foreground boundaries :param ndarray seg: input segmentation :param [(int, int)] centers: list of centers :param float close_points: remove closest point then a given threshold :param int min_diam: minimal size of expected objest :param int sel_bg: smoothing background with morphological operation :param int sel_fg: smoothing foreground with morphological operation :return [ndarray]: >>> seg = np.zeros((10, 20), dtype=int) >>> ell_params = 5, 10, 4, 6, np.deg2rad(30) >>> seg = add_overlap_ellipse(seg, ell_params, 1) >>> pts = prepare_boundary_points_ray_mean(seg, [(4, 9)], 2.5, 3, sel_bg=1, sel_fg=0) >>> np.round(pts).tolist() # doctest: +NORMALIZE_WHITESPACE [[[4.0, 16.0], [7.0, 15.0], [9.0, 5.0], [4.0, 5.0], [1.0, 7.0], [0.0, 14.0]]] """ seg_bg, seg_fc = split_segm_background_foreground(seg, sel_bg, sel_fg) points_centers = [] for center in centers: ray_bg = compute_ray_features_segm_2d(seg_bg, center) ray_fc = compute_ray_features_segm_2d(seg_fc, center, edge='down') # replace not found (-1) by large values rays = np.array([ray_bg, ray_fc], dtype=float) rays[rays < 0] = np.inf rays[rays < min_diam] = min_diam # take the smalles from both ray_min = np.min(rays, axis=0) ray_mean = np.mean(rays, axis=0) ray_mean[np.isinf(ray_mean)] = ray_min[np.isinf(ray_mean)] points_close = reconstruct_ray_features_2d(center, ray_mean) points_close = reduce_close_points(points_close, close_points) points_centers.append(points_close) return points_centers
def test_ray_features_circle(self): seg = np.ones((400, 600), dtype=bool) x, y = draw.circle(200, 250, 100, shape=seg.shape) seg[x, y] = False points = [(200, 250), (150, 200), (250, 200), (250, 300)] for i, point in enumerate(points): ray_dist_raw = compute_ray_features_segm_2d( seg, point, angle_step=ANGULAR_STEP) ray_dist, shift = shift_ray_features(ray_dist_raw) points = reconstruct_ray_features_2d(point, ray_dist, shift) p_fig = export_ray_results(seg, point, points, ray_dist_raw, ray_dist, 'circle-%i.png' % i) self.assertTrue(os.path.isfile(p_fig))
def prepare_boundary_points_ray_dist(seg, centers, close_points=1, sel_bg=STRUC_ELEM_BG, sel_fg=STRUC_ELEM_FG): """ extract some point around foreground boundaries :param ndarray seg: input segmentation :param [(int, int)] centers: list of centers :param float close_points: remove closest point then a given threshold :param int sel_bg: smoothing background with morphological operation :param int sel_fg: smoothing foreground with morphological operation :return [ndarray]: >>> seg = np.zeros((10, 20), dtype=int) >>> ell_params = 5, 10, 4, 6, np.deg2rad(30) >>> seg = add_overlap_ellipse(seg, ell_params, 1) >>> pts = prepare_boundary_points_ray_dist(seg, [(4, 9)], 2, sel_bg=0, sel_fg=0) >>> np.round(pts, 2).tolist() # doctest: +NORMALIZE_WHITESPACE [[[4.0, 16.0], [6.8, 15.0], [9.0, 5.5], [4.35, 5.0], [1.0, 6.9], [1.0, 9.26], [0.0, 11.31], [0.5, 14.0], [1.45, 16.0]]] """ seg_bg, _ = split_segm_background_foreground(seg, sel_bg, sel_fg) points = [] for center in centers: ray = compute_ray_features_segm_2d(seg_bg, center) points_bg = reconstruct_ray_features_2d(center, ray, 0) points_bg = reduce_close_points(points_bg, close_points) points += points_bg.tolist() points = np.array(points) # remove all very small negative valeue, probaly by rounding points[(points < 0) & (points > -1e-3)] = 0. dists = spatial.distance.cdist(points, centers, metric='euclidean') close_center = np.argmin(dists, axis=1) points_centers = [] for i in range(close_center.max() + 1): pts = points[close_center == i] points_centers.append(pts) return points_centers
def test_ray_features_ellipse(self): seg = np.ones((400, 600), dtype=bool) x, y = draw.ellipse(200, 250, 120, 200, rotation=np.deg2rad(30), shape=seg.shape) seg[x, y] = False points = [(200, 250), (150, 200), (250, 300)] for i, point in enumerate(points): ray_dist_raw = seg_fts.compute_ray_features_segm_2d( seg, point, angle_step=ANGULAR_STEP) # ray_dist, shift = seg_fts.shift_ray_features(ray_dist_raw) points = seg_fts.reconstruct_ray_features_2d(point, ray_dist_raw) p_fig = export_ray_results(seg, point, points, ray_dist_raw, [], 'ellipse-%i.png' % i) self.assertTrue(os.path.exists(p_fig))
def test_ray_features_polygon(self): seg = np.ones((400, 600), dtype=bool) x, y = draw.polygon(np.array([50, 170, 300, 250, 150, 150, 50]), np.array([100, 270, 240, 150, 150, 80, 50]), shape=seg.shape) seg[x, y] = False centres = [(150, 200), (200, 250), (250, 200), (120, 100)] for i, point in enumerate(centres): ray_dist_raw = compute_ray_features_segm_2d( seg, point, angle_step=ANGULAR_STEP) ray_dist, shift = shift_ray_features(ray_dist_raw) points = reconstruct_ray_features_2d(point, ray_dist, shift) p_fig = export_ray_results(seg, point, points, ray_dist_raw, ray_dist, 'polygon-%i.png' % i) self.assertTrue(os.path.isfile(p_fig))
def prepare_boundary_points_ray_dist(seg, centers, close_points=1, sel_bg=STRUC_ELEM_BG, sel_fg=STRUC_ELEM_FG): """ extract some point around foreground boundaries :param ndarray seg: input segmentation :param [(int, int)] centers: list of centers :param float close_points: remove closest point then a given threshold :param int sel_bg: smoothing background with morphological operation :param int sel_fg: smoothing foreground with morphological operation :return [ndarray]: >>> seg = np.zeros((10, 20), dtype=int) >>> ell_params = 5, 10, 4, 6, np.deg2rad(30) >>> seg = add_overlap_ellipse(seg, ell_params, 1) >>> pts = prepare_boundary_points_ray_dist(seg, [(4, 9)], 2, ... sel_bg=0, sel_fg=0) >>> np.round(pts).tolist() # doctest: +NORMALIZE_WHITESPACE [[[0.0, 2.0], [4.0, 16.0], [6.0, 15.0], [9.0, 6.0], [6.0, 5.0], [3.0, 7.0], [0.0, 10.0]]] """ seg_bg, _ = split_segm_background_foreground(seg, sel_bg, sel_fg) points = np.array((0, np.asarray(centers).shape[1])) for center in centers: ray = seg_fts.compute_ray_features_segm_2d(seg_bg, center) points_bg = seg_fts.reconstruct_ray_features_2d(center, ray, 0) points_bg = seg_fts.reduce_close_points(points_bg, close_points) points = np.vstack((points, points_bg)) dists = spatial.distance.cdist(points, centers, metric='euclidean') close_center = np.argmin(dists, axis=1) points_centers = [] for i in range(close_center.max() + 1): pts = points[close_center == i] points_centers.append(pts) return points_centers