def driving_grid(self): final_grid = np.copy(self.occupancy_grid) final_grid[final_grid > 0.4] = 1 final_grid[final_grid <= 0.4] = 0 rr, cc = rectangle_perimeter((2, 2), (77, 77), shape=final_grid.shape) final_grid[rr, cc] = 1 final_grid = pad_grid(final_grid, 1) rr, cc = rectangle((4, 4), (13, 13), shape=final_grid.shape) final_grid[rr, cc] = 0 rr, cc = rectangle((4, 65), (13, 74), shape=final_grid.shape) final_grid[rr, cc] = 0 reduced_grid = np.copy(final_grid) reduced_grid = block_padding(self.blocks, reduced_grid, size=5) final_grid = block_padding(self.blocks, final_grid) # rr, cc = ellipse(int(other_bot[0]) , int(other_bot[1]), 9,9, shape=final_grid.shape) # for i in range(len(rr)): # final_grid[bound(rr[i]), bound(cc[i])] = 1 return final_grid, reduced_grid
def explore_uncertainty(self): flattened = np.copy(self.occupancy_grid) flattened = flattened.flatten() avg_uncertainty = np.average(flattened * np.log2(1 / flattened)) uncertainty_grid = self.occupancy_grid * np.log2( 1 / self.occupancy_grid) uncertainty_grid[uncertainty_grid > 0.3] = 1 uncertainty_grid[uncertainty_grid <= 0.3] = 0 # detect on uncertainty grid rr, cc = rectangle_perimeter((0, 0), (79, 79), shape=uncertainty_grid.shape) uncertainty_grid[rr, cc] = 0 rr, cc = rectangle((0, 0), (13, 13), shape=uncertainty_grid.shape) uncertainty_grid[rr, cc] = 0 rr, cc = rectangle((0, 65), (13, 79), shape=uncertainty_grid.shape) uncertainty_grid[rr, cc] = 0 uncertainty_grid = gaussian(uncertainty_grid, sigma=0.8) coords = np.where(uncertainty_grid > 0.2) if len(coords) > 0: explore_coordinate = np.average(coords, weights=uncertainty_grid[coords], axis=1) return explore_coordinate else: return None
def test_rectangle_extent_negative(): # These two tests should be done together. expected = np.array([[0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1], [0, 0, 1, 2, 2, 1], [0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]], dtype=np.uint8) start = (3, 5) extent = (-1, -2) img = np.zeros(expected.shape, dtype=np.uint8) rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape) img[rr, cc] = 1 rr, cc = rectangle(start, extent=extent, shape=img.shape) img[rr, cc] = 2 assert_array_equal(img, expected) # Ensure that rr and cc have no overlap img = np.zeros(expected.shape, dtype=np.uint8) rr, cc = rectangle(start, extent=extent, shape=img.shape) img[rr, cc] = 2 rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected)
def draw_bboxes_withindex(img, boxes, uids): """ A helper function to draw bounding box rectangles on images Args: img: image to be drawn on in array format boxes: An (N,4) array of bounding boxes Output: Image with drawn bounding boxes """ source = Image.fromarray(img) draw = ImageDraw.Draw(source) w2, h2 = (img.shape[0], img.shape[1]) font = ImageFont.truetype( '/usr/share/fonts/truetype/freefont/FreeSerif.ttf', 40) #font = ImageFont.truetype('arial.ttf', 24) idx = 0 for b in boxes: xmin, ymin, xmax, ymax = b for j in range(3): draw.rectangle(((xmin + j, ymin + j), (xmax + j, ymax + j)), outline="red") draw.text((xmin + 20, ymin + 70), str(uids[idx]), font=font) idx += 1 return source
def generate_patch_info(self, sizx, sizy, ori): max_s = int(2*max(sizx, sizy)) patch = np.zeros((max_s, max_s)) patch_0 = np.zeros((max_s, max_s)) # patch with zero offset v_siz_w = 1 + sizx//4 v_siz_h = 1 + sizy//3 v_off_w = (1 + (sizx - v_siz_w)//3)*2 v_off_h = (1 + (sizy - v_siz_h)//6)*2 + v_siz_h//2 start1 = (int((max_s - v_off_h - v_siz_h)//2), int((max_s - v_off_w - v_siz_w)//2)) start2 = (int((max_s + v_off_h - v_siz_h)//2), int((max_s + v_off_w - v_siz_w)//2)) start01 = (int((max_s - v_off_h - v_siz_h)//2), int((max_s - 0 - v_siz_w)//2)) start02 = (int((max_s + v_off_h - v_siz_h)//2), int((max_s + 0 - v_siz_w)//2)) extent = (int(v_siz_h), int(v_siz_w)) rr1, cc1 = rectangle(start=start1, extent=extent, shape=patch.shape) rr2, cc2 = rectangle(start=start2, extent=extent, shape=patch.shape) rr01, cc01 = rectangle(start=start01, extent=extent, shape=patch.shape) rr02, cc02 = rectangle(start=start02, extent=extent, shape=patch.shape) patch[ rr1, cc1 ] = 255 patch[ rr2, cc2 ] = 255 patch_0[rr01, cc01] = 255 patch_0[rr02, cc02] = 255 patch = rotate(patch, ori).astype(int) patch_info = [patch, np.fliplr(patch), patch_0] return patch_info
def test_rectangle_end(): expected = np.array([[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) start = (0, 1) end = (3, 3) img = np.zeros((5, 5), dtype=np.uint8) rr, cc = rectangle(start, end=end, shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected) # Swap start and end img = np.zeros((5, 5), dtype=np.uint8) rr, cc = rectangle(end=start, start=end, shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected) # Bottom left and top right img = np.zeros((5, 5), dtype=np.uint8) rr, cc = rectangle(start=(3, 1), end=(0, 3), shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected) img = np.zeros((5, 5), dtype=np.uint8) rr, cc = rectangle(end=(3, 1), start=(0, 3), shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected)
def find_blocks(self): ''' finds blocks using the current occupancy grid args: None returns: numpy array: final grid - final grid showing detections list: detected_blocks - list of detected blocks ''' final_grid = gaussian(np.copy(self.occupancy_grid), sigma=0.2) final_grid[final_grid > 0.95] = 1 final_grid[final_grid <= 0.95] = 0 rr, cc = rectangle_perimeter((1, 1), (78, 78), shape=final_grid.shape) final_grid[rr, cc] = 0 rr, cc = rectangle((1, 1), (13, 13), shape=final_grid.shape) final_grid[rr, cc] = 0 rr, cc = rectangle((1, 65), (13, 78), shape=final_grid.shape) final_grid[rr, cc] = 0 final_grid = gaussian(final_grid, sigma=0.4) self.detected_blocks = blob_dog((final_grid), min_sigma=1.5, max_sigma=2.3, threshold=0.01, overlap=0.4) return final_grid, self.detected_blocks
def find_blocks(self): final_grid = gaussian(np.copy(self.occupancy_grid), sigma=0.2) final_grid[final_grid > 0.95] = 1 final_grid[final_grid <= 0.95] = 0 rr, cc = rectangle_perimeter((1, 1), (78, 78), shape=final_grid.shape) final_grid[rr, cc] = 0 rr, cc = rectangle((1, 1), (13, 13), shape=final_grid.shape) final_grid[rr, cc] = 0 rr, cc = rectangle((1, 65), (13, 78), shape=final_grid.shape) final_grid[rr, cc] = 0 final_grid = gaussian(final_grid, sigma=0.4) self.detected_blocks = blob_dog((final_grid), min_sigma=1.5, max_sigma=2.3, threshold=0.01, overlap=0.4) return final_grid, self.detected_blocks
def generate_position_image(element, position_converter): img = Image.new("RGB", (100, 100), "white") if isinstance(element, E): Ep = element.value elements = Ep.strip(" ()").split("+") elif isinstance(element, str): if element == "all": elements = position_converter.keys() else: Ep = element elements = Ep.strip(" ()").split("+") else: return img draw = ImageDraw.Draw(img) for el in elements: x_start, y_start, x_end, y_end = position_converter[el] draw.rectangle(((x_start, y_start), (x_end, y_end)), fill="gold", outline=True, width=1) draw.text((x_start, y_start), el, fill="black") draw.rectangle(((0, 0), (100 - 1, 100 - 1)), outline=True, width=1) return img
def t_maze(t_shape, thick): assert t_shape[0] % 2 == 0, 'top bar size should be even number for symmetric shape. ' x = np.ones([t_shape[1] + thick + 2, t_shape[0] + thick + 2], dtype=np.uint8) rr, cc = rectangle([1, 1], extent=[thick, t_shape[0] + thick]) x[rr, cc] = 0 rr, cc = rectangle([1 + thick, x.shape[1]//2 - thick//2], extent=[t_shape[1], thick]) x[rr, cc] = 0 return x
def _sample(): x = np.random.randint(width, size=4).tolist() y = np.random.randint(height, size=4).tolist() x.sort() y.sort() # sample intersected bboxes fake = Faker() if fake.pybool(): if fake.pybool(): ax0, bx0, ax1, bx1 = x else: bx0, ax0, bx1, ax1 = x if fake.pybool(): ay0, by0, ay1, by1 = y else: by0, ay0, by1, ay1 = y else: ax0, bx0, bx1, ax1 = x ay0, by0, by1, ay1 = y bboxes = [(ax0, ay0, ax1, ay1), (bx0, by0, bx1, by1)] # sample layers layers = [0, 3] shuffle(layers) im = Image.new("RGB", (width, width)) draw = ImageDraw.Draw(im) im_t = [np.array(im)] for layer, bbox in zip(layers, bboxes): x0, y0, x1, y1 = bbox if layer == 4: draw.text((x0, y0), fake.sentence(), fill=fake.hex_color()) elif layer == 3: f = os.path.join(photo_folder, files[fake.pyint(min=0, max=len(files) - 1)]) _im = Image.open(f).resize((x1 - x0, y1 - y0)) im.paste(_im, box=(x0, y0)) elif layer == 0: draw.rectangle((x0, y0, x1, y1), fill=fake.hex_color()) im_t.append(np.array(im)) # sample final layer: text x0 = np.random.randint(width / 2) y0 = np.random.randint(height / 2) text = fake.sentence() draw.text((x0, y0), text, fill=fake.hex_color()) w, h = draw.textsize(text) layers.append(4) bboxes.append((x0, y0, w, h)) ims = np.stack( [np.concatenate([x, np.array(im)], axis=2) for x in im_t]) return ims, np.array(layers), np.array(bboxes)
def draw_haar_feature(w1, w2, w3, h, angle): img = np.zeros((h, w1 + w2 + w3), dtype=np.uint8) rr, cc = draw.rectangle((0, 0), extent=(h, w1), shape=img.shape) img[rr, cc] = BRIGHT_RECTANGLE rr, cc = draw.rectangle((0, w1), extent=(h, w2), shape=img.shape) img[rr, cc] = DARK_RECTANGLE if w3: rr, cc = draw.rectangle((0, w1 + w2), extent=(h, w3), shape=img.shape) img[rr, cc] = BRIGHT_RECTANGLE img = transform.rotate(img, 360 - angle, resize=True, preserve_range=False) return img
def ginput2boundingbox(a, b, mode='mask'): """ Takes the two corner points from the ginput of a rectangle and returns that rectangle in different forms :param a: (x_1,y_1)the first point :param b:(x_2,y_2) the second point :param mode: 'mask','verts',or 'patch' to define the output style :return: (rr,cc) if 'mask' or 'verts', (coord,h,w) if 'patch' """ pts = np.array([[a[0], a[1]], [b[0], b[1]], [a[0], b[1]], [b[0], a[1]]], dtype='int') bot_x = np.min(pts[:, 0]) bot_y = np.min(pts[:, 1]) top_x = np.max(pts[:, 0]) top_y = np.max(pts[:, 1]) if mode == 'mask': rr, cc = draw.rectangle((bot_x, bot_y), end=(top_x, top_y)) elif mode == 'verts': rr = np.array([bot_x, bot_x, top_x, top_x]) cc = np.array([bot_y, top_y, top_y, bot_y]) elif mode == 'patch': coord = (bot_x, bot_y) w = top_x - bot_x h = top_y - bot_y return (coord, h, w) else: raise ValueError( "Unknown mode of output requested. Must be 'mask' or 'verts'") return (cc, rr)
def expand_bbox(box, expansion=0.5): """ accepts a solid rectangle ROI and expands it :param box: A bounding box in "mask" form. :param expansion: the percentage to expand each dimension by [0,1]. Default: 0.5 :return: """ if len(box) == 4: x_bds = np.array([box[1], box[3]]) y_bds = np.array([box[0], box[2]]) else: x_bds = np.array([np.min(box[1]), np.max(box[1])]) y_bds = np.array([np.min(box[0]), np.max(box[0])]) w = np.abs(np.diff(x_bds)) h = np.abs(np.diff(y_bds)) if type(expansion) is list: pad_w = expansion[0] pad_h = expansion[1] else: pad_w = int(w * expansion / 2) pad_h = int(h * expansion / 2) x_bds += [-pad_w, pad_w] y_bds += [-pad_h, pad_h] # check boundary of im x_bds[x_bds < 0] = 0 y_bds[y_bds < 0] = 0 return (draw.rectangle((y_bds[0], x_bds[0]), (y_bds[1], x_bds[1])))
def _highlight(self, image): """ Test to make sure we're augmenting in the right place """ print("Area shape: {}".format( (self.area_shape[0], self.area_shape[1]))) rr, cc = draw.rectangle( (0, 0), end=(self.area_shape[0] - 1, self.area_shape[1] - 1)) image[rr, cc, :] = (0.5, 0.5, 0.5)
def bbox_to_map(x1, y1, x2, y2): bbmap = np.zeros((224, 224, 1), np.float32) start = (int(y1 * 224), int(x1 * 224)) end = (int(y2 * 224), int(x2 * 224)) rr, cc = draw.rectangle(start, end=end, shape=(224, 224)) bbmap[rr, cc] = 1 return bbmap
def QS(self, width, height, period, sep, n): """WRITE GRATINGS INTO ARRAY --- n is the number of gratings either side of the central stop --- sep is the width of the central stop""" self.width = width self.height = height self.period = np.around(period / (self.resolution), decimals=0) self.sep = np.around(sep / (self.resolution * 2), decimals=0) self.n = n rx = np.around(self.width / (self.resolution), decimals=0) ry = np.around(self.height / (self.resolution), decimals=0) for i in range(self.n): # NEGATIVE GRATINGS start = (self.C - ry, self.C - self.sep - (i + 1) * (self.period + rx) - rx) end = (self.C + ry, self.C - self.sep - (i + 1) * (self.period + rx) - rx) r, c = draw.rectangle(start=start, end=end, shape=self.array.shape) self.array[r.astype(dtype=np.int16), c.astype(dtype=np.int16)] = 1 self.array += np.rot90(np.rot90(self.array)) self.array += np.rot90(self.array) plt.set_cmap("bone") plt.imshow(self.array) return self.array
def get_bbox_mask(data, N=513): """ input: img: np.array, image data: dict, json correspondant à l'image """ try: gt = data['annotations'] # on extrait les données de détections N = gt[0]['height'] except: gt = [] mask = np.zeros((N, N)) for tank_data in gt: bbox = tank_data['bbox'] rx, ry = rectangle(start=(bbox[1], bbox[0]), extent=(bbox[2], bbox[3]), shape=(N, N)) mask[np.int64(rx), np.int64(ry)] = 1 nb_tanks = len(list(gt)) return mask, nb_tanks
def _build_bbox_mask(self, image_id): # Create rectangular bounding box since we are doing object detection, not segmentation # desired dimension is [height, width, instance_count] img = self.image_info[image_id] mask = np.zeros([img["height"], img["width"], len(img["annotations"])], dtype=np.uint8) for i,(_,p) in enumerate(img["annotations"].iterrows()): # Create rectangular bounding box since we are doing object detection, not segmentation xmax = int(img["width"]*p['XMax']) xmin = int(img["width"]*p['XMin']) ymin = int(img["height"]*p['YMin']) ymax = int(img["height"]*p['YMax']) start = (ymin, xmin) #top left corner ... are coordinates reversed? end = (ymax, xmax) #height and width rr, cc = rectangle(start, end=end, shape=(img["height"],img["width"])) mask[rr, cc, i] = 1 # Return mask, and array of class IDs of each instance. return mask.astype(np.bool), np.array(img['annotations']['LabelID'].values, dtype=np.int32)
def test_rect_3d_end(): expected = np.array([[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], [[0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], [[0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], [[0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]], dtype=np.uint8) img = np.zeros((4, 5, 5), dtype=np.uint8) start = (1, 0, 2) end = (3, 2, 3) pp, rr, cc = rectangle(start, end=end, shape=img.shape) img[pp, rr, cc] = 1 assert_array_equal(img, expected)
def asarray(self, dtype=None, val=1, shape=None): """Return the Roi as a binary array. Arguments: dtype -- the desired dtype of the returned array (default: np.bool_) val -- the value of pixels indicating the Roi (default: 1) shape -- the desired shape of the returned array (default: largest coordinates + 1) """ import skimage.draw as skd if self.coords is None: return None if dtype is None: dtype = np.bool_ if shape is None: shape = (bbox['bottom'] + 1, bbox['right'] + 1) bbox = self.bbox arr = np.zeros(shape, dtype=dtype) if self._type == TYPE_RECT: rr, cc = skd.rectangle(start=(bbox['top'], bbox['left']), end=(bbox['bottom'], bbox['right']), shape=shape) elif self._type in (TYPE_POLYGON, TYPE_FREEHAND): rr, cc = skd.polygon(self.rows, self.cols, shape=shape) else: raise NotImplementedError(f"Array conversion for ROI type '{self._type}' is not implemented.") arr[rr, cc] = val return arr
def draw_rectangle(centre, w, h , contrast, N): rect=np.zeros((N,N),dtype=np.int8) start = tuple(np.subtract(centre,(w/2,h/2)).astype(int)) extent = tuple(np.add(centre,(w/2,h/2)).astype(int)) x,y = draw.rectangle(start, extent, shape=rect.shape) rect[x,y] = contrast return rect
def draw_square(centre, l, contrast, N): square=np.zeros((N,N), dtype=np.int8) start = tuple(np.subtract(centre,(l/2,l/2)).astype(int)) extent = tuple(np.add(centre,(l/2,l/2)).astype(int)) x,y = draw.rectangle(start, extent, shape=square.shape) square[x,y]=contrast return square
def get_input_tensor(last_layer_np, image, x, y): cand1_as_input = np.full((1000, 1000), 0) begin_big_x = x - 50 end_big_x = x + 50 begin_big_y = y - 50 end_big_y = y + 50 begin_small_x = x - 5 end_small_x = x + 5 begin_small_y = y - 5 end_small_y = y + 5 rr, cc = draw.rectangle((begin_small_x, begin_small_y), extent=(11, 11)) cand1_as_input[rr, cc] = 1 cand1_as_input = cand1_as_input[np.ix_( np.arange(begin_big_x, end_big_x), np.arange(begin_big_y, end_big_y))] last_layer_np_cut = last_layer_np[np.ix_( np.arange(0, last_layer_np.shape[0]), np.arange(begin_big_x, end_big_x), np.arange(begin_big_y, end_big_y))] image_cut = image[np.ix_(np.arange(begin_big_x, end_big_x), np.arange(begin_big_y, end_big_y))] last_layer_tensor = torch.from_numpy( last_layer_np_cut.astype(np.float32)) imagetensor = torch.from_numpy(image_cut.astype( np.float32)).unsqueeze(0) cand1_as_input_tensor = torch.from_numpy( cand1_as_input.astype(np.float32)).unsqueeze(0) input_tensor2 = torch.cat( (imagetensor, last_layer_tensor, cand1_as_input_tensor), 0) return input_tensor2
def rectangle_mutation(self, img, msk, bbox): start = (bbox.y_min, bbox.x_min) end = (bbox.y_max, bbox.x_max) rr, cc = draw.rectangle(start, end=end, shape=msk.shape) if self.thin_iterations == 0: if self.binary_dilation_disk_size > 0: # do not dilate what we already have on the image tmp_msk = np.zeros_like(msk) tmp_msk[rr, cc] = 1 tmp_msk = self.binary_dilation_disk( tmp_msk, self.binary_dilation_disk_size) msk = msk + tmp_msk elif self.find_boundaries == True: tmp_msk = np.zeros_like(msk) tmp_msk[rr, cc] = 1 border_msk = find_boundaries(tmp_msk, mode='inner', background=0) # selem = square(3) # border_msk = binary_dilation(border_msk,selem) msk = msk + tmp_msk + border_msk.astype('uint8') else: msk[rr, cc] += 1 return msk else: tmp_msk = np.zeros_like(msk) tmp_msk[rr, cc] = 1 tmp_msk = self.thin_region_fast(tmp_msk, self.thin_iterations) msk += tmp_msk return msk
def render(self, mode='human'): """ Class that renders the position of the walker on the plane :param mode: :return: """ if mode == 'human': return None else: array = np.zeros(self.resolution + [3]) for goal in self.goals: pixel_coord = (goal * self.center) + self.center # print("Goal: {} - Coord: {}".format(goal, pixel_coord)) rr, cc = draw.rectangle(pixel_coord[0], pixel_coord[1], shape=array.shape) array[rr.astype(int), cc.astype(int)] = np.array([1, 0, 0]) pixel_coord = (self.pose * self.center) + self.center rr, cc = draw.circle(pixel_coord[0], pixel_coord[1], radius=2, shape=array.shape) array[rr, cc] = np.array([0, 0, 1]) # Paint circle in red return array
def set_pixels(image, bounding_boxes): image_with_boxes = np.copy(image) for box in bounding_boxes: Xmin, Xmax, Ymin, Ymax = box rr, cc = rectangle(start=(Ymin, Xmin), end=( Ymax, Xmax), shape=image.shape) image_with_boxes[rr, cc] = 1 # set color white return image_with_boxes
def cover_rectangle(self, start, end): ''' Function removes a specified rectangle region of the data. Use to remove the bleeding effects. ''' rr, cc = rectangle(start, extent=end) self.astro_flux[rr, cc] = 1
def square(top, left, width, image_width=301, amplitude=1): square = np.zeros((image_width, image_width)) xx, yy = rectangle((top, left), (top + width - 1, left + width - 1), shape=(image_width, image_width)) square[xx.astype(int), yy.astype(int)] = amplitude return square
def test_rectangle_extent(): expected = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) start = (1, 1) extent = (3, 3) img = np.zeros((5, 5), dtype=np.uint8) rr, cc = rectangle(start, extent=extent, shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected) img = np.zeros((5, 5, 3), dtype=np.uint8) rr, cc = rectangle(start, extent=extent, shape=img.shape) img[rr, cc, 0] = 1 expected_2 = np.zeros_like(img) expected_2[..., 0] = expected assert_array_equal(img, expected_2)
def draw_object(color=(128, 128, 128), rot_angle=0, flip=False, pos=(8, 8), image_size=(32, 32), size=(16, 16), chop_size=(12, 4)): img = np.zeros(image_size + (3, ), dtype=np.uint8) rr, cc = rectangle(start=pos, extent=size, shape=image_size) img[rr, cc] = color rr, cc = rectangle(start=pos, extent=chop_size, shape=image_size) img[rr, cc] = 0 if flip: img = img[::-1] return rotate(img, rot_angle).copy()
def test_rectangle_extent(): expected = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) img = np.zeros((5, 5), dtype=np.uint8) start = (1, 1) extent = (3, 3) rr, cc = rectangle(start, extent=extent, shape=img.shape) img[rr, cc] = 1 assert_array_equal(img, expected)