def drawRectangle(image, rec): rr, cc = line(rec[0], rec[1], rec[0], rec[3]) image[rr, cc] = 1 rr, cc = line(rec[0], rec[3], rec[2], rec[3]) image[rr, cc] = 1 rr, cc = line(rec[2], rec[3], rec[2], rec[1]) image[rr, cc] = 1 rr, cc = line(rec[2], rec[1], rec[0], rec[1]) image[rr, cc] = 1
def drawRectangle(image, rec): rec = list(rec) if rec[1] >= image.shape[1]: rec[1] = image.shape[1] - 1 if rec[3] >= image.shape[1]: rec[3] = image.shape[1] - 1 if rec[0] >= image.shape[0]: rec[0] = image.shape[0] - 1 if rec[2] >= image.shape[0]: rec[2] = image.shape[0] - 1 rr, cc = line(rec[0], rec[1], rec[0], rec[3]) image[rr, cc] = 1 rr, cc = line(rec[0], rec[3], rec[2], rec[3]) image[rr, cc] = 1 rr, cc = line(rec[2], rec[3], rec[2], rec[1]) image[rr, cc] = 1 rr, cc = line(rec[2], rec[1], rec[0], rec[1]) image[rr, cc] = 1
def polygon_perimeter(cr, cc, shape=None, clip=False): """Generate polygon perimeter coordinates. Parameters ---------- cr : (N,) ndarray Row (Y) coordinates of vertices of polygon. cc : (N,) ndarray Column (X) coordinates of vertices of polygon. shape : tuple, optional Image shape which is used to determine maximum extents of output pixel coordinates. This is useful for polygons which exceed the image size. By default the full extents of the polygon are used. clip : bool, optional Whether to clip the polygon to the provided shape. If this is set to True, the drawn figure will always be a closed polygon with all edges visible. Returns ------- pr, pc : ndarray of int Pixel coordinates of polygon. May be used to directly index into an array, e.g. ``img[pr, pc] = 1``. """ if clip: if shape is None: raise ValueError("Must specify clipping shape") clip_box = array([0, 0, shape[0] - 1, shape[1] - 1]) else: clip_box = array([min(cr), min(cc), max(cr), max(cc)]) # Do the clipping irrespective of whether clip is set. This # ensures that the returned polygon is closed and is an array. cr, cc = polygon_clip(cr, cc, *clip_box) cr = round(cr).astype(int) cc = round(cc).astype(int) # Construct line segments pr, pc = [], [] for i in range(len(cr) - 1): line_r, line_c = line(cr[i], cc[i], cr[i + 1], cc[i + 1]) pr.extend(line_r) pc.extend(line_c) pr = asarray(pr) pc = asarray(pc) if shape is None: return pr, pc else: return _coords_inside_image(pr, pc, shape)
def connect_points(self): """ connect points with lines """ points = [] normed_coords = np.array(self.coords) normed_coords[0, :] = normed_coords[0, :] * 10.0 / self.norm_w normed_coords[1, :] = normed_coords[1, :] * 10.0 / self.norm_h normed_coords = normed_coords.astype(int) prev_coords = normed_coords[:, 0] for idx in range(1, normed_coords.shape[1]): rr, cc = line(prev_coords[0], prev_coords[1], normed_coords[0, idx], normed_coords[1, idx]) points.append([rr, cc]) prev_coords = normed_coords[:, idx] if points == []: return normed_coords else: return np.concatenate(points, axis=1)
def get_local_image(self, trace_dict): """ generate a binary image of this symbol """ coords = None trace_id_map = [] counter = 0 for trace in self.traces: if coords is None: coords = np.array(trace.coords) else: coords = np.concatenate((coords, trace.coords), axis=1) trace_id_map += [counter for i in range(trace.coords.shape[1])] counter += 1 #self.calc_cmy_asc_des() max_width = np.max(coords[0, :]) - np.min(coords[0, :]) max_height = np.max(coords[1, :]) - np.min(coords[1, :]) max_length = max(max_width, max_height) coords[0, :] = (coords[0, :] - np.min(coords[0, :])) * 25.0 // (max_length + 1e-12) coords[1, :] = (coords[1, :] - np.min(coords[1, :])) * 25.0 // (max_length + 1e-12) center_x = (np.max(coords[0, :]) - np.min(coords[0, :])) // 2 center_y = (np.max(coords[1, :]) - np.min(coords[1, :])) // 2 coords[0, :] = coords[0, :] - center_x + 16 coords[1, :] = coords[1, :] - center_y + 16 coords = coords.astype(np.int) prev_id = -1 image = np.zeros((32, 32), dtype=np.uint8) for idx in range(coords.shape[1]): if prev_id == trace_id_map[idx]: rr, cc = line(prev_x, prev_y, coords[0, idx], coords[1, idx]) image[cc, rr] = 255 prev_id = trace_id_map[idx] prev_x = coords[0, idx] prev_y = coords[1, idx] return image
def draw_hog(orientation_histogram, w_sz_x, w_sz_y, c_sz_x, c_sz_y, n_bins): orientations = n_bins n_cellsx = int(np.floor(w_sz_x // c_sz_x)) # number of cells in x n_cellsy = int(np.floor(w_sz_y // c_sz_y)) # number of cells in y radius = min(c_sz_x, c_sz_y) // 2 - 1 # - 1 hog_image = np.zeros((w_sz_x, w_sz_y), dtype=float) # for x in range(n_cellsx): # for y in range(n_cellsy): for y in range(n_cellsy): for x in range(n_cellsx): for o in range(orientations): centre = tuple([y * c_sz_y + c_sz_y // 2, x * c_sz_x + c_sz_x // 2]) # dx = radius * scipy.cos(float(o) / orientations * np.pi) # dy = radius * scipy.sin(float(o) / orientations * np.pi) dy = radius * scipy.cos(float(o) / orientations * np.pi) dx = radius * scipy.sin(float(o) / orientations * np.pi) rr, cc = line(int(centre[0] - dx), int(centre[1] + dy), int(centre[0] + dx), int(centre[1] - dy)) hog_image[rr, cc] += orientation_histogram[y, x, o] return hog_image
def visible(self, trace2, trace_map): """ calculate if it's visible to trace2, which means no other traces block them """ rr1, cc1 = self.normed_connected_coords[0, :], self.normed_connected_coords[1, :] # np.nonzero(trace_map == self.id) cd1 = np.vstack([rr1, cc1]) rr2, cc2 = trace2.normed_connected_coords[0, :], trace2.normed_connected_coords[1, :] # np.nonzero(trace_map == trace2.id) cd2 = np.vstack([rr2, cc2]) dist = distance.cdist(cd1.T, cd2.T, 'euclidean') x, y = np.unravel_index(np.argmin(dist), dist.shape) x1, y1 = cd1[:, x] x2, y2 = cd2[:, y] rr, cc = line(int(x1), int(y1), int(x2), int(y2)) for idx in range(len(rr)): pixels = (trace_map[rr[idx] - 1:rr[idx] + 2, cc[idx] - 1:cc[idx] + 1] != self.id) \ * (trace_map[rr[idx] - 1:rr[idx] + 2, cc[idx] - 1:cc[idx] + 1] != trace2.id) \ * (trace_map[rr[idx] - 1:rr[idx] + 2, cc[idx] - 1:cc[idx] + 1] != -1) if np.sum(pixels) != 0: return 0 return 1