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
    def draw_frame_grids(self, scr, grids):
        grids_meta = [(0, "street boundaries"),
                      (3, "crashables (except cars)"), (7, "street markings"),
                      (4, "current lane"), (1, "cars"), (2, "cars in mirrors")]
        titles = [title for idx, title in grids_meta]
        grids = to_numpy(grids[0])
        grids = [grids[idx] for idx, title in grids_meta]
        #self.grid_to_graph(scr, grids[0])

        bgcolor = [0, 0, 0]
        image = np.zeros((720, 1280, 3), dtype=np.uint8) + bgcolor
        scr_main = ia.imresize_single_image(
            scr, (int(720 * 0.58), int(1280 * 0.58)))
        #util.draw_image(image, y=720-scr_main.shape[0], x=1080-scr_main.shape[1], other_img=scr_main, copy=False)
        util.draw_image(image,
                        y=int((image.shape[0] - scr_main.shape[0]) / 2),
                        x=1280 - scr_main.shape[1] - 2,
                        other_img=scr_main,
                        copy=False)
        image = util.draw_text(
            image,
            x=1280 - (scr_main.shape[1] // 2) - 125,
            y=image.shape[0] - int(
                (image.shape[0] - scr_main.shape[0]) / 2) + 10,
            text="Framerate matches the one that the model sees (10fps).",
            size=10,
            color=[128, 128, 128])

        grid_rel_size = 0.19
        scr_small = ia.imresize_single_image(
            scr, (int(720 * grid_rel_size), int(1280 * grid_rel_size)))
        grid_hms = []
        for grid, title in zip(grids, titles):
            grid = (grid * 255).astype(np.uint8)[:, :, np.newaxis]
            grid = ia.imresize_single_image(
                grid, (int(720 * grid_rel_size), int(1280 * grid_rel_size)),
                interpolation="nearest")
            grid_hm = util.draw_heatmap_overlay(scr_small, grid / 255)
            grid_hm = np.pad(grid_hm, ((2, 0), (2, 2), (0, 0)),
                             mode="constant",
                             constant_values=np.average(bgcolor))
            #grid_hm = np.pad(grid_hm, ((0, 20), (0, 0), (0, 0)), mode="constant", constant_values=0)
            #grid_hm[-20:, 2:-2, :] = [128, 128, 255]
            #grid_hm = util.draw_text(grid_hm, x=4, y=grid_hm.shape[0]-16, text=title, size=10, color=[255, 255, 255])
            grid_hm = np.pad(grid_hm, ((40, 0), (0, 0), (0, 0)),
                             mode="constant",
                             constant_values=0)
            grid_hm = util.draw_text(grid_hm,
                                     x=4,
                                     y=20,
                                     text=title,
                                     size=12,
                                     color=[255, 255, 255])
            grid_hms.append(grid_hm)
        grid_hms = ia.draw_grid(grid_hms, cols=2)

        util.draw_image(image, y=70, x=0, other_img=grid_hms, copy=False)

        return image
def screens_show_same_scene(scr1, scr2):
    scr1 = ia.imresize_single_image(scr1, (50, 70))
    scr2 = ia.imresize_single_image(scr2, (50, 70))
    hist1 = cv2.calcHist([scr1[..., 0], scr1[..., 1], scr1[..., 2]], [0, 1, 2],
                         None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
    hist2 = cv2.calcHist([scr2[..., 0], scr2[..., 1], scr2[..., 2]], [0, 1, 2],
                         None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
    diff = np.sum(np.abs(hist1 - hist2))
    return diff <= MAX_PIXELDIFF
Exemplo n.º 4
0
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")
Exemplo n.º 5
0
def do_augmentation(seqs, seq2_train, X_train, y_train):
    # Use seq_det to build augmentation.
    seq_det = seqs.to_deterministic()
    X_train_aug = seq2_train.augment_image(seq_det.augment_image(X_train))
    y_train_aug = seq_det.augment_image(y_train)

    if y_train_aug.shape != (101, 101):
        X_train_aug = ia.imresize_single_image(X_train_aug, (101, 101),
                                               interpolation="linear")
        y_train_aug = ia.imresize_single_image(y_train_aug, (101, 101),
                                               interpolation="nearest")
    return np.array(X_train_aug), np.array(y_train_aug)
Exemplo n.º 6
0
    def draw_on_image(self, image, random_state):
        flake_size_sample = self.flake_size.draw_sample(random_state)
        flake_size_uniformity_sample = self.flake_size_uniformity.draw_sample(
            random_state)
        angle_sample = self.angle.draw_sample(random_state)
        speed_sample = self.speed.draw_sample(random_state)
        blur_sigma_fraction_sample = self.blur_sigma_fraction.draw_sample(
            random_state)

        height, width = image.shape[0:2]
        downscale_factor = np.clip(1.0 - flake_size_sample, 0.001, 1.0)
        height_down = int(height*downscale_factor)
        width_down = int(width*downscale_factor)
        noise = self._generate_noise(
            height_down,
            width_down,
            self.density,
            ia.derive_random_state(random_state)
        )

        # gate the sampled noise via noise in range [0.0, 1.0]
        # this leads to less flakes in some areas of the image and more in
        # other areas
        gate_noise = iap.Beta(1.0, 1.0 - self.density_uniformity)
        noise = self._gate(noise, gate_noise, self.gate_noise_size,
                           ia.derive_random_state(random_state))
        noise = ia.imresize_single_image(noise, (height, width),
                                         interpolation="cubic")

        # apply a bit of gaussian blur and then motion blur according to
        # angle and speed
        sigma = max(height, width) * blur_sigma_fraction_sample
        sigma = np.clip(sigma,
                        self.blur_sigma_limits[0], self.blur_sigma_limits[1])
        noise_small_blur = self._blur(noise, sigma)
        noise_small_blur = self._motion_blur(noise_small_blur,
                                             angle=angle_sample,
                                             speed=speed_sample,
                                             random_state=random_state)

        # use contrast adjustment of noise to make the flake size a bit less
        # uniform then readjust the noise values to make them more visible
        # again
        gain = 1.0 + 2*(1 - flake_size_uniformity_sample)
        gain_adj = 1.0 + 5*(1 - flake_size_uniformity_sample)
        noise_small_blur = contrast.GammaContrast(gain).augment_image(
            noise_small_blur)
        noise_small_blur = noise_small_blur.astype(np.float32) * gain_adj
        noise_small_blur_rgb = np.tile(
            noise_small_blur[..., np.newaxis], (1, 1, 3))

        # blend:
        # sum for a bit of glowy, hardly visible flakes
        # max for the main flakes
        image_f32 = image.astype(np.float32)
        image_f32 = self._blend_by_sum(
            image_f32, (0.1 + 20*speed_sample) * noise_small_blur_rgb)
        image_f32 = self._blend_by_max(
            image_f32, (1.0 + 20*speed_sample) * noise_small_blur_rgb)
        return image_f32
Exemplo n.º 7
0
def load_images(n_batches=10, sleep=0.0, draw_text=True):
    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):
        if draw_text:
            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)
        else:
            if i == 0:
                batch_images = np.array(
                    [np.copy(astronaut) for _ in range(batch_size)],
                    dtype=np.uint8)

            batch = ia.Batch(
                images=np.copy(batch_images),
                keypoints=[kps.deepcopy() for _ in range(batch_size)])
        yield batch
        if sleep > 0:
            time.sleep(sleep)
Exemplo n.º 8
0
def find_bbs(img, model, conf_threshold, input_size):
    """Find bounding boxes in an image."""
    # pad image so that its square
    img_pad, (pad_top, pad_right, pad_bottom,
              pad_left) = to_aspect_ratio_add(img, 1.0, return_paddings=True)

    # resize padded image to desired input size
    # "linear" interpolation seems to be enough here for 400x400 or larger images
    # change to "area" or "cubic" for marginally better quality
    img_rs = ia.imresize_single_image(img_pad, (input_size, input_size),
                                      interpolation="linear")

    # convert to torch-ready input variable
    inputs_np = (np.array([img_rs]) / 255.0).astype(np.float32).transpose(
        0, 3, 1, 2)
    inputs = torch.from_numpy(inputs_np)
    inputs = Variable(inputs, volatile=True)
    if GPU >= 0:
        inputs = inputs.cuda(GPU)

    # apply model and measure the model's time
    time_start = time.time()
    outputs_pred = model(inputs)
    time_req = time.time() - time_start

    # process the model's output (i.e. convert heatmaps to BBs)
    result = ModelResult(outputs_pred, inputs_np, img,
                         (pad_top, pad_right, pad_bottom, pad_left))
    bbs = result.get_bbs()

    return bbs, time_req
Exemplo n.º 9
0
def example_multicore_augmentation():
    print("Example: Multicore Augmentation")
    import skimage.data
    import imgaug as ia
    import imgaug.augmenters as iaa
    from imgaug.augmentables.batches import UnnormalizedBatch

    # 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 = skimage.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(UnnormalizedBatch(images=[astronaut] * batch_size))

    # Show the augmented images.
    # Note that augment_batches() returns a generator.
    for images_aug in augseq.augment_batches(batches, background=True):
        ia.imshow(ia.draw_grid(images_aug.images_aug, cols=8))
Exemplo n.º 10
0
    def _augment_images(self, images, random_state, parents, hooks):
        result = []
        nb_images = len(images)
        height, width = self.dim[1], self.dim[0]
        pad_cval = self.pad_cval
        for i in sm.xrange(nb_images):
            image = images[i]
            # Calculate letterbox parameters
            resize_w, resize_h, x_pad, y_pad = self._compute_height_width_pad(
                image.shape, height, width)
            # Resize
            image_rs = ia.imresize_single_image(
                image, (resize_h, resize_w), interpolation=self.interpolation)
            # Add paddings
            pad_left, pad_right = x_pad, width - resize_w - x_pad
            pad_top, pad_bottom = y_pad, height - resize_h - y_pad
            if image_rs.ndim == 2:
                pad_vals = ((pad_top, pad_bottom), (pad_left, pad_right))
            else:
                pad_vals = ((pad_top, pad_bottom), (pad_left, pad_right), (0,
                                                                           0))
            image_cr_pa = np.pad(image_rs,
                                 pad_vals,
                                 mode="constant",
                                 constant_values=pad_cval)
            result.append(image_cr_pa)

        if not isinstance(images, list):
            all_same_size = (len(set([image.shape for image in result])) == 1)
            if all_same_size:
                result = np.array(result, dtype=np.uint8)
        return result
Exemplo n.º 11
0
    def load_batches():
        # Here, load 10 batches of size 4 each.
        # You can also load an infinite amount of batches, if you don't train
        # in epochs.
        batch_size = 4
        nb_batches = 10

        # Here, for simplicity we just always use the same image.
        astronaut = data.astronaut()
        astronaut = ia.imresize_single_image(astronaut, (64, 64))

        for i in range(nb_batches):
            # A list containing all images of the batch.
            batch_images = []
            # A list containing IDs per image. This is not necessary for the
            # background augmentation and here just used to showcase that you
            # can transfer additional information.
            batch_data = []

            # Add some images to the batch.
            for b in range(batch_size):
                batch_images.append(astronaut)
                batch_data.append((i, b))

            # Create the batch object to send to the background processes.
            batch = ia.Batch(
                images=np.array(batch_images, dtype=np.uint8),
                data=batch_data
            )

            yield batch
Exemplo n.º 12
0
    def Resize(it):
        """
        Resizes images according to the resize dimensions
        image: (array) Image
        resize: (tuple of integers) New dimensions
        target: (Dictionary) Containing BBox, Area, Labels and Index

        Returns normalized_image, target
        """
        image, target = it
        resize = res
        new_target = target.copy()
        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=new_target['boxes'][0].item(),
                        y1=new_target['boxes'][1].item(),
                        x2=new_target['boxes'][2].item(),
                        y2=new_target['boxes'][3].item())
        ], image.shape)
        img = ia.imresize_single_image(np.array(image), resize)
        new_bbs = bbs.on(img)
        bbox = torch.tensor(
            [new_bbs[0].x1, new_bbs[0].y1, new_bbs[0].x2, new_bbs[0].y2])
        new_target['area'] = torch.tensor([
            (new_bbs[0].x2 - new_bbs[0].x1) * (new_bbs[0].y2 - new_bbs[0].y1)
        ])
        new_target['boxes'] = bbox.unsqueeze(0)
        return img, [new_target]
Exemplo n.º 13
0
def load_images(n_batches=10, sleep=0.0, draw_text=True):
    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):
        if draw_text:
            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
            )
        else:
            if i == 0:
                batch_images = np.array([np.copy(astronaut) for _ in range(batch_size)], dtype=np.uint8)

            batch = ia.Batch(
                images=np.copy(batch_images),
                keypoints=[kps.deepcopy() for _ in range(batch_size)]
            )
        yield batch
        if sleep > 0:
            time.sleep(sleep)
Exemplo n.º 14
0
def chapter_examples_bounding_boxes_projection():
    import imgaug as ia
    from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage

    ia.seed(1)

    # Define image with two bounding boxes
    image = ia.quokka(size=(256, 256))
    bbs = BoundingBoxesOnImage([
        BoundingBox(x1=25, x2=75, y1=25, y2=75),
        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, size=2)
    image_rescaled_bbs = bbs_rescaled.draw_on_image(image_rescaled, size=2)

    # ------------

    save("examples_bounding_boxes",
         "projection.jpg",
         grid([image_bbs, image_rescaled_bbs], cols=2, rows=1),
         quality=90)
Exemplo n.º 15
0
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):
        ia.imshow(ia.draw_grid(images_aug, cols=8))
Exemplo n.º 16
0
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, 3, 5, 7, (3, 3), (1, 11)]

    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.MedianBlur(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)
Exemplo n.º 17
0
    def __getitem__(self, index):
        """Get the corresponding image and label

        # Arguments
            index [int]: the index of the file in the dataset

        # Returns
            [FloatTensor]: image of shape CxHxW
            [int]: the object class
        """
        filename = self.label[0][index]
        filepath = os.path.join(self.image_folder, filename)

        # load and transform image
        image = cv2.imread(filepath)
        if self.phase != 'test':
            image = augmenter.augment_image(image)
        image = ia.imresize_single_image(image, self.shape)
        image = image / 255
        image = (image - self.mean) / self.std
        image = image.transpose(2, 0, 1)
        image = torch.FloatTensor(image)

        # load label
        object_class = self.label[1][index].item() - 1

        return image, object_class
Exemplo n.º 18
0
 def _resize(self, image, bbs):
     image_rescaled = ia.imresize_single_image(image,
                                               sizes=(self.image_size,
                                                      self.image_size))
     bbs_rescaled = bbs.on(image_rescaled)
     return image_rescaled, bbs_rescaled.remove_out_of_image(
     ).clip_out_of_image()
Exemplo n.º 19
0
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"
    )
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
    def test_resize_image_to_segmentation_map(self):
        # resizing of image to segmap
        arr = np.int32([[0, 1, 1], [0, 1, 1], [0, 1, 1]])
        segmap = ia.SegmentationMapsOnImage(arr, shape=(1, 3))

        image = np.uint8([[0, 10, 20]])
        image = np.tile(image[:, :, np.newaxis], (1, 1, 3))
        image_rs = ia.imresize_single_image(image,
                                            arr.shape[0:2],
                                            interpolation="cubic")

        a1 = 0.7
        a0 = 1.0 - a1

        observed = segmap.draw_on_image(image,
                                        alpha=a1,
                                        draw_background=True,
                                        resize="image")

        col0 = self.col(0)
        col1 = self.col(1)
        expected = np.uint8([[col0, col1, col1], [col0, col1, col1],
                             [col0, col1, col1]])
        expected = a0 * image_rs + a1 * expected
        d_max = np.max(
            np.abs(observed[0].astype(np.float32) -
                   expected.astype(np.float32)))
        assert isinstance(observed, list)
        assert len(observed) == 1
        assert observed[0].shape == expected.shape
        assert d_max <= 1.0 + 1e-4
    def create_augimg(self, img, img_info):
        """
        Create an augmented image and key points.

        Parameters
            img: as numpy array
            img_info: tuple with label and keypoints

        Return
            augmented image and keypoints
        """
        label, points = img_info
        # print(points)
        kps_merge = [point for key_points in points for point in key_points]
        kps = KeypointsOnImage(kps_merge, shape=img.shape)
        # print(kps)
        if img.shape != self.dim:
            img = ia.imresize_single_image(img, self.dim[0:2])
            kps = kps.on(img)
        # Augment keypoints and images.
        img_aug, kps_aug = seq(image=img, keypoints=kps)
        aug_points = [[kp.x, kp.y] for kp in kps_aug.keypoints]
        aug_points_dic = {'label': label, 'points': aug_points}

        return img_aug, aug_points_dic
Exemplo n.º 23
0
    def test_use_size_arg_to_resize_to_2x(self):
        # same example, with resizing to 2x the size
        double_size_args = [
            (6, 6),
            (2.0, 2.0),
            6,
            2.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,
                                            (6, 6),
                                            interpolation="nearest")

        for double_size_arg in double_size_args:
            with self.subTest(size=double_size_arg):
                observed = self.segmap.draw(size=double_size_arg)

                assert isinstance(observed, list)
                assert len(observed) == 1
                assert np.array_equal(observed[0], expected)
Exemplo n.º 24
0
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,
        3,
        5,
        7,
        (3, 3),
        (1, 11)
    ]

    cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, 64)

    for ki in k:
        aug = iaa.MedianBlur(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))))

        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)
Exemplo n.º 25
0
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
Exemplo n.º 26
0
    def detect_cell(self, c: Candidate):
        """ Run detector on signle candidate and store results """
        print('processing', c)
        center = c.coords + self.img_disp
        csz = c.cell_size * 3
        ul_corner = center - csz
        dr_corner = center + csz

        if ul_corner[0] < 0 or ul_corner[1] < 0 or dr_corner[0] > self.img.shape[1] or dr_corner[1] > self.img.shape[0]:
            raise RuntimeError('Run out of bounds', ul_corner, dr_corner, self.img.shape)

        print(ul_corner, dr_corner)
        crop = self.img[int(round(ul_corner[1])):int(round(dr_corner[1])), int(round(ul_corner[0])):int(round(dr_corner[0])), :]

        cs = self.cell_detector.crop_size
        crop = ia.imresize_single_image(crop, sizes=(cs,cs))

        with torch.no_grad():
            inp = u.build_batch(crop).to(self.cell_detector.device)
            prediction = self.cell_detector.forward(inp)
            detection = Detection(idx=c.idx, prev_idx=c.prev_idx, zero=ul_corner - self.img_disp, crop_scales=(csz*2,csz*2),
                                  extractor=self.cell_detector.extract_data, prediction=prediction)

            if c.idx not in self.segment_map or self.segment_map[c.idx].confidence < detection.confidence:
                self.segment_map[c.idx] = detection

                dirs = self.guess_directions(detection)
                for i in range(4):
                    conf = detection.nb_confidences[i]
                    if conf > self.threshold:
                        new_idx = (c.idx[0] + dirs[i][0], c.idx[1] + dirs[i][1])
                        cand = Candidate(idx=new_idx , prev_idx=c.idx, coords=detection.neighs[i], priority=conf, cell_size = detection.estim_cell_size())
                        heapq.heappush(self.front_pq, cand)
Exemplo n.º 27
0
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)

    ia.imshow(np.hstack([image_before, image_after]))
    imageio.imwrite("bb_aug.jpg", np.hstack([image_before, image_after]))
Exemplo n.º 28
0
def background_process_augmentation_example_3():
    # Number of batches and batch size for this example.
    nb_batches = 10
    batch_size = 32
    nb_epochs = 5

    # 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))

    for epoch in range(nb_epochs):
        # Note that augment_batches() returns a generator.
        for idx, images_aug in enumerate(
                augseq.augment_batches(batches, background=True)):
            # Show the augmented images.
            #ia.imshow(ia.draw_grid(images_aug, cols=8))

            # Do something else with the batch here.
            print('Do something with batch ({}, {}).'.format(epoch, idx))
Exemplo n.º 29
0
def load_batches():
    # Here, load 10 batches of size 4 each.
    # You can also load an infinite amount of batches, if you don't train in epochs.
    batch_size = 4
    nb_batches = 10  #1000000000

    # Here, for simplicity we just always use the same image.
    astronaut = data.astronaut()
    astronaut = ia.imresize_single_image(astronaut, (64, 64))

    for i in range(nb_batches):
        # A list containing all images of the batch.
        batch_images = []
        # A list containing IDs of images in the batch. This is not necessary
        # for the background augmentation and here just used to showcase that
        # you can transfer additional information.
        batch_data = []

        # Add some images to the batch.
        for b in range(batch_size):
            batch_images.append(astronaut)
            batch_data.append((i, b))

        # Create the batch object to send to the background processes.
        batch = ia.Batch(images=np.array(batch_images, dtype=np.uint8),
                         data=batch_data)

        yield batch
Exemplo n.º 30
0
def process_image_detection(image, boxes, labels, desired_w, desired_h,
                            augment):

    # resize the image to standard size
    if (desired_w and desired_h) or augment:
        bbs = _to_bbs(boxes, labels, image.shape)

        if (desired_w and desired_h):
            # Rescale image and bounding boxes
            image = ia.imresize_single_image(image, (desired_w, desired_h))
            bbs = bbs.on(image)

        if augment:
            aug_pipe = _create_augment_pipeline()
            image, bbs = aug_pipe(image=image, bounding_boxes=bbs)
            bbs = bbs.remove_out_of_image().clip_out_of_image()

        new_boxes, new_labels = _to_array(bbs)
        #if len(new_boxes) != len(boxes):
        #    print(new_boxes)
        #    print(boxes)
        #    print("_________________")

        return image, np.array(new_boxes), new_labels
    else:
        return image, np.array(boxes), labels
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)
Exemplo n.º 32
0
 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))
Exemplo n.º 33
0
def chapter_augmenters_changecolortemperature():
    fn_start = "color/changecolortemperature"

    image = imageio.imread(
        os.path.join(INPUT_IMAGES_DIR, "Pahalgam_Valley.jpg"))
    image = ia.imresize_single_image(image, 0.2)

    aug = iaa.ChangeColorTemperature((1100, 10000))
    run_and_save_augseq(fn_start + ".jpg", aug, [image] * 8, cols=3, rows=2)
Exemplo n.º 34
0
def test_SegmentationMapOnImage_scale():
    arr = np.int32([
        [0, 1],
        [0, 2]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(2, 2), nb_classes=3)

    segmap_scaled = segmap.resize((4, 4))
    observed = segmap_scaled.arr
    expected = np.clip(ia.imresize_single_image(segmap.arr, (4, 4), interpolation="cubic"), 0, 1.0)
    assert np.allclose(observed, expected)
    assert np.array_equal(segmap_scaled.get_arr_int(), np.int32([
        [0, 0, 1, 1],
        [0, 0, 1, 1],
        [0, 0, 2, 2],
        [0, 0, 2, 2],
    ]))

    segmap_scaled = segmap.resize((4, 4), interpolation="nearest")
    observed = segmap_scaled.arr
    expected = ia.imresize_single_image(segmap.arr, (4, 4), interpolation="nearest")
    assert np.allclose(observed, expected)
    assert np.array_equal(segmap_scaled.get_arr_int(), np.int32([
        [0, 0, 1, 1],
        [0, 0, 1, 1],
        [0, 0, 2, 2],
        [0, 0, 2, 2],
    ]))

    segmap_scaled = segmap.resize(2.0)
    observed = segmap_scaled.arr
    expected = np.clip(ia.imresize_single_image(segmap.arr, 2.0, interpolation="cubic"), 0, 1.0)
    assert np.allclose(observed, expected)
    assert np.array_equal(segmap_scaled.get_arr_int(), np.int32([
        [0, 0, 1, 1],
        [0, 0, 1, 1],
        [0, 0, 2, 2],
        [0, 0, 2, 2],
    ]))
Exemplo n.º 35
0
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.Fog()", iaa.Fog())
        ]

        for descr, aug in augs:
            print(descr)
            images_aug = aug.augment_images([image] * 64)
            ia.imshow(ia.draw_grid(images_aug))
Exemplo n.º 36
0
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
Exemplo n.º 37
0
def main():
    img = data.astronaut()
    img = ia.imresize_single_image(img, (64, 64))
    aug = iaa.Fliplr(0.5)
    unseeded1 = aug.draw_grid(img, cols=8, rows=1)
    unseeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1000)
    seeded1 = aug.draw_grid(img, cols=8, rows=1)
    seeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1000)
    reseeded1 = aug.draw_grid(img, cols=8, rows=1)
    reseeded2 = aug.draw_grid(img, cols=8, rows=1)

    ia.seed(1001)
    reseeded3 = aug.draw_grid(img, cols=8, rows=1)
    reseeded4 = aug.draw_grid(img, cols=8, rows=1)

    all_rows = np.vstack([unseeded1, unseeded2, seeded1, seeded2, reseeded1, reseeded2, reseeded3, reseeded4])
    ia.imshow(all_rows)
Exemplo n.º 38
0
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (128, 128))
    print("image shape:", image.shape)
    print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP,))

    configs = [
        (1, 75, 75),
        (3, 75, 75),
        (5, 75, 75),
        (10, 75, 75),
        (10, 25, 25),
        (10, 250, 150),
        (15, 75, 75),
        (15, 150, 150),
        (15, 250, 150),
        (20, 75, 75),
        (40, 150, 150),
        ((1, 5), 75, 75),
        (5, (10, 250), 75),
        (5, 75, (10, 250)),
        (5, (10, 250), (10, 250)),
        (10, (10, 250), (10, 250)),
    ]

    cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("aug", 128*NB_AUGS_PER_IMAGE, 128)

    for (d, sigma_color, sigma_space) in configs:
        aug = iaa.BilateralBlur(d=d, sigma_color=sigma_color, sigma_space=sigma_space)

        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))))

        title = "d=%s, sc=%s, ss=%s" % (str(d), str(sigma_color), str(sigma_space))
        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)
Exemplo n.º 39
0
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):
        ia.imshow(ia.draw_grid(images_aug, cols=8))
Exemplo n.º 40
0
def test_determinism():
    reseed()

    images = [
        ia.quokka(size=(128, 128)),
        ia.quokka(size=(64, 64)),
        ia.imresize_single_image(skimage.data.astronaut(), (128, 256))
    ]
    images.extend([ia.quokka(size=(16, 16))] * 20)

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=20, y=10), ia.Keypoint(x=5, y=5),
            ia.Keypoint(x=10, y=43)], shape=(50, 60, 3))
    ] * 20

    augs = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))),
        # iaa.WithChannels([0], iaa.Add((-50, 50))),
        # iaa.Noop(name="Noop-nochange"),
        # iaa.Lambda(
        #     func_images=lambda images, random_state, parents, hooks: images,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images,
        #     name="Lambda-nochange"
        # ),
        # iaa.AssertLambda(
        #     func_images=lambda images, random_state, parents, hooks: True,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True,
        #     name="AssertLambda-nochange"
        # ),
        # iaa.AssertShape(
        #     (None, None, None, 3),
        #     check_keypoints=False,
        #     name="AssertShape-nochange"
        # ),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)),
        # iaa.ChangeColorspace(to_colorspace="GRAY"),
        iaa.Grayscale(alpha=(0.1, 1.0)),
        iaa.GaussianBlur((0.1, 3.0)),
        iaa.AverageBlur((3, 11)),
        iaa.MedianBlur((3, 11)),
        # iaa.Convolve(np.array([[0, 1, 0],
        #                       [1, -4, 1],
        #                       [0, 1, 0]])),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)),
        iaa.EdgeDetect(alpha=(0.1, 1.0)),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)),
        iaa.Add((-50, 50)),
        iaa.AddElementwise((-50, 50)),
        iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)),
        iaa.Multiply((0.6, 1.4)),
        iaa.MultiplyElementwise((0.6, 1.4)),
        iaa.Dropout((0.3, 0.5)),
        iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)),
        iaa.Invert(0.5),
        iaa.ContrastNormalization((0.6, 1.4)),
        iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20), shear=(-20, 20), order=ia.ALL,
                   mode=ia.ALL, cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=0.5)
    ]

    augs_affect_geometry = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20), shear=(-20, 20), order=ia.ALL,
                   mode=ia.ALL, cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5))
    ]

    for aug in augs:
        aug_det = aug.to_deterministic()
        images_aug1 = aug_det.augment_images(images)
        images_aug2 = aug_det.augment_images(images)

        aug_det = aug.to_deterministic()
        images_aug3 = aug_det.augment_images(images)
        images_aug4 = aug_det.augment_images(images)

        assert array_equal_lists(images_aug1, images_aug2), \
            "Images (1, 2) expected to be identical for %s" % (aug.name,)

        assert array_equal_lists(images_aug3, images_aug4), \
            "Images (3, 4) expected to be identical for %s" % (aug.name,)

        assert not array_equal_lists(images_aug1, images_aug3), \
            "Images (1, 3) expected to be different for %s" % (aug.name,)

    for aug in augs_affect_geometry:
        aug_det = aug.to_deterministic()
        kps_aug1 = aug_det.augment_keypoints(keypoints)
        kps_aug2 = aug_det.augment_keypoints(keypoints)

        aug_det = aug.to_deterministic()
        kps_aug3 = aug_det.augment_keypoints(keypoints)
        kps_aug4 = aug_det.augment_keypoints(keypoints)

        assert keypoints_equal(kps_aug1, kps_aug2), \
            "Keypoints (1, 2) expected to be identical for %s" % (aug.name,)

        assert keypoints_equal(kps_aug3, kps_aug4), \
            "Keypoints (3, 4) expected to be identical for %s" % (aug.name,)

        assert not keypoints_equal(kps_aug1, kps_aug3), \
            "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
Exemplo n.º 41
0
 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))
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (128, 128))

    # check if scipy and cv2 remap similarly
    rs = ia.new_random_state(1)
    aug_scipy = ElasticTransformationScipy(alpha=30, sigma=3, random_state=rs, deterministic=True)
    aug_cv2 = ElasticTransformationCv2(alpha=30, sigma=3, random_state=rs, deterministic=True)
    augs_scipy = aug_scipy.augment_images([image] * 8)
    augs_cv2 = aug_cv2.augment_images([image] * 8)
    ia.imshow(ia.draw_grid(augs_scipy + augs_cv2, rows=2))

    print("alpha=vary, sigma=0.25")
    augs = [iaa.ElasticTransformation(alpha=alpha, sigma=0.25) for alpha in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=1.0")
    augs = [iaa.ElasticTransformation(alpha=alpha, sigma=1.0) for alpha in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=3.0")
    augs = [iaa.ElasticTransformation(alpha=alpha, sigma=3.0) for alpha in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=5.0")
    augs = [iaa.ElasticTransformation(alpha=alpha, sigma=5.0) for alpha in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=1.0, sigma=vary")
    augs = [iaa.ElasticTransformation(alpha=1.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=10.0, sigma=vary")
    augs = [iaa.ElasticTransformation(alpha=10.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1)]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    kps = ia.KeypointsOnImage(
        [ia.Keypoint(x=1, y=1),
         ia.Keypoint(x=50, y=24), ia.Keypoint(x=42, y=96), ia.Keypoint(x=88, y=106), ia.Keypoint(x=88, y=53),
         ia.Keypoint(x=0, y=0), ia.Keypoint(x=128, y=128), ia.Keypoint(x=-20, y=30), ia.Keypoint(x=20, y=-30),
         ia.Keypoint(x=-20, y=-30)],
        shape=image.shape
    )

    images = []
    params = [
        (0.0, 0.0),
        (0.2, 0.2),
        (2.0, 0.25),
        (0.25, 3.0),
        (2.0, 3.0),
        (6.0, 3.0),
        (12.0, 3.0),
        (50.0, 5.0),
        (100.0, 5.0),
        (100.0, 10.0)
    ]

    for (alpha, sigma) in params:
        images_row = []
        seqs_row = [
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=3),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=3),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=3),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=2),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=3),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=2),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=3),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=0),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=1),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=2),
            iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=3)
        ]

        for seq in seqs_row:
            seq_det = seq.to_deterministic()
            image_aug = seq_det.augment_image(image)
            kps_aug = seq_det.augment_keypoints([kps])[0]
            image_aug_kp = np.copy(image_aug)
            image_aug_kp = kps_aug.draw_on_image(image_aug_kp, size=3)
            images_row.append(image_aug_kp)

        images.append(np.hstack(images_row))

    ia.imshow(np.vstack(images))
    imageio.imwrite("elastic_transformations.jpg", np.vstack(images))
Exemplo n.º 43
0
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)

    pairs = []
    params = [
        {"rotate": 45},
        {"translate_px": 20},
        {"translate_percent": 0.1},
        {"scale": 1.2},
        {"scale": 0.8},
        {"shear": 45},
        {"rotate": 45, "cval": 255},
        {"translate_px": 20, "mode": "constant"},
        {"translate_px": 20, "mode": "edge"},
        {"translate_px": 20, "mode": "symmetric"},
        {"translate_px": 20, "mode": "reflect"},
        {"translate_px": 20, "mode": "wrap"},
        {"scale": 0.5, "order": 0},
        {"scale": 0.5, "order": 1},
        {"scale": 0.5, "order": 2},
        {"scale": 0.5, "order": 3},
        {"scale": 0.5, "order": 4},
        {"scale": 0.5, "order": 5},
        {"rotate": 45, "translate_px": 20, "scale": 1.2},
        {"rotate": 45, "translate_px": 20, "scale": 0.8},
        {"rotate": (-45, 45), "translate_px": (-20, 20), "scale": (0.8, 1.2), "order": ia.ALL,
         "mode": ia.ALL, "cval": ia.ALL},
        {"rotate": (-45, 45), "translate_px": (-20, 20), "scale": (0.8, 1.2), "order": ia.ALL,
         "mode": ia.ALL, "cval": ia.ALL},
        {"rotate": (-45, 45), "translate_px": (-20, 20), "scale": (0.8, 1.2), "order": ia.ALL,
         "mode": ia.ALL, "cval": ia.ALL},
        {"rotate": (-45, 45), "translate_px": (-20, 20), "scale": (0.8, 1.2), "order": ia.ALL,
         "mode": ia.ALL, "cval": ia.ALL}
    ]
    seqs_skimage = [iaa.Affine(backend="skimage", **p) for p in params]
    seqs_cv2 = [iaa.Affine(backend="auto", **p) for p in params]

    for seq_skimage, seq_cv2 in zip(seqs_skimage, seqs_cv2):
        seq_skimage_det = seq_skimage.to_deterministic()
        seq_cv2_det = seq_cv2.to_deterministic()

        seq_cv2_det.copy_random_state_(seq_skimage_det)

        image_aug_skimage = seq_skimage_det.augment_image(image)
        image_aug_cv2 = seq_cv2_det.augment_image(image)
        kps_aug_skimage = seq_skimage_det.augment_keypoints([kps])[0]
        kps_aug_cv2     = seq_cv2_det.augment_keypoints([kps])[0]
        bbs_aug_skimage = seq_skimage_det.augment_bounding_boxes([bbs])[0]
        bbs_aug_cv2     = seq_cv2_det.augment_bounding_boxes([bbs])[0]

        image_before_skimage = np.copy(image)
        image_before_cv2 = np.copy(image)
        image_before_skimage = kps.draw_on_image(image_before_skimage)
        image_before_cv2 = kps.draw_on_image(image_before_cv2)
        image_before_skimage = bbs.draw_on_image(image_before_skimage)
        image_before_cv2 = bbs.draw_on_image(image_before_cv2)

        image_after_skimage = np.copy(image_aug_skimage)
        image_after_cv2 = np.copy(image_aug_cv2)
        image_after_skimage = kps_aug_skimage.draw_on_image(image_after_skimage)
        image_after_cv2 = kps_aug_cv2.draw_on_image(image_after_cv2)
        image_after_skimage = bbs_aug_skimage.draw_on_image(image_after_skimage)
        image_after_cv2 = bbs_aug_cv2.draw_on_image(image_after_cv2)

        pairs.append(np.hstack((image_before_skimage, image_after_skimage, image_after_cv2)))

    ia.imshow(np.vstack(pairs))
    imageio.imwrite("affine.jpg", np.vstack(pairs))
Exemplo n.º 44
0
def test_SegmentationMapOnImage_draw():
    arr = np.int32([
        [0, 1, 1],
        [0, 1, 1],
        [0, 1, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3), nb_classes=2)

    # simple example with 2 classes
    observed = segmap.draw()
    col0 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[0]
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    assert np.array_equal(observed, expected)

    # same example, with resizing to 2x the size
    observed = segmap.draw(size=(6, 6))
    expected = ia.imresize_single_image(expected, (6, 6), interpolation="nearest")
    assert np.array_equal(observed, expected)

    # custom choice of colors
    col0 = (10, 10, 10)
    col1 = (50, 51, 52)
    observed = segmap.draw(colors=[col0, col1])
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    assert np.array_equal(observed, expected)

    # background_threshold, background_class and foreground mask
    arr_c0 = np.float32([
        [0, 0, 0],
        [1.0, 0, 0],
        [0, 0, 0]
    ])
    arr_c1 = np.float32([
        [0, 1, 1],
        [0, 1, 1],
        [0.1, 1, 1]
    ])
    arr = np.concatenate([
        arr_c0[..., np.newaxis],
        arr_c1[..., np.newaxis]
    ], axis=2)
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3))

    observed, observed_fg = segmap.draw(background_threshold=0.01, return_foreground_mask=True)
    col0 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[0]
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    col2 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[2]
    expected = np.uint8([
        [col0, col2, col2],
        [col1, col2, col2],
        [col2, col2, col2]
    ])
    expected_fg = np.array([
        [False, True, True],
        [True, True, True],
        [True, True, True]
    ], dtype=np.bool)
    assert np.array_equal(observed, expected)
    assert np.array_equal(observed_fg, expected_fg)

    # background_threshold, background_class and foreground mask
    # here with higher threshold so that bottom left pixel switches to background
    observed, observed_fg = segmap.draw(background_threshold=0.11, return_foreground_mask=True)
    col0 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[0]
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    col2 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[2]
    expected = np.uint8([
        [col0, col2, col2],
        [col1, col2, col2],
        [col0, col2, col2]
    ])
    expected_fg = np.array([
        [False, True, True],
        [True, True, True],
        [False, True, True]
    ], dtype=np.bool)
    assert np.array_equal(observed, expected)
    assert np.array_equal(observed_fg, expected_fg)
Exemplo n.º 45
0
def test_SegmentationMapOnImage_draw_on_image():
    arr = np.int32([
        [0, 1, 1],
        [0, 1, 1],
        [0, 1, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3), nb_classes=2)

    image = np.uint8([
        [0, 10, 20],
        [30, 40, 50],
        [60, 70, 80]
    ])
    image = np.tile(image[:, :, np.newaxis], (1, 1, 3))

    # only image visible
    observed = segmap.draw_on_image(image, alpha=0)
    assert np.array_equal(observed, image)

    # only segmap visible
    observed = segmap.draw_on_image(image, alpha=1.0, draw_background=True)
    col0 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[0]
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    assert np.array_equal(observed, expected)

    # only segmap visible - in foreground
    observed = segmap.draw_on_image(image, alpha=1.0, draw_background=False)
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    expected = np.uint8([
        [image[0, 0, :], col1, col1],
        [image[1, 0, :], col1, col1],
        [image[2, 0, :], col1, col1]
    ])
    assert np.array_equal(observed, expected)

    # overlay without background drawn
    a1 = 0.7
    a0 = 1.0 - a1
    observed = segmap.draw_on_image(image, alpha=a1, draw_background=False)
    col1 = np.uint8(ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1])
    expected = np.float32([
        [image[0, 0, :], a0*image[0, 1, :] + a1*col1, a0*image[0, 2, :] + a1*col1],
        [image[1, 0, :], a0*image[1, 1, :] + a1*col1, a0*image[1, 2, :] + a1*col1],
        [image[2, 0, :], a0*image[2, 1, :] + a1*col1, a0*image[2, 2, :] + a1*col1]
    ])
    d_max = np.max(np.abs(observed.astype(np.float32) - expected))
    assert observed.shape == expected.shape
    assert d_max <= 1.0 + 1e-4

    # overlay with background drawn
    a1 = 0.7
    a0 = 1.0 - a1
    observed = segmap.draw_on_image(image, alpha=a1, draw_background=True)
    col0 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[0]
    col1 = ia.SegmentationMapOnImage.DEFAULT_SEGMENT_COLORS[1]
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    expected = a0 * image + a1 * expected
    d_max = np.max(np.abs(observed.astype(np.float32) - expected.astype(np.float32)))
    assert observed.shape == expected.shape
    assert d_max <= 1.0 + 1e-4

    # resizing of segmap to image
    arr = np.int32([
        [0, 1, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3), nb_classes=2)

    image = np.uint8([
        [0, 10, 20],
        [30, 40, 50],
        [60, 70, 80]
    ])
    image = np.tile(image[:, :, np.newaxis], (1, 1, 3))

    a1 = 0.7
    a0 = 1.0 - a1
    observed = segmap.draw_on_image(image, alpha=a1, draw_background=True, resize="segmentation_map")
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    expected = a0 * image + a1 * expected
    d_max = np.max(np.abs(observed.astype(np.float32) - expected.astype(np.float32)))
    assert observed.shape == expected.shape
    assert d_max <= 1.0 + 1e-4

    # resizing of image to segmap
    arr = np.int32([
        [0, 1, 1],
        [0, 1, 1],
        [0, 1, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(1, 3), nb_classes=2)

    image = np.uint8([
        [0, 10, 20]
    ])
    image = np.tile(image[:, :, np.newaxis], (1, 1, 3))
    image_rs = ia.imresize_single_image(image, arr.shape[0:2], interpolation="cubic")

    a1 = 0.7
    a0 = 1.0 - a1
    observed = segmap.draw_on_image(image, alpha=a1, draw_background=True, resize="image")
    expected = np.uint8([
        [col0, col1, col1],
        [col0, col1, col1],
        [col0, col1, col1]
    ])
    expected = a0 * image_rs + a1 * expected
    d_max = np.max(np.abs(observed.astype(np.float32) - expected.astype(np.float32)))
    assert observed.shape == expected.shape
    assert d_max <= 1.0 + 1e-4
Exemplo n.º 46
0
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (64, 64))
    keypoints_on_image = ia.KeypointsOnImage([ia.Keypoint(x=10, y=10)], shape=image.shape)
    images_arr = np.array([image for _ in range(NB_AUGS_PER_IMAGE)])
    images_list = [image for _ in range(NB_AUGS_PER_IMAGE)]
    keypoints_on_images = [keypoints_on_image.deepcopy() for _ in range(NB_AUGS_PER_IMAGE)]
    print("image shape:", image.shape)
    print("Press ENTER or wait %d ms to proceed to the next image." % (TIME_PER_STEP,))

    children = [
        iaa.CoarseDropout(p=0.5, size_percent=0.05),
        iaa.AdditiveGaussianNoise(scale=0.1*255)
    ]

    n = [
        None,
        0,
        1,
        len(children),
        len(children)+1,
        (1, 1),
        (1, len(children)),
        (1, len(children)+1),
        (1, None)
    ]

    cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, (2+4+4)*64)

    rows = []
    for ni in n:
        aug = iaa.SomeOf(ni, children, random_order=False)
        rows.append(aug.augment_images(images_arr))
    grid = to_grid(rows, None)
    cv2.imshow("aug", grid[..., ::-1])  # here with rgb2bgr
    cv2.waitKey(TIME_PER_STEP)

    for ni in n:
        print("------------------------")
        print("-- %s" % (str(ni),))
        print("------------------------")
        aug = iaa.SomeOf(ni, children, random_order=False)
        aug_ro = iaa.SomeOf(ni, children, random_order=True)

        aug_det = aug.to_deterministic()
        aug_ro_det = aug_ro.to_deterministic()

        aug_kps = []
        aug_kps.extend([aug_det.augment_keypoints(keypoints_on_images)] * 4)
        aug_kps.extend([aug_ro_det.augment_keypoints(keypoints_on_images)] * 4)

        aug_rows = []
        aug_rows.append(images_arr)
        aug_rows.append(images_list)

        aug_rows.append(aug_det.augment_images(images_arr))
        aug_rows.append(aug_det.augment_images(images_arr))
        aug_rows.append(aug_det.augment_images(images_list))
        aug_rows.append(aug_det.augment_images(images_list))

        aug_rows.append(aug_ro_det.augment_images(images_arr))
        aug_rows.append(aug_ro_det.augment_images(images_arr))
        aug_rows.append(aug_ro_det.augment_images(images_list))
        aug_rows.append(aug_ro_det.augment_images(images_list))

        grid = to_grid(aug_rows, aug_kps)

        title = "n=%s" % (str(ni),)
        grid = ia.draw_text(grid, x=5, y=5, text=title)

        cv2.imshow("aug", grid[..., ::-1]) # here with rgb2bgr
        cv2.waitKey(TIME_PER_STEP)
Exemplo n.º 47
0
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)

    pairs = []
    seqs = [
        iaa.AffineCv2(rotate=45),
        iaa.AffineCv2(translate_px=20),
        iaa.AffineCv2(translate_percent=0.1),
        iaa.AffineCv2(scale=1.2),
        iaa.AffineCv2(scale=0.8),
        iaa.AffineCv2(shear=45),
        iaa.AffineCv2(rotate=45, cval=256),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_CONSTANT),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REPLICATE),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT_101),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_WRAP),
        iaa.AffineCv2(translate_px=20, mode="constant"),
        iaa.AffineCv2(translate_px=20, mode="replicate"),
        iaa.AffineCv2(translate_px=20, mode="reflect"),
        iaa.AffineCv2(translate_px=20, mode="reflect_101"),
        iaa.AffineCv2(translate_px=20, mode="wrap"),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_NEAREST),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_LINEAR),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_CUBIC),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_LANCZOS4),
        iaa.AffineCv2(scale=0.5, order="nearest"),
        iaa.AffineCv2(scale=0.5, order="linear"),
        iaa.AffineCv2(scale=0.5, order="cubic"),
        iaa.AffineCv2(scale=0.5, order="lanczos4"),
        iaa.AffineCv2(rotate=45, translate_px=20, scale=1.2),
        iaa.AffineCv2(rotate=45, translate_px=20, scale=0.8),
        iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL)
    ]

    for seq in seqs:
        seq_det = seq.to_deterministic()
        image_aug = seq_det.augment_image(image)
        #print(image_aug.dtype, np.min(image_aug), np.max(image_aug))
        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)

        pairs.append(np.hstack((image_before, image_after)))

    ia.imshow(np.vstack(pairs))
    imageio.imwrite("affinecv2.jpg", np.vstack(pairs))
Exemplo n.º 48
0
    def _augment_images(self, images, random_state, parents, hooks):
        iadt.gate_dtypes(images,
                         allowed=["bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64"],
                         disallowed=["uint128", "uint256", "int128", "int256",
                                     "float16", "float32", "float64", "float96", "float128", "float256"],
                         augmenter=self)

        nb_images = len(images)
        rss = ia.derive_random_states(random_state, 1+nb_images)
        n_segments_samples = self.n_segments.draw_samples((nb_images,), random_state=rss[0])
        for i, (image, rs) in enumerate(zip(images, rss[1:])):
            # TODO this results in an error when n_segments is 0
            replace_samples = self.p_replace.draw_samples((n_segments_samples[i],), random_state=rs)

            if np.max(replace_samples) == 0:
                # not a single superpixel would be replaced by its average color,
                # i.e. the image would not be changed, so just keep it
                pass
            else:
                image = images[i]
                min_value, _center_value, max_value = iadt.get_value_range_of_dtype(image.dtype)

                orig_shape = image.shape
                if self.max_size is not None:
                    size = max(image.shape[0], image.shape[1])
                    if size > self.max_size:
                        resize_factor = self.max_size / size
                        new_height, new_width = int(image.shape[0] * resize_factor), int(image.shape[1] * resize_factor)
                        image = ia.imresize_single_image(image, (new_height, new_width),
                                                         interpolation=self.interpolation)

                image_sp = np.copy(image)
                segments = segmentation.slic(image, n_segments=n_segments_samples[i], compactness=10)
                nb_channels = image.shape[2]
                for c in sm.xrange(nb_channels):
                    # segments+1 here because otherwise regionprops always misses
                    # the last label
                    regions = measure.regionprops(segments+1, intensity_image=image[..., c])
                    for ridx, region in enumerate(regions):
                        # with mod here, because slic can sometimes create more superpixel
                        # than requested. replace_samples then does not have enough
                        # values, so we just start over with the first one again.
                        if replace_samples[ridx % len(replace_samples)] >= 0.5:
                            mean_intensity = region.mean_intensity
                            image_sp_c = image_sp[..., c]
                            if image_sp_c.dtype.kind in ["i", "u", "b"]:
                                # After rounding the value can end up slightly outside of the value_range.
                                # Hence, we need to clip. We do clip via min(max(...)) instead of np.clip
                                # because the latter one does not seem to keep dtypes for dtypes with
                                # large itemsizes (e.g. uint64).
                                value = int(np.round(mean_intensity))
                                value = min(max(value, min_value), max_value)
                                image_sp_c[segments == ridx] = value
                            else:
                                image_sp_c[segments == ridx] = mean_intensity

                if orig_shape != image.shape:
                    image_sp = ia.imresize_single_image(image_sp, orig_shape[0:2], interpolation=self.interpolation)

                images[i] = image_sp
        return images