def _rasterize_matplotlib(image, pclouds, render_lines=True, line_style='-', line_colour='b', line_width=1, render_markers=True, marker_style='o', marker_size=1, marker_face_colour='b', marker_edge_colour='b', marker_edge_width=1): import matplotlib.pyplot as plt # Convert image shape into 100 DPI inches # This makes sure we maintain the original image size image_shape = np.array(image.shape)[::-1] / 100.0 f = plt.figure(figsize=image_shape, frameon=False, dpi=100) image.view(figure_id=f.number, figure_size=image_shape) for k, p in enumerate(pclouds): p.view(figure_id=f.number, render_axes=False, figure_size=image_shape, render_lines=render_lines[k], line_style=line_style[k], line_colour=line_colour[k], line_width=line_width[k], render_markers=render_markers[k], marker_style=marker_style[k], marker_size=marker_size[k], marker_face_colour=marker_face_colour[k], marker_edge_colour=marker_edge_colour[k], marker_edge_width=marker_edge_width[k]) # Make sure the layout is tight so that the image is of the original size f.tight_layout(pad=0) # Get the pixels directly from the canvas buffer which is fast c_buffer, shape = f.canvas.print_to_buffer() # Turn buffer into numpy array and reshape to image pixels_buffer = np.array(c_buffer).reshape(shape[::-1] + (-1,)) # Prevent matplotlib from rendering plt.close(f) # Ignore the Alpha channel return Image.init_from_rolled_channels(pixels_buffer[..., :3])
def _rasterize_pillow(image, pclouds, render_lines=True, line_style='-', line_colour='b', line_width=1, render_markers=True, marker_style='o', marker_size=1, marker_face_colour='b', marker_edge_colour='b', marker_edge_width=1): from PIL import ImageDraw if any(x != '-'for x in line_style): raise ValueError("The Pillow rasterizer only supports the '-' " "line style.") if any(x not in {'o', 's'} for x in marker_style): raise ValueError("The Pillow rasterizer only supports the 'o' and 's' " "marker styles.") if any(x > 1 for x in marker_edge_width): raise ValueError('The Pillow rasterizer only supports ' 'marker_edge_width of 1 or 0.') pil_im = image.as_PILImage() draw = ImageDraw.Draw(pil_im) line_colour = [_parse_colour(x) for x in line_colour] marker_edge_colour = [_parse_colour(x) for x in marker_edge_colour] marker_face_colour = [_parse_colour(x) for x in marker_face_colour] for k in range(len(pclouds)): p = pclouds[k] if isinstance(p, TriMesh): pclouds[k] = p.as_pointgraph() points = p.points if (render_lines[k] and line_width[k] > 0 and hasattr(p, 'edges') and p.edges.size > 0): edges = p.edges lines = zip(points[edges[:, 0], :], points[edges[:, 1], :]) for l1, l2 in lines: draw.line([tuple(l1[::-1]), tuple(l2[::-1])], fill=line_colour[k], width=line_width[k]) if render_markers[k] and marker_size[k] > 0: draw_func = (draw.ellipse if marker_style[k] == 'o' else draw.rectangle) outline = (marker_edge_colour[k] if marker_edge_width[k] == 1 else None) for p in points: y, x = p draw_func((x - marker_size[k], y - marker_size[k], x + marker_size[k], y + marker_size[k]), fill=marker_face_colour[k], outline=outline) del draw pixels = np.asarray(pil_im) if image.n_channels == 3: return Image.init_from_rolled_channels(pixels) else: return Image(pixels)
def _rasterize_matplotlib(image, pclouds, render_lines=True, line_style='-', line_colour='b', line_width=1, render_markers=True, marker_style='o', marker_size=1, marker_face_colour='b', marker_edge_colour='b', marker_edge_width=1): import matplotlib.pyplot as plt # Convert image shape into 100 DPI inches # This makes sure we maintain the original image size image_shape = np.array(image.shape)[::-1] / 100.0 f = plt.figure(figsize=image_shape, frameon=False, dpi=100) image.view(figure_id=f.number, figure_size=image_shape) for k, p in enumerate(pclouds): p.view(figure_id=f.number, render_axes=False, figure_size=image_shape, render_lines=render_lines[k], line_style=line_style[k], line_colour=line_colour[k], line_width=line_width[k], render_markers=render_markers[k], marker_style=marker_style[k], marker_size=marker_size[k], marker_face_colour=marker_face_colour[k], marker_edge_colour=marker_edge_colour[k], marker_edge_width=marker_edge_width[k]) # Make sure the layout is tight so that the image is of the original size f.tight_layout(pad=0) # Get the pixels directly from the canvas buffer which is fast c_buffer, shape = f.canvas.print_to_buffer() # Turn buffer into numpy array and reshape to image pixels_buffer = np.array(c_buffer).reshape(shape[::-1] + (-1, )) # Prevent matplotlib from rendering plt.close(f) # Ignore the Alpha channel return Image.init_from_rolled_channels(pixels_buffer[..., :3])
def test_init_from_rolled_channels(): p = np.empty([50, 60, 3]) im = Image.init_from_rolled_channels(p) assert im.n_channels == 3 assert im.height == 50 assert im.width == 60
def shape_context_shape(pc, xr, yr, groups=None, sampls=None, r_inner=0.125, r_outer=2, nbins_r=5, nbins_theta=12): nbins = nbins_r * nbins_theta def get_angle(p1, p2): """Return angle in radians""" return math.atan2((p2[1] - p1[1]), (p2[0] - p1[0])) def compute_one(pt, points, mean_dist): distances = np.array([euclidean(pt, p) for p in points]) r_array_n = distances / mean_dist r_bin_edges = np.logspace(np.log10(r_inner), np.log10(r_outer), nbins_r) r_array_q = np.zeros(len(points)) for m in xrange(nbins_r): r_array_q += (r_array_n < r_bin_edges[m]) fz = r_array_q > 0 def _get_angles(self, x): result = zeros((len(x), len(x))) for i in xrange(len(x)): for j in xrange(len(x)): result[i, j] = get_angle(x[i], x[j]) return result theta_array = np.array([get_angle(pt, p) for p in points]) # 2Pi shifted theta_array_2 = theta_array + 2 * math.pi * (theta_array < 0) theta_array_q = 1 + np.floor(theta_array_2 / (2 * math.pi / nbins_theta)) sn = np.zeros((nbins_r, nbins_theta)) for j in xrange(len(points)): if (fz[j]): sn[r_array_q[j] - 1, theta_array_q[j] - 1] += 1 return sn.reshape(nbins) rsi = binary_shape(pc, xr, yr, groups) pixels = rsi.pixels.squeeze() pts = np.argwhere(pixels > 0) if sampls: pts = pts[np.random.randint(0, pts.shape[0], sampls)] mean_dist = dist([0, 0], [xr, yr]) / 2 sc = np.zeros((xr, yr, nbins)) for x in xrange(xr): for y in xrange(yr): sc[x, y, :] = compute_one(np.array([x, y]), pts, mean_dist) return Image.init_from_rolled_channels(sc)
def distance_transform_shape(pc, xr, yr, groups=None): rsi = binary_shape(pc, xr, yr, groups) ret = distance_transform_edt(rsi.rolled_channels().squeeze()) return Image.init_from_rolled_channels(ret.T)
def _rasterize_pillow(image, pclouds, render_lines=True, line_style='-', line_colour='b', line_width=1, render_markers=True, marker_style='o', marker_size=1, marker_face_colour='b', marker_edge_colour='b', marker_edge_width=1): from PIL import ImageDraw if any(x != '-' for x in line_style): raise ValueError("The Pillow rasterizer only supports the '-' " "line style.") if any(x not in {'o', 's'} for x in marker_style): raise ValueError("The Pillow rasterizer only supports the 'o' and 's' " "marker styles.") if any(x > 1 for x in marker_edge_width): raise ValueError('The Pillow rasterizer only supports ' 'marker_edge_width of 1 or 0.') pil_im = image.as_PILImage() draw = ImageDraw.Draw(pil_im) line_colour = [_parse_colour(x) for x in line_colour] marker_edge_colour = [_parse_colour(x) for x in marker_edge_colour] marker_face_colour = [_parse_colour(x) for x in marker_face_colour] for k in range(len(pclouds)): p = pclouds[k] if isinstance(p, TriMesh): pclouds[k] = p.as_pointgraph() points = p.points if (render_lines[k] and line_width[k] > 0 and hasattr(p, 'edges') and p.edges.size > 0): edges = p.edges lines = zip(points[edges[:, 0], :], points[edges[:, 1], :]) for l1, l2 in lines: draw.line([tuple(l1[::-1]), tuple(l2[::-1])], fill=line_colour[k], width=line_width[k]) if render_markers[k] and marker_size[k] > 0: draw_func = (draw.ellipse if marker_style[k] == 'o' else draw.rectangle) outline = (marker_edge_colour[k] if marker_edge_width[k] == 1 else None) for p in points: y, x = p draw_func((x - marker_size[k], y - marker_size[k], x + marker_size[k], y + marker_size[k]), fill=marker_face_colour[k], outline=outline) del draw pixels = np.asarray(pil_im) if image.n_channels == 3: return Image.init_from_rolled_channels(pixels) else: return Image(pixels)