Python imgaug.imresize_single_image() Examples
The following are 30
code examples of imgaug.imresize_single_image().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
imgaug
, or try the search function
.
Example #1
Source File: common.py From cat-bbs with MIT License | 6 votes |
def draw_heatmap(img, heatmap, alpha=0.5): """Draw a heatmap overlay over an image.""" assert len(heatmap.shape) == 2 or \ (len(heatmap.shape) == 3 and heatmap.shape[2] == 1) assert img.dtype in [np.uint8, np.int32, np.int64] assert heatmap.dtype in [np.float32, np.float64] if img.shape[0:2] != heatmap.shape[0:2]: heatmap_rs = np.clip(heatmap * 255, 0, 255).astype(np.uint8) heatmap_rs = ia.imresize_single_image( heatmap_rs[..., np.newaxis], img.shape[0:2], interpolation="nearest" ) heatmap = np.squeeze(heatmap_rs) / 255.0 cmap = plt.get_cmap('jet') heatmap_cmapped = cmap(heatmap) heatmap_cmapped = np.delete(heatmap_cmapped, 3, 2) heatmap_cmapped = heatmap_cmapped * 255 mix = (1-alpha) * img + alpha * heatmap_cmapped mix = np.clip(mix, 0, 255).astype(np.uint8) return mix
Example #2
Source File: augmentation.py From open-solution-data-science-bowl-2018 with MIT License | 6 votes |
def _perspective_transform_augment_images(self, images, random_state, parents, hooks): result = images if not self.keep_size: result = list(result) matrices, max_heights, max_widths = self._create_matrices( [image.shape for image in images], random_state ) for i, (M, max_height, max_width) in enumerate(zip(matrices, max_heights, max_widths)): warped = cv2.warpPerspective(images[i], M, (max_width, max_height)) if warped.ndim == 2 and images[i].ndim == 3: warped = np.expand_dims(warped, 2) if self.keep_size: h, w = images[i].shape[0:2] warped = ia.imresize_single_image(warped, (h, w)) result[i] = warped return result
Example #3
Source File: check_background_augmentation.py From ViolenceDetection with Apache License 2.0 | 6 votes |
def load_images(): batch_size = 4 astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape) counter = 0 for i in range(10): batch_images = [] batch_kps = [] for b in range(batch_size): astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter,), color=[0, 255, 0], size=16) batch_images.append(astronaut_text) batch_kps.append(kps) counter += 1 batch = ia.Batch( images=np.array(batch_images, dtype=np.uint8), keypoints=batch_kps ) yield batch
Example #4
Source File: segmentation.py From imgaug with MIT License | 6 votes |
def _augment_single_image(self, image, random_state): rss = random_state.duplicate(2) orig_shape = image.shape image = _ensure_image_max_size(image, self.max_size, self.interpolation) cell_coordinates = self.points_sampler.sample_points([image], rss[0])[0] p_replace = self.p_replace.draw_samples((len(cell_coordinates),), rss[1]) replace_mask = (p_replace > 0.5) image_aug = segment_voronoi(image, cell_coordinates, replace_mask) if orig_shape != image_aug.shape: image_aug = ia.imresize_single_image( image_aug, orig_shape[0:2], interpolation=self.interpolation) return image_aug
Example #5
Source File: pooling.py From imgaug with MIT License | 6 votes |
def _augment_images_by_samples(self, images, samples): if not self.keep_size: images = list(images) kernel_sizes_h, kernel_sizes_w = samples gen = enumerate(zip(images, kernel_sizes_h, kernel_sizes_w)) for i, (image, ksize_h, ksize_w) in gen: if ksize_h >= 2 or ksize_w >= 2: image_pooled = self._pool_image( image, ksize_h, ksize_w) if self.keep_size: image_pooled = ia.imresize_single_image( image_pooled, image.shape[0:2]) images[i] = image_pooled return images # Added in 0.4.0.
Example #6
Source File: blend.py From imgaug with MIT License | 6 votes |
def _binarize_mask(cls, mask, arr_height, arr_width): # Average over channels, resize to heatmap/segmap array size # (+clip for cubic interpolation). We can use none-NN interpolation # for segmaps here as this is just the mask and not the segmap # array. mask_3d = np.atleast_3d(mask) # masks with zero-sized axes crash in np.average() and cannot be # upscaled in imresize_single_image() if mask.size == 0: mask_rs = np.zeros((arr_height, arr_width), dtype=np.float32) else: mask_avg = ( np.average(mask_3d, axis=2) if mask_3d.shape[2] > 0 else 1.0) mask_rs = ia.imresize_single_image(mask_avg, (arr_height, arr_width)) mask_arr = iadt.clip_(mask_rs, 0, 1.0) mask_arr_binarized = (mask_arr >= 0.5) return mask_arr_binarized # Added in 0.4.0.
Example #7
Source File: annotate_street_boundaries.py From self-driving-truck with MIT License | 6 votes |
def set_canvas_background(self, image): if self.background_label is None: # initialize background image label (first call) #img = self.current_state.screenshot_rs #bg_img_tk = numpy_to_tk_image(np.zeros(img.shape)) img_heatmap = self._generate_heatmap() img_heatmap_rs = ia.imresize_single_image(img_heatmap, (img_heatmap.shape[0]*ZOOM_FACTOR, img_heatmap.shape[1]*ZOOM_FACTOR), interpolation="nearest") bg_img_tk = numpy_to_tk_image(img_heatmap_rs) self.background_label = Tkinter.Label(self.canvas, image=bg_img_tk) self.background_label.place(x=0, y=0, relwidth=1, relheight=1, anchor=Tkinter.NW) self.background_label.image = bg_img_tk #print("image size", image.shape) #print("image height, width", image.to_array().shape) image_rs = ia.imresize_single_image(image, (image.shape[0]*ZOOM_FACTOR, image.shape[1]*ZOOM_FACTOR), interpolation="nearest") image_tk = numpy_to_tk_image(image_rs) self.background_label.configure(image=image_tk) self.background_label.image = image_tk
Example #8
Source File: annotate_attributes.py From self-driving-truck with MIT License | 6 votes |
def set_canvas_background(self, image): if self.background_label is None: # initialize background image label (first call) #img = self.current_state.screenshot_rs #bg_img_tk = numpy_to_tk_image(np.zeros(img.shape)) img_heatmap = self._generate_heatmap() img_heatmap_rs = ia.imresize_single_image(img_heatmap, (img_heatmap.shape[0]*self.zoom_factor, img_heatmap.shape[1]*self.zoom_factor), interpolation="nearest") bg_img_tk = numpy_to_tk_image(img_heatmap_rs) self.background_label = Tkinter.Label(self.canvas, image=bg_img_tk) self.background_label.place(x=0, y=0, relwidth=1, relheight=1, anchor=Tkinter.NW) self.background_label.image = bg_img_tk #print("image size", image.shape) #print("image height, width", image.to_array().shape) image_rs = ia.imresize_single_image(image, (image.shape[0]*self.zoom_factor, image.shape[1]*self.zoom_factor), interpolation="nearest") image_tk = numpy_to_tk_image(image_rs) self.background_label.configure(image=image_tk) self.background_label.image = image_tk
Example #9
Source File: common.py From self-driving-truck with MIT License | 6 votes |
def set_canvas_background(self, image): if self.background_label is None: # initialize background image label (first call) #img = self.current_state.screenshot_rs #bg_img_tk = numpy_to_tk_image(np.zeros(img.shape)) img_heatmap = self._generate_heatmap() img_heatmap_rs = ia.imresize_single_image(img_heatmap, (img_heatmap.shape[0]*self.zoom_factor, img_heatmap.shape[1]*self.zoom_factor), interpolation="nearest") bg_img_tk = numpy_to_tk_image(img_heatmap_rs) self.background_label = Tkinter.Label(self.canvas, image=bg_img_tk) self.background_label.place(x=0, y=0, relwidth=1, relheight=1, anchor=Tkinter.NW) self.background_label.image = bg_img_tk #print("image size", image.shape) #print("image height, width", image.to_array().shape) image_rs = ia.imresize_single_image(image, (image.shape[0]*self.zoom_factor, image.shape[1]*self.zoom_factor), interpolation="nearest") image_tk = numpy_to_tk_image(image_rs) self.background_label.configure(image=image_tk) self.background_label.image = image_tk
Example #10
Source File: util.py From self-driving-truck with MIT License | 6 votes |
def draw_heatmap_overlay(img, heatmap, alpha=0.5): #assert img.shape[0:2] == heatmap.shape[0:2] assert len(heatmap.shape) == 2 or (heatmap.ndim == 3 and heatmap.shape[2] == 1) assert img.dtype in [np.uint8, np.int32, np.int64] assert heatmap.dtype in [np.float32, np.float64] if heatmap.ndim == 3 and heatmap.shape[2] == 1: heatmap = np.squeeze(heatmap) if img.shape[0:2] != heatmap.shape[0:2]: heatmap_rs = np.clip(heatmap * 255, 0, 255).astype(np.uint8) heatmap_rs = ia.imresize_single_image(heatmap_rs[..., np.newaxis], img.shape[0:2], interpolation="nearest") heatmap = np.squeeze(heatmap_rs) / 255.0 cmap = plt.get_cmap('jet') heatmap_cmapped = cmap(heatmap) #img_heatmaps_cmapped = img_heatmaps_cmapped[:, :, 0:3] heatmap_cmapped = np.delete(heatmap_cmapped, 3, 2) #heatmap_cmapped = np.clip(heatmap_cmapped * 255, 0, 255).astype(np.uint8) heatmap_cmapped = heatmap_cmapped * 255 mix = (1-alpha) * img + alpha * heatmap_cmapped mix = np.clip(mix, 0, 255).astype(np.uint8) return mix
Example #11
Source File: check_rain.py From imgaug with MIT License | 6 votes |
def main(): augs = [ iaa.Rain(speed=(0.1, 0.3)), iaa.Rain(), iaa.Rain(drop_size=(0.1, 0.2)) ] image = imageio.imread( ("https://upload.wikimedia.org/wikipedia/commons/8/89/" "Kukle%2CCzech_Republic..jpg"), format="jpg") for aug, size in zip(augs, [0.1, 0.2, 1.0]): image_rs = ia.imresize_single_image(image, size, "cubic") print(image_rs.shape) images_aug = aug.augment_images([image_rs] * 64) ia.imshow(ia.draw_grid(images_aug))
Example #12
Source File: check_background_augmentation.py From imgaug with MIT License | 6 votes |
def load_images(n_batches=10, sleep=0.0): batch_size = 4 astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape) counter = 0 for i in range(n_batches): batch_images = [] batch_kps = [] for b in range(batch_size): astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter,), color=[0, 255, 0], size=16) batch_images.append(astronaut_text) batch_kps.append(kps) counter += 1 batch = ia.Batch( images=np.array(batch_images, dtype=np.uint8), keypoints=batch_kps ) yield batch if sleep > 0: time.sleep(sleep)
Example #13
Source File: visualization.py From self-driving-truck with MIT License | 5 votes |
def output_grid_to_image(output_grid, upscale_factor=(4, 4)): if output_grid is None: grid_vis = np.zeros((Config.MODEL_NB_REWARD_BINS, Config.MODEL_NB_FUTURE_BLOCKS), dtype=np.uint8) else: if output_grid.ndim == 3: output_grid = output_grid[0] grid_vis = (output_grid.transpose((1, 0)) * 255).astype(np.uint8) grid_vis = np.tile(grid_vis[:, :, np.newaxis], (1, 1, 3)) if output_grid is None: grid_vis[::2, ::2, :] = [255, 0, 0] grid_vis = ia.imresize_single_image(grid_vis, (grid_vis.shape[0]*upscale_factor[0], grid_vis.shape[1]*upscale_factor[1]), interpolation="nearest") grid_vis = np.pad(grid_vis, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=128) return grid_vis
Example #14
Source File: visualization.py From self-driving-truck with MIT License | 5 votes |
def downscale(im): return ia.imresize_single_image(im, (90, 160), interpolation="cubic")
Example #15
Source File: preprocess.py From deep-learning-for-document-dewarping with MIT License | 5 votes |
def resize_and_rotate(filename): img = cv2.imread(filename)[:, :, ::-1] print('image opened: ' + filename) img = rotate_image(img,90) img = ia.imresize_single_image(img, (1024, 2048)) cv2.imwrite(filename, img) print('image rotated and resized saved as: ' + filename)
Example #16
Source File: preprocess.py From deep-learning-for-document-dewarping with MIT License | 5 votes |
def upsample(args): filename, root = args img = imageio.imread(os.path.join(root, filename)) img = rotate_image(img,-90) img = ia.imresize_single_image(img, (4400, 3400)) cv2.imwrite(os.path.join(root, filename))
Example #17
Source File: check_bb_augmentation.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (HEIGHT, WIDTH)) kps = [] for y in range(NB_ROWS): ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1)) for x in range(NB_COLS): xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1)) kp = (xcoord, ycoord) kps.append(kp) kps = set(kps) kps = [ia.Keypoint(x=xcoord, y=ycoord) for (xcoord, ycoord) in kps] kps = ia.KeypointsOnImage(kps, shape=image.shape) bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2) bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape) seq = iaa.Affine(rotate=45) seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) kps_aug = seq_det.augment_keypoints([kps])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] image_before = np.copy(image) image_before = kps.draw_on_image(image_before) image_before = bbs.draw_on_image(image_before) image_after = np.copy(image_aug) image_after = kps_aug.draw_on_image(image_after) image_after = bbs_aug.draw_on_image(image_after) misc.imshow(np.hstack([image_before, image_after])) misc.imsave("bb_aug.jpg", np.hstack([image_before, image_after]))
Example #18
Source File: visualization.py From self-driving-truck with MIT License | 5 votes |
def draw_successor_dr_grid(outputs_dr_successors_preds, outputs_dr_successors_gt, upscale_factor=(4, 4)): T, S = outputs_dr_successors_preds.shape cols = [] for t in range(T): col = (outputs_dr_successors_preds[t][np.newaxis, :].transpose((1, 0)) * 255).astype(np.uint8) col = np.tile(col[:, :, np.newaxis], (1, 1, 3)) correct_bin_idx = np.argmax(outputs_dr_successors_gt[t]) col[correct_bin_idx, 0, 2] = 255 col = ia.imresize_single_image(col, (col.shape[0]*upscale_factor[0], col.shape[1]*upscale_factor[1]), interpolation="nearest") col = np.pad(col, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=128) cols.append(col) return np.hstack(cols)
Example #19
Source File: batching.py From self-driving-truck with MIT License | 5 votes |
def downscale(im, h, w): """Downscale one or more images to size (h, w).""" if isinstance(im, list): return [downscale(i, h, w) for i in im] else: if im.ndim == 2: im = im[:, :, np.newaxis] im_rs = ia.imresize_single_image(im, (h, w), interpolation="cubic") return np.squeeze(im) else: return ia.imresize_single_image(im, (h, w), interpolation="cubic")
Example #20
Source File: check_average_blur.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (64, 64)) print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) k = [ 1, 2, 4, 8, 16, (8, 8), (1, 8), ((1, 1), (8, 8)), ((1, 16), (1, 16)), ((1, 16), 1) ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, 64) #cv2.imshow("aug", image[..., ::-1]) #cv2.waitKey(TIME_PER_STEP) for ki in k: aug = iaa.AverageBlur(k=ki) img_aug = [aug.augment_image(image) for _ in range(NB_AUGS_PER_IMAGE)] img_aug = np.hstack(img_aug) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim-1)))) #print("dtype", img_aug.dtype, "averages", img_aug.mean(axis=range(1, img_aug.ndim))) title = "k=%s" % (str(ki),) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
Example #21
Source File: models.py From self-driving-truck with MIT License | 5 votes |
def downscale_prev(self, img): return ia.imresize_single_image(img, (train.MODEL_PREV_HEIGHT, train.MODEL_PREV_WIDTH), interpolation="cubic")
Example #22
Source File: generate_documentation_images.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def chapter_examples_bounding_boxes_projection(): import imgaug as ia from imgaug import augmenters as iaa ia.seed(1) # Define image with two bounding boxes image = ia.quokka(size=(256, 256)) bbs = ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=25, x2=75, y1=25, y2=75), ia.BoundingBox(x1=100, x2=150, y1=25, y2=75) ], shape=image.shape) # Rescale image and bounding boxes image_rescaled = ia.imresize_single_image(image, (512, 512)) bbs_rescaled = bbs.on(image_rescaled) # Draw image before/after rescaling and with rescaled bounding boxes image_bbs = bbs.draw_on_image(image, thickness=2) image_rescaled_bbs = bbs_rescaled.draw_on_image(image_rescaled, thickness=2) # ------------ save( "examples_bounding_boxes", "projection.jpg", grid([image_bbs, image_rescaled_bbs], cols=2, rows=1), quality=75 )
Example #23
Source File: generate_example_images.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def add_row(self, title, images, subtitles): assert len(images) == len(subtitles) images_rs = [] for image in images: images_rs.append(ia.imresize_single_image(image, (self.image_height, self.image_width))) self.rows.append((title, images_rs, subtitles))
Example #24
Source File: test_readme_examples.py From ViolenceDetection with Apache License 2.0 | 5 votes |
def example_background_augment_batches(): print("Example: Background Augmentation via augment_batches()") import imgaug as ia from imgaug import augmenters as iaa import numpy as np from skimage import data # Number of batches and batch size for this example nb_batches = 10 batch_size = 32 # Example augmentation sequence to run in the background augseq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1) ]) # For simplicity, we use the same image here many times astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) # Make batches out of the example image (here: 10 batches, each 32 times # the example image) batches = [] for _ in range(nb_batches): batches.append( np.array( [astronaut for _ in range(batch_size)], dtype=np.uint8 ) ) # Show the augmented images. # Note that augment_batches() returns a generator. for images_aug in augseq.augment_batches(batches, background=True): misc.imshow(ia.draw_grid(images_aug, cols=8))
Example #25
Source File: test_segmaps.py From imgaug with MIT License | 5 votes |
def test_use_size_arg_to_keep_at_same_size(self): # same example, keeps size at 3x3 via None and (int)3 or (float)1.0 size_args = [ None, (None, None), (3, None), (None, 3), (1.0, None), (None, 1.0) ] col0 = self.col(0) col1 = self.col(1) expected = np.uint8([ [col0, col1, col1], [col0, col1, col1], [col0, col1, col1] ]) expected = ia.imresize_single_image(expected, (3, 3), interpolation="nearest") for size_arg in size_args: with self.subTest(size=size_arg): observed = self.segmap.draw(size=size_arg) assert isinstance(observed, list) assert len(observed) == 1 assert np.array_equal(observed[0], expected)
Example #26
Source File: train.py From self-driving-truck with MIT License | 5 votes |
def generate_debug_image(inputs, outputs_gt, outputs_pred): """Draw an image with current ground truth and predictions for debug purposes.""" current_image = inputs.data[0].cpu().numpy() current_image = np.clip(current_image * 255, 0, 255).astype(np.uint8).transpose((1, 2, 0)) current_image = ia.imresize_single_image(current_image, (32*4, 64*4)) h, w = current_image.shape[0:2] outputs_gt = to_numpy(outputs_gt)[0] outputs_pred = to_numpy(outputs_pred)[0] binwidth = 6 outputs_grid = np.zeros((20+2, outputs_gt.shape[0]*binwidth, 3), dtype=np.uint8) for angle_bin_idx in xrange(outputs_gt.shape[0]): val = outputs_pred[angle_bin_idx] x_start = angle_bin_idx*binwidth x_end = (angle_bin_idx+1)*binwidth fill_start = 1 fill_end = 1 + int(20*val) #print(angle_bin_idx, x_start, x_end, fill_start, fill_end, outputs_grid.shape, outputs_grid[fill_start:fill_end, x_start+1:x_end].shape) if fill_start < fill_end: outputs_grid[fill_start:fill_end, x_start+1:x_end] = [255, 255, 255] bordercol = [128, 128, 128] if outputs_gt[angle_bin_idx] < 1 else [0, 0, 255] outputs_grid[0:22, x_start:x_start+1] = bordercol outputs_grid[0:22, x_end:x_end+1] = bordercol outputs_grid[0, x_start:x_end+1] = bordercol outputs_grid[21, x_start:x_end+1] = bordercol outputs_grid = outputs_grid[::-1, :, :] bin_gt = np.argmax(outputs_gt) bin_pred = np.argmax(outputs_pred) angles = [(binidx*ANGLE_BIN_SIZE) - 180 for binidx in [bin_gt, bin_pred]] #print(outputs_grid.shape) current_image = np.pad(current_image, ((0, 128), (0, 400), (0, 0)), mode="constant") current_image[h+4:h+4+22, 4:4+outputs_grid.shape[1], :] = outputs_grid current_image = util.draw_text(current_image, x=4, y=h+4+22+4, text="GT: %03.2fdeg\nPR: %03.2fdeg" % (angles[0], angles[1]), size=10) return current_image
Example #27
Source File: train.py From self-driving-truck with MIT License | 5 votes |
def downscale_image(steering_wheel_image): """Downscale an image to the model's input sizes (height, width).""" return ia.imresize_single_image( steering_wheel_image, (MODEL_HEIGHT, MODEL_WIDTH), interpolation="linear" )
Example #28
Source File: models.py From self-driving-truck with MIT License | 5 votes |
def downscale(self, img): return ia.imresize_single_image(img, (train.MODEL_HEIGHT, train.MODEL_WIDTH), interpolation="cubic")
Example #29
Source File: models.py From self-driving-truck with MIT License | 5 votes |
def downscale_prev(self, img): return ia.imresize_single_image(img, (train.MODEL_PREV_HEIGHT, train.MODEL_PREV_WIDTH), interpolation="cubic")
Example #30
Source File: check_snowflakes.py From imgaug with MIT License | 5 votes |
def main(): for size in [0.1, 0.2, 1.0]: image = imageio.imread("https://upload.wikimedia.org/wikipedia/commons/8/89/Kukle%2CCzech_Republic..jpg", format="jpg") image = ia.imresize_single_image(image, size, "cubic") print(image.shape) augs = [ ("iaa.Snowflakes()", iaa.Snowflakes()) ] for descr, aug in augs: print(descr) images_aug = aug.augment_images([image] * 64) ia.imshow(ia.draw_grid(images_aug))