def main():
    black_and_white = iaa.RandomColorsBinaryImageColorizer(
        color_true=255, color_false=0)

    print("alpha=1.0, black and white")
    image = ia.quokka_square((128, 128))
    aug = iaa.Canny(alpha=1.0, colorizer=black_and_white)
    ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))

    print("alpha=1.0, random color")
    image = ia.quokka_square((128, 128))
    aug = iaa.Canny(alpha=1.0)
    ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))

    print("alpha=1.0, sobel ksize=[3, 13], black and white")
    image = ia.quokka_square((128, 128))
    aug = iaa.Canny(alpha=1.0, sobel_kernel_size=[3, 7],
                    colorizer=black_and_white)
    ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))

    print("alpha=1.0, sobel ksize=3, black and white")
    image = ia.quokka_square((128, 128))
    aug = iaa.Canny(alpha=1.0, sobel_kernel_size=3,
                    colorizer=black_and_white)
    ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))

    print("fully random")
    image = ia.quokka_square((128, 128))
    aug = iaa.Canny()
    ia.imshow(ia.draw_grid(aug(images=[image] * (5*5))))
Exemple #2
0
    def test__draw_samples__single_value_hysteresis(self):
        seed = 1
        nb_images = 1000

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            sobel_kernel_size=[3, 5, 7],
            random_state=iarandom.RNG(seed))

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=iarandom.RNG(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = iarandom.RNG(seed).duplicate(4)
        alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images, ),
                                                             rss[0])
        hthresh_expected = iap.Choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                                       10]).draw_samples((nb_images, 2),
                                                         rss[1])
        sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images, ),
                                                            rss[2])

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        assert np.any(invalid)
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
  def rgbmore(self, im):
    return_im = []
    add_return_im = lambda im: return_im.extend(im)

    grey = np.array(im.convert(mode='L'))
    im = np.array(im)
    rgb_grey = np.dstack((im, grey))

    edge = iaa.EdgeDetect(alpha=1)(images=rgb_grey)

    dir_edge = lambda d: iaa.DirectedEdgeDetect(alpha=1, direction=d)(images=
                                                                      grey)
    dir_edges = np.array(
      [dir_edge(d) for d in np.linspace(0, 1, num=3, endpoint=False)])
    dir_edges = np.transpose(dir_edges, (1, 2, 0))
    canny = iaa.Canny(alpha=1.0,
                      hysteresis_thresholds=128,
                      sobel_kernel_size=4,
                      deterministic=True,
                      colorizer=iaa.RandomColorsBinaryImageColorizer(
                        color_true=255, color_false=0))(images=grey)

    avg_pool = iaa.AveragePooling(2)(images=grey)
    max_pool = iaa.MaxPooling(2)(images=grey)
    min_pool = iaa.MinPooling(2)(images=grey)

    add_return_im([im, grey])
    add_return_im([edge, dir_edges, canny])
    add_return_im([avg_pool, max_pool, min_pool])
    return np.dstack(return_im)
Exemple #4
0
    def test_augment_images__alpha_is_one(self):
        colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=254,
                                                         color_false=1)

        aug = iaa.Canny(alpha=1.0,
                        hysteresis_thresholds=100,
                        sobel_kernel_size=3,
                        colorizer=colorizer,
                        random_state=1)

        image_single_chan = np.uint8([[0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 1, 1, 1, 0, 0, 0]])
        image = np.tile(image_single_chan[:, :, np.newaxis] * 128, (1, 1, 3))

        # canny image, looks a bit unintuitive, but is what OpenCV returns
        # can be checked via something like
        # print("canny\n", cv2.Canny(image_single_chan*255, threshold1=100,
        #            threshold2=200,
        #            apertureSize=3,
        #            L2gradient=True))
        image_canny = np.array([[0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0],
                                [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0],
                                [0, 1, 0, 0, 1, 0, 0]],
                               dtype=bool)

        image_aug_expected = np.copy(image)
        image_aug_expected[image_canny] = 254
        image_aug_expected[~image_canny] = 1

        image_aug = aug.augment_image(image)
        assert np.array_equal(image_aug, image_aug_expected)
Exemple #5
0
 def test___str___tuple_as_hysteresis(self):
     alpha = iap.Deterministic(0.2)
     hysteresis_thresholds = (
         iap.Deterministic(10),
         iap.Deterministic(11)
     )
     sobel_kernel_size = iap.Deterministic(3)
     colorizer = iaa.RandomColorsBinaryImageColorizer(
         color_true=10, color_false=20)
     aug = iaa.Canny(
         alpha=alpha,
         hysteresis_thresholds=hysteresis_thresholds,
         sobel_kernel_size=sobel_kernel_size,
         colorizer=colorizer
     )
     observed = aug.__str__()
     expected = ("Canny(alpha=%s, hysteresis_thresholds=(%s, %s), "
                 "sobel_kernel_size=%s, colorizer=%s, name=UnnamedCanny, "
                 "deterministic=False)") % (
                     str(aug.alpha),
                     str(aug.hysteresis_thresholds[0]),
                     str(aug.hysteresis_thresholds[1]),
                     str(aug.sobel_kernel_size),
                     colorizer)
     assert observed == expected
Exemple #6
0
 def test___init___custom_settings(self):
     aug = iaa.Canny(
         alpha=0.2,
         hysteresis_thresholds=([0, 1, 2], iap.DiscreteUniform(1, 10)),
         sobel_kernel_size=[3, 5],
         colorizer=iaa.RandomColorsBinaryImageColorizer(
             color_true=10, color_false=20)
     )
     assert is_parameter_instance(aug.alpha, iap.Deterministic)
     assert isinstance(aug.hysteresis_thresholds, tuple)
     assert is_parameter_instance(aug.sobel_kernel_size, iap.Choice)
     assert isinstance(aug.colorizer, iaa.RandomColorsBinaryImageColorizer)
     assert np.isclose(aug.alpha.value, 0.2)
     assert len(aug.hysteresis_thresholds) == 2
     assert is_parameter_instance(aug.hysteresis_thresholds[0], iap.Choice)
     assert aug.hysteresis_thresholds[0].a == [0, 1, 2]
     assert is_parameter_instance(aug.hysteresis_thresholds[1],
                                  iap.DiscreteUniform)
     assert np.isclose(aug.hysteresis_thresholds[1].a.value, 1)
     assert np.isclose(aug.hysteresis_thresholds[1].b.value, 10)
     assert is_parameter_instance(aug.sobel_kernel_size, iap.Choice)
     assert aug.sobel_kernel_size.a == [3, 5]
     assert is_parameter_instance(aug.colorizer.color_true,
                                  iap.Deterministic)
     assert is_parameter_instance(aug.colorizer.color_false,
                                  iap.Deterministic)
     assert aug.colorizer.color_true.value == 10
     assert aug.colorizer.color_false.value == 20
Exemple #7
0
 def test___init___default_settings(self):
     aug = iaa.Canny()
     assert is_parameter_instance(aug.alpha, iap.Uniform)
     assert isinstance(aug.hysteresis_thresholds, tuple)
     assert is_parameter_instance(aug.sobel_kernel_size, iap.DiscreteUniform)
     assert isinstance(aug.colorizer, iaa.RandomColorsBinaryImageColorizer)
     assert np.isclose(aug.alpha.a.value, 0.0)
     assert np.isclose(aug.alpha.b.value, 1.0)
     assert len(aug.hysteresis_thresholds) == 2
     assert is_parameter_instance(aug.hysteresis_thresholds[0],
                                  iap.DiscreteUniform)
     assert np.isclose(aug.hysteresis_thresholds[0].a.value, 100-40)
     assert np.isclose(aug.hysteresis_thresholds[0].b.value, 100+40)
     assert is_parameter_instance(aug.hysteresis_thresholds[1],
                                  iap.DiscreteUniform)
     assert np.isclose(aug.hysteresis_thresholds[1].a.value, 200-40)
     assert np.isclose(aug.hysteresis_thresholds[1].b.value, 200+40)
     assert aug.sobel_kernel_size.a.value == 3
     assert aug.sobel_kernel_size.b.value == 7
     assert is_parameter_instance(aug.colorizer.color_true,
                                  iap.DiscreteUniform)
     assert is_parameter_instance(aug.colorizer.color_false,
                                  iap.DiscreteUniform)
     assert aug.colorizer.color_true.a.value == 0
     assert aug.colorizer.color_true.b.value == 255
     assert aug.colorizer.color_false.a.value == 0
     assert aug.colorizer.color_false.b.value == 255
Exemple #8
0
    def test__draw_samples__single_value_hysteresis(self):
        seed = 1
        nb_images = 1000

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            sobel_kernel_size=[3, 5, 7],
            random_state=np.random.RandomState(seed))

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=np.random.RandomState(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = ia.derive_random_states(np.random.RandomState(seed), 4)
        alpha_expected = [0.2] * nb_images
        hthresh_expected = rss[1].choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                         size=(nb_images, 2))
        sobel_expected = rss[3].choice([3, 5, 7], size=(nb_images, ))

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        assert np.any(invalid)
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
Exemple #9
0
    def test_augment_images__random_values(self):
        colorizer = iaa.RandomColorsBinaryImageColorizer(
            color_true=255,
            color_false=0
        )

        image_single_chan = iarandom.RNG(1).integers(
            0, 255, size=(100, 100), dtype="uint8")
        image = np.tile(image_single_chan[:, :, np.newaxis], (1, 1, 3))

        images_canny_uint8 = {}
        for thresh1, thresh2, ksize in itertools.product([100],
                                                         [200],
                                                         [3, 5]):
            if thresh1 > thresh2:
                continue

            image_canny = cv2.Canny(
                image,
                threshold1=thresh1,
                threshold2=thresh2,
                apertureSize=ksize,
                L2gradient=True)
            image_canny_uint8 = np.tile(
                image_canny[:, :, np.newaxis], (1, 1, 3))

            similar = 0
            for key, image_expected in images_canny_uint8.items():
                if np.array_equal(image_canny_uint8, image_expected):
                    similar += 1
            assert similar == 0

            images_canny_uint8[(thresh1, thresh2, ksize)] = image_canny_uint8

        seen = {key: False for key in images_canny_uint8.keys()}

        for i in range(500):
            aug = iaa.Canny(
                alpha=1.0,
                hysteresis_thresholds=(iap.Deterministic(100),
                                       iap.Deterministic(200)),
                sobel_kernel_size=[3, 5],
                colorizer=colorizer,
                seed=i)

            image_aug = aug.augment_image(image)
            match_index = None
            for key, image_expected in images_canny_uint8.items():
                if np.array_equal(image_aug, image_expected):
                    match_index = key
                    break
            assert match_index is not None
            seen[match_index] = True

            assert len(seen.keys()) == len(images_canny_uint8.keys())
            if all(seen.values()):
                break
        assert np.all(seen.values())
 def __call__(self, sample):
     image, polygon, labels = sample["image"], sample["polygon"], sample[
         "labels"]
     image = np.array(image)
     t = iaa.Canny(alpha=self.alpha)
     img = t(image=image)
     image = Image.fromarray(img)
     sample = {'image': image, 'polygon': polygon, 'labels': labels}
     return sample
Exemple #11
0
    def test_augment_images__alpha_is_zero(self):
        aug = iaa.Canny(alpha=0.0,
                        hysteresis_thresholds=(0, 10),
                        sobel_kernel_size=[3, 5, 7],
                        random_state=1)

        image = np.arange(5 * 5 * 3).astype(np.uint8).reshape((5, 5, 3))
        image_aug = aug.augment_image(image)
        assert np.array_equal(image_aug, image)
Exemple #12
0
    def test_augment_images__random_color(self):
        class _Color(iap.StochasticParameter):
            def __init__(self, values):
                super(_Color, self).__init__()
                self.values = values

            def _draw_samples(self, size, random_state):
                v = random_state.choice(self.values)
                return np.full(size, v, dtype=np.uint8)

        colorizer = iaa.RandomColorsBinaryImageColorizer(
            color_true=_Color([253, 254]), color_false=_Color([1, 2]))

        image_single_chan = np.uint8([[0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 0, 0, 1, 0, 0, 0],
                                      [0, 1, 1, 1, 0, 0, 0]])
        image = np.tile(image_single_chan[:, :, np.newaxis] * 128, (1, 1, 3))

        # canny image, looks a bit unintuitive, but is what OpenCV returns
        # can be checked via something like
        # print("canny\n", cv2.Canny(image_single_chan*255, threshold1=100,
        #            threshold2=200,
        #            apertureSize=3,
        #            L2gradient=True))
        image_canny = np.array([[0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0],
                                [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0],
                                [0, 1, 0, 0, 1, 0, 0]],
                               dtype=bool)

        seen = {
            (253, 1): False,
            (253, 2): False,
            (254, 1): False,
            (254, 2): False
        }
        for i in range(100):
            aug = iaa.Canny(alpha=1.0,
                            hysteresis_thresholds=100,
                            sobel_kernel_size=3,
                            colorizer=colorizer,
                            random_state=i)

            image_aug = aug.augment_image(image)
            color_true = np.unique(image_aug[image_canny])
            color_false = np.unique(image_aug[~image_canny])
            assert len(color_true) == 1
            assert len(color_false) == 1
            color_true = int(color_true[0])
            color_false = int(color_false[0])

            seen[(int(color_true), int(color_false))] = True
            assert len(seen.keys()) == 4
            if all(seen.values()):
                break
        assert np.all(seen.values())
Exemple #13
0
    def test_zero_sized_axes(self):
        shapes = [(0, 0, 3), (0, 1, 3), (1, 0, 3)]

        for shape in shapes:
            with self.subTest(shape=shape):
                image = np.zeros(shape, dtype=np.uint8)
                aug = iaa.Canny(alpha=1)

                image_aug = aug(image=image)

                assert image_aug.shape == image.shape
def chapter_augmenters_canny():
    fn_start = "edges/canny"

    aug = iaa.Canny()
    run_and_save_augseq(fn_start + ".jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Canny(alpha=(0.0, 0.5))
    run_and_save_augseq(fn_start + "_alpha.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Canny(alpha=(0.0, 0.5),
                    colorizer=iaa.RandomColorsBinaryImageColorizer(
                        color_true=255, color_false=0))
    run_and_save_augseq(fn_start + "_alpha_white_on_black.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Canny(alpha=(0.5, 1.0), sobel_kernel_size=[3, 7])
    run_and_save_augseq(fn_start + "_sobel_kernel_size.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Alpha((0.0, 1.0), iaa.Canny(alpha=1), iaa.MedianBlur(13))
    run_and_save_augseq(fn_start + "_alpha_median_blur.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)
Exemple #15
0
    def test__draw_samples__tuple_as_hysteresis(self):
        seed = 1
        nb_images = 10

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                   iap.DiscreteUniform(5, 100)),
            sobel_kernel_size=[3, 5, 7],
            random_state=iarandom.RNG(seed))
        aug.alpha = remove_prefetching(aug.alpha)
        aug.hysteresis_thresholds = (
            remove_prefetching(aug.hysteresis_thresholds[0]),
            remove_prefetching(aug.hysteresis_thresholds[1])
        )
        aug.sobel_kernel_size = remove_prefetching(aug.sobel_kernel_size)

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=iarandom.RNG(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = iarandom.RNG(seed).duplicate(4)
        alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images,),
                                                             rss[0])
        hthresh_expected = [None, None]
        hthresh_expected[0] = iap.Choice(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).draw_samples((nb_images,),
                                                             rss[1])
        # TODO simplify this to rss[2].randint(5, 100+1)
        #      would currenlty be a bit more ugly, because DiscrUniform
        #      samples two values for a and b first from rss[2]
        hthresh_expected[1] = iap.DiscreteUniform(5, 100).draw_samples(
            (nb_images,), rss[2])
        hthresh_expected = np.stack(hthresh_expected, axis=-1)

        sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images,),
                                                            rss[3])

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
Exemple #16
0
 def test_get_parameters(self):
     alpha = iap.Deterministic(0.2)
     hysteresis_thresholds = iap.Deterministic(10)
     sobel_kernel_size = iap.Deterministic(3)
     colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=10,
                                                      color_false=20)
     aug = iaa.Canny(alpha=alpha,
                     hysteresis_thresholds=hysteresis_thresholds,
                     sobel_kernel_size=sobel_kernel_size,
                     colorizer=colorizer)
     params = aug.get_parameters()
     assert params[0] is alpha
     assert params[1] is hysteresis_thresholds
     assert params[2] is sobel_kernel_size
     assert params[3] is colorizer
Exemple #17
0
 def test___str___single_value_hysteresis(self):
     alpha = iap.Deterministic(0.2)
     hysteresis_thresholds = iap.Deterministic(10)
     sobel_kernel_size = iap.Deterministic(3)
     colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=10,
                                                      color_false=20)
     aug = iaa.Canny(alpha=alpha,
                     hysteresis_thresholds=hysteresis_thresholds,
                     sobel_kernel_size=sobel_kernel_size,
                     colorizer=colorizer)
     observed = aug.__str__()
     expected = ("Canny(alpha=%s, hysteresis_thresholds=%s, "
                 "sobel_kernel_size=%s, colorizer=%s, name=UnnamedCanny, "
                 "deterministic=False)") % (alpha, hysteresis_thresholds,
                                            sobel_kernel_size, colorizer)
     assert observed == expected
Exemple #18
0
 def test___init___single_value_hysteresis(self):
     aug = iaa.Canny(alpha=0.2,
                     hysteresis_thresholds=[0, 1, 2],
                     sobel_kernel_size=[3, 5],
                     colorizer=iaa.RandomColorsBinaryImageColorizer(
                         color_true=10, color_false=20))
     assert isinstance(aug.alpha, iap.Deterministic)
     assert isinstance(aug.hysteresis_thresholds, iap.Choice)
     assert isinstance(aug.sobel_kernel_size, iap.Choice)
     assert isinstance(aug.colorizer, iaa.RandomColorsBinaryImageColorizer)
     assert np.isclose(aug.alpha.value, 0.2)
     assert aug.hysteresis_thresholds.a == [0, 1, 2]
     assert isinstance(aug.sobel_kernel_size, iap.Choice)
     assert aug.sobel_kernel_size.a == [3, 5]
     assert isinstance(aug.colorizer.color_true, iap.Deterministic)
     assert isinstance(aug.colorizer.color_false, iap.Deterministic)
     assert aug.colorizer.color_true.value == 10
     assert aug.colorizer.color_false.value == 20
Exemple #19
0
    def test__draw_samples__tuple_as_hysteresis(self):
        seed = 1
        nb_images = 10

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                                    10], iap.DiscreteUniform(5, 100)),
            sobel_kernel_size=[3, 5, 7],
            random_state=np.random.RandomState(seed))

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=np.random.RandomState(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = ia.derive_random_states(np.random.RandomState(seed), 4)
        alpha_expected = [0.2] * nb_images
        hthresh_expected = (
            rss[1].choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                          size=(nb_images, )),
            # TODO simplify this to rss[2].randint(5, 100+1)
            #      would currenlty be a bit more ugly, because DiscrUniform
            #      samples two values for a and b first from rss[2]
            iap.DiscreteUniform(5, 100).draw_samples((nb_images, ), rss[2]))
        hthresh_expected = np.stack(hthresh_expected, axis=-1)

        sobel_expected = rss[3].choice([3, 5, 7], size=(nb_images, ))

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
Exemple #20
0
 def test_pickleable(self):
     aug = iaa.Canny(random_state=1)
     runtest_pickleable_uint8_img(aug, iterations=20)
Exemple #21
0
    elif augmentation == 'random_shadow':
        transform = RandomShadow(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'random_sun_flare':
        transform = RandomSunFlare(always_apply=True)
        transformed_image = transform(image=image)['image']
        
    elif augmentation == 'spatter':
        transform = iaa.imgcorruptlike.Spatter(severity=2)
        transformed_image = transform(image=image)
    
    ## Edges

    elif augmentation == 'canny':
        transform = iaa.Canny(alpha=(0.0, 0.9))
        transformed_image = transform(image=image)

    ## Pooling
    
    elif augmentation == 'average_pooling':
        transform = iaa.AveragePooling(5)
        transformed_image = transform(image=image)

    elif augmentation == 'max_pooling':
        transform = iaa.MaxPooling(5)
        transformed_image = transform(image=image)

    elif augmentation == 'min_pooling':
        transform = iaa.MinPooling(5)
        transformed_image = transform(image=image)
from PIL import Image

img=plt.imread('bird.jpg')




seq = iaa.Sequential([
    iaa.Fliplr(p=0),# basically this is original one
    iaa.Crop(px=(22, 45),keep_size=False), # crop images from each side by 0 to 16px (randomly chosen)
    iaa.Fliplr(1), # horizontally flip 50% of the images
    iaa.GaussianBlur(sigma=(5, 7.0)), # blur images with a sigma of 0 to 3.0
    iaa.ImpulseNoise(p=(0.6,1)),
    iaa.EdgeDetect(alpha=(0.9,1)),
    #iaa.AddToBrightness(add=(100,124)),
    iaa.Canny(alpha=(0.8,0.9)),
    iaa.Grayscale(alpha=1.00),
    iaa.ChannelShuffle(p=1),
    iaa.geometric.Affine( scale=2,rotate=22, backend='cv2'),
    iaa.Cartoon(blur_ksize=(11,13)),
    iaa.CenterCropToAspectRatio(1),
    iaa.CenterCropToFixedSize(100,100),
    iaa.ChangeColorTemperature(kelvin=(2222,3333)),
    #iaa.segmentation(),
    iaa.CLAHE(clip_limit=(4,8)),
    iaa.Rotate(rotate=(-30,90))
])

plt.figure(figsize=(12,12))

for idx,Augmentor in enumerate(seq):
Exemple #23
0
 def test_pickleable(self):
     aug = iaa.Canny(seed=1)
     runtest_pickleable_uint8_img(aug, iterations=20)
import tensorflow as tf
from matplotlib import pyplot as plt
path= 'Image A*/train/*.xml'
import cv2



seq = iaa.Sequential([
    iaa.Fliplr(p=0),# basically this is original one
    iaa.Sometimes(0.05,(iaa.Crop(px=(22, 45),keep_size=True))), # crop images from each side by 0 to 16px (randomly chosen)
    iaa.Sometimes(0.5,(iaa.Fliplr(1))), # horizontally flip 50% of the images
    iaa.Sometimes(0.02,iaa.GaussianBlur(sigma=(5, 7.0))), # blur images with a sigma of 0 to 3.0
    iaa.Sometimes(0.02 ,iaa.ImpulseNoise(p=(0.6,1))),
    iaa.Sometimes(0.02 ,iaa.EdgeDetect(alpha=(0.09,1))),
    #iaa.AddToBrightness(add=(100,124)),
    iaa.Sometimes(0.02 ,iaa.Canny(alpha=(0.8,0.9))),
    iaa.Sometimes(0.5 ,iaa.Grayscale(alpha=1.00)),
    iaa.Sometimes(0.5 ,iaa.ChannelShuffle(p=1)),
    #iaa.Sometimes(0.02 ,(iaa.geometric.Affine( scale=2,rotate=22,order=1))),
    iaa.Sometimes(0.5 ,iaa.Cartoon(blur_ksize=(11,13))),
    iaa.Sometimes(0.02 ,iaa.CenterCropToAspectRatio(1)),
    iaa.Sometimes(0.02 ,iaa.CenterCropToFixedSize(100,100)),
    iaa.Sometimes(0.12 ,iaa.ChangeColorTemperature(kelvin=(2222,3333))),
    #iaa.segmentation(),
    iaa.Sometimes(0.12 ,iaa.CLAHE(clip_limit=(4,8))),
    iaa.Sometimes(0.8 ,iaa.Rotate(rotate=(-90,90),order=1))
])

plt.figure(figsize=(12,12))

aug50 = iaa.UniformColorQuantizationToNBits()
aug51 = iaa.GammaContrast((0.5, 2.0), per_channel=True)
aug52 = iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True)
aug53 = iaa.LogContrast(gain=(0.6, 1.4), per_channel=True)
aug54 = iaa.LinearContrast((0.4, 1.6), per_channel=True)
# aug55 = iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True)
aug56 = iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization())
aug57 = iaa.HistogramEqualization(
    from_colorspace=iaa.HistogramEqualization.BGR,
    to_colorspace=iaa.HistogramEqualization.HSV)

aug58  = iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 0.5))
aug59 = iaa.Canny(
    alpha=(0.0, 0.3),
    colorizer=iaa.RandomColorsBinaryImageColorizer(
        color_true=255,
        color_false=0
    )
)

def aug_imgaug(aug, image):

    image2 = image.copy()
    image2 = np.expand_dims(image2, axis=0)
    images_aug = aug(images = image2)
    
    return images_aug

class FaceEmbeddings():
  """Class to load  model and run inference."""
def create_augmenters(height, width, height_augmentable, width_augmentable,
                      only_augmenters):
    def lambda_func_images(images, random_state, parents, hooks):
        return images

    def lambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return heatmaps

    def lambda_func_keypoints(keypoints, random_state, parents, hooks):
        return keypoints

    def assertlambda_func_images(images, random_state, parents, hooks):
        return True

    def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return True

    def assertlambda_func_keypoints(keypoints, random_state, parents, hooks):
        return True

    augmenters_meta = [
        iaa.Sequential([iaa.Noop(), iaa.Noop()],
                       random_order=False,
                       name="Sequential_2xNoop"),
        iaa.Sequential([iaa.Noop(), iaa.Noop()],
                       random_order=True,
                       name="Sequential_2xNoop_random_order"),
        iaa.SomeOf((1, 3),
                   [iaa.Noop(), iaa.Noop(), iaa.Noop()],
                   random_order=False,
                   name="SomeOf_3xNoop"),
        iaa.SomeOf((1, 3),
                   [iaa.Noop(), iaa.Noop(), iaa.Noop()],
                   random_order=True,
                   name="SomeOf_3xNoop_random_order"),
        iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"),
        iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"),
        iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"),
        iaa.Noop(name="Noop"),
        iaa.Lambda(func_images=lambda_func_images,
                   func_heatmaps=lambda_func_heatmaps,
                   func_keypoints=lambda_func_keypoints,
                   name="Lambda"),
        iaa.AssertLambda(func_images=assertlambda_func_images,
                         func_heatmaps=assertlambda_func_heatmaps,
                         func_keypoints=assertlambda_func_keypoints,
                         name="AssertLambda"),
        iaa.AssertShape((None, height_augmentable, width_augmentable, None),
                        name="AssertShape"),
        iaa.ChannelShuffle(0.5, name="ChannelShuffle")
    ]
    augmenters_arithmetic = [
        iaa.Add((-10, 10), name="Add"),
        iaa.AddElementwise((-10, 10), name="AddElementwise"),
        #iaa.AddElementwise((-500, 500), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"),
        iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"),
        iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Dropout((0.01, 0.05), name="Dropout"),
        iaa.CoarseDropout((0.01, 0.05),
                          size_percent=(0.01, 0.1),
                          name="CoarseDropout"),
        iaa.ReplaceElementwise((0.01, 0.05), (0, 255),
                               name="ReplaceElementwise"),
        #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"),
        iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"),
        iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"),
        iaa.CoarseSaltAndPepper((0.01, 0.05),
                                size_percent=(0.01, 0.1),
                                name="CoarseSaltAndPepper"),
        iaa.Salt((0.01, 0.05), name="Salt"),
        iaa.CoarseSalt((0.01, 0.05),
                       size_percent=(0.01, 0.1),
                       name="CoarseSalt"),
        iaa.Pepper((0.01, 0.05), name="Pepper"),
        iaa.CoarsePepper((0.01, 0.05),
                         size_percent=(0.01, 0.1),
                         name="CoarsePepper"),
        iaa.Invert(0.1, name="Invert"),
        # ContrastNormalization
        iaa.JpegCompression((50, 99), name="JpegCompression")
    ]
    augmenters_blend = [
        iaa.Alpha((0.01, 0.99), iaa.Noop(), name="Alpha"),
        iaa.AlphaElementwise((0.01, 0.99), iaa.Noop(),
                             name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha((-2.0, 2.0),
                                iaa.Noop(),
                                name="FrequencyNoiseAlpha")
    ]
    augmenters_blur = [
        iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"),
        iaa.MotionBlur(k=(3, 11), name="MotionBlur")
    ]
    augmenters_color = [
        # InColorspace (deprecated)
        iaa.WithColorspace(to_colorspace="HSV",
                           children=iaa.Noop(),
                           name="WithColorspace"),
        iaa.WithHueAndSaturation(children=iaa.Noop(),
                                 name="WithHueAndSaturation"),
        iaa.MultiplyHueAndSaturation((0.8, 1.2),
                                     name="MultiplyHueAndSaturation"),
        iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"),
        iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"),
        iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"),
        iaa.AddToHue((-10, 10), name="AddToHue"),
        iaa.AddToSaturation((-10, 10), name="AddToSaturation"),
        iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"),
        iaa.Grayscale((0.01, 0.99), name="Grayscale"),
        iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"),
        iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization")
    ]
    augmenters_contrast = [
        iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"),
        iaa.SigmoidContrast(gain=(5, 20),
                            cutoff=(0.25, 0.75),
                            name="SigmoidContrast"),
        iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"),
        iaa.LinearContrast((0.5, 1.5), name="LinearContrast"),
        iaa.AllChannelsCLAHE(clip_limit=(2, 10),
                             tile_grid_size_px=(3, 11),
                             name="AllChannelsCLAHE"),
        iaa.CLAHE(clip_limit=(2, 10),
                  tile_grid_size_px=(3, 11),
                  to_colorspace="HSV",
                  name="CLAHE"),
        iaa.AllChannelsHistogramEqualization(
            name="AllChannelsHistogramEqualization"),
        iaa.HistogramEqualization(to_colorspace="HSV",
                                  name="HistogramEqualization"),
    ]
    augmenters_convolutional = [
        iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]),
                     name="Convolve_3x3"),
        iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"),
        iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect")
    ]
    augmenters_edges = [iaa.Canny(alpha=(0.01, 0.99), name="Canny")]
    augmenters_flip = [
        iaa.Fliplr(1.0, name="Fliplr"),
        iaa.Flipud(1.0, name="Flipud")
    ]
    augmenters_geometric = [
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=0,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_0_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_1_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=3,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_3_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="edge",
                   cval=(0, 255),
                   name="Affine_order_1_edge"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="constant",
                   cval=(0, 255),
                   backend="skimage",
                   name="Affine_order_1_constant_skimage"),
        # TODO AffineCv2
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=1,
                            mode="constant",
                            name="PiecewiseAffine_4x4_order_1_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=0,
                            mode="constant",
                            name="PiecewiseAffine_4x4_order_0_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=1,
                            mode="edge",
                            name="PiecewiseAffine_4x4_order_1_edge"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=8,
                            nb_cols=8,
                            order=1,
                            mode="constant",
                            name="PiecewiseAffine_8x8_order_1_constant"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                 keep_size=False,
                                 name="PerspectiveTransform"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                 keep_size=True,
                                 name="PerspectiveTransform_keep_size"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=0,
            mode="constant",
            cval=0,
            name="ElasticTransformation_order_0_constant"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="constant",
            cval=0,
            name="ElasticTransformation_order_1_constant"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="nearest",
            cval=0,
            name="ElasticTransformation_order_1_nearest"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="reflect",
            cval=0,
            name="ElasticTransformation_order_1_reflect"),
        iaa.Rot90((1, 3), keep_size=False, name="Rot90"),
        iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size")
    ]
    augmenters_pooling = [
        iaa.AveragePooling(kernel_size=(1, 16),
                           keep_size=False,
                           name="AveragePooling"),
        iaa.AveragePooling(kernel_size=(1, 16),
                           keep_size=True,
                           name="AveragePooling_keep_size"),
        iaa.MaxPooling(kernel_size=(1, 16), keep_size=False,
                       name="MaxPooling"),
        iaa.MaxPooling(kernel_size=(1, 16),
                       keep_size=True,
                       name="MaxPooling_keep_size"),
        iaa.MinPooling(kernel_size=(1, 16), keep_size=False,
                       name="MinPooling"),
        iaa.MinPooling(kernel_size=(1, 16),
                       keep_size=True,
                       name="MinPooling_keep_size"),
        iaa.MedianPooling(kernel_size=(1, 16),
                          keep_size=False,
                          name="MedianPooling"),
        iaa.MedianPooling(kernel_size=(1, 16),
                          keep_size=True,
                          name="MedianPooling_keep_size")
    ]
    augmenters_segmentation = [
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=64,
                        interpolation="cubic",
                        name="Superpixels_max_size_64_cubic"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=64,
                        interpolation="linear",
                        name="Superpixels_max_size_64_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=128,
                        interpolation="linear",
                        name="Superpixels_max_size_128_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=224,
                        interpolation="linear",
                        name="Superpixels_max_size_224_linear"),
        iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"),
        iaa.RegularGridVoronoi(n_rows=(16, 31),
                               n_cols=(16, 31),
                               name="RegularGridVoronoi"),
        iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14),
                                       n_cols_frac=(0.07, 0.14),
                                       name="RelativeRegularGridVoronoi"),
    ]
    augmenters_size = [
        iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"),
        iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"),
        iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="constant",
                       pad_cval=(0, 255),
                       keep_size=False,
                       name="CropAndPad"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="edge",
                       pad_cval=(0, 255),
                       keep_size=False,
                       name="CropAndPad_edge"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="constant",
                       pad_cval=(0, 255),
                       name="CropAndPad_keep_size"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="constant",
                pad_cval=(0, 255),
                keep_size=False,
                name="Pad"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="edge",
                pad_cval=(0, 255),
                keep_size=False,
                name="Pad_edge"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="constant",
                pad_cval=(0, 255),
                name="Pad_keep_size"),
        iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"),
        iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"),
        iaa.PadToFixedSize(width=width + 10,
                           height=height + 10,
                           pad_mode="constant",
                           pad_cval=(0, 255),
                           name="PadToFixedSize"),
        iaa.CropToFixedSize(width=width - 10,
                            height=height - 10,
                            name="CropToFixedSize"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="nearest",
                             name="KeepSizeByResize_CropToFixedSize_nearest"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="linear",
                             name="KeepSizeByResize_CropToFixedSize_linear"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="cubic",
                             name="KeepSizeByResize_CropToFixedSize_cubic"),
    ]
    augmenters_weather = [
        iaa.FastSnowyLandscape(lightness_threshold=(100, 255),
                               lightness_multiplier=(1.0, 4.0),
                               name="FastSnowyLandscape"),
        iaa.Clouds(name="Clouds"),
        iaa.Fog(name="Fog"),
        iaa.CloudLayer(intensity_mean=(196, 255),
                       intensity_freq_exponent=(-2.5, -2.0),
                       intensity_coarse_scale=10,
                       alpha_min=0,
                       alpha_multiplier=(0.25, 0.75),
                       alpha_size_px_max=(2, 8),
                       alpha_freq_exponent=(-2.5, -2.0),
                       sparsity=(0.8, 1.0),
                       density_multiplier=(0.5, 1.0),
                       name="CloudLayer"),
        iaa.Snowflakes(name="Snowflakes"),
        iaa.SnowflakesLayer(density=(0.005, 0.075),
                            density_uniformity=(0.3, 0.9),
                            flake_size=(0.2, 0.7),
                            flake_size_uniformity=(0.4, 0.8),
                            angle=(-30, 30),
                            speed=(0.007, 0.03),
                            blur_sigma_fraction=(0.0001, 0.001),
                            name="SnowflakesLayer")
    ]

    augmenters = (augmenters_meta + augmenters_arithmetic + augmenters_blend +
                  augmenters_blur + augmenters_color + augmenters_contrast +
                  augmenters_convolutional + augmenters_edges +
                  augmenters_flip + augmenters_geometric + augmenters_pooling +
                  augmenters_segmentation + augmenters_size +
                  augmenters_weather)

    if only_augmenters is not None:
        augmenters_reduced = []
        for augmenter in augmenters:
            if any([
                    re.search(pattern, augmenter.name)
                    for pattern in only_augmenters
            ]):
                augmenters_reduced.append(augmenter)
        augmenters = augmenters_reduced

    return augmenters
Exemple #27
0
    def augmentation_of_image(self, test_image, output_path):
        self.test_image = test_image
        self.output_path = output_path
        #define the Augmenters

        #properties: A range of values signifies that one of these numbers is randmoly chosen for every augmentation for every batch

        # Apply affine transformations to each image.
        rotate = iaa.Affine(rotate=(-90, 90))
        scale = iaa.Affine(scale={
            "x": (0.5, 0.9),
            "y": (0.5, 0.9)
        })
        translation = iaa.Affine(translate_percent={
            "x": (-0.15, 0.15),
            "y": (-0.15, 0.15)
        })
        shear = iaa.Affine(shear=(-2, 2))
        #plagio parallhlogrammo wihthin a range (-8,8)
        zoom = iaa.PerspectiveTransform(
            scale=(0.01, 0.15),
            keep_size=True)  # do not change the output size of the image
        h_flip = iaa.Fliplr(1.0)
        # flip horizontally all images (100%)
        v_flip = iaa.Flipud(1.0)
        #flip vertically all images
        padding = iaa.KeepSizeByResize(
            iaa.CropAndPad(percent=(0.05, 0.25))
        )  #positive values correspond to padding 5%-25% of the image,but keeping the origial output size of the new image

        #More augmentations
        blur = iaa.GaussianBlur(
            sigma=(0, 1.22)
        )  # blur images with a sigma 0-2,a number ofthis range is randomly chosen everytime.Low values suggested for this application
        contrast = iaa.contrast.LinearContrast((0.75, 1.5))
        #change the contrast by a factor of 0.75 and 1.5 sampled randomly per image
        contrast_channels = iaa.LinearContrast(
            (0.75, 1.5), per_channel=True
        )  #and for 50% of all images also independently per channel:
        sharpen = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))
        #sharpen with an alpha from 0(no sharpening) - 1(full sharpening) and change the lightness form 0.75 to 1.5
        gauss_noise = iaa.AdditiveGaussianNoise(
            scale=0.111 * 255, per_channel=True
        )  #some random gaussian noise might occur in cell images,especially when image quality is poor
        laplace_noise = iaa.AdditiveLaplaceNoise(
            scale=(0, 0.111 * 255)
        )  #we choose to be in a small range, as it is logical for training the cell images

        #Brightness
        brightness = iaa.Multiply(
            (0.35, 1.65
             ))  #change brightness between 35% or 165% of the original image
        brightness_channels = iaa.Multiply(
            (0.5, 1.5), per_channel=0.75
        )  # change birghtness for 25% of images.For the remaining 75%, change it, but also channel-wise.

        #CHANNELS (RGB)=(Red,Green,Blue)
        red = iaa.WithChannels(0, iaa.Add(
            (10,
             100)))  #increase each Red-pixels value within the range 10-100
        red_rot = iaa.WithChannels(0, iaa.Affine(
            rotate=(0, 45)))  #rotate each image's red channel by 0-45 degrees
        green = iaa.WithChannels(1, iaa.Add(
            (10,
             100)))  #increase each Green-pixels value within the range 10-100
        green_rot = iaa.WithChannels(1, iaa.Affine(
            rotate=(0,
                    45)))  #rotate each image's green channel by 0-45 degrees
        blue = iaa.WithChannels(2, iaa.Add(
            (10,
             100)))  #increase each Blue-pixels value within the range 10-100
        blue_rot = iaa.WithChannels(2, iaa.Affine(
            rotate=(0, 45)))  #rotate each image's blue channel by 0-45 degrees

        #colors
        channel_shuffle = iaa.ChannelShuffle(1.0)
        #shuffle all images of the batch
        grayscale = iaa.Grayscale(1.0)
        hue_n_saturation = iaa.MultiplyHueAndSaturation(
            (0.5, 1.5), per_channel=True
        )  #change hue and saturation with this range of values for different values
        add_hue_saturation = iaa.AddToHueAndSaturation(
            (-50, 50),
            per_channel=True)  #add more hue and saturation to its pixels
        #Quantize colors using k-Means clustering
        kmeans_color = iaa.KMeansColorQuantization(
            n_colors=(4, 16)
        )  #quantizes to k means 4 to 16 colors (randomly chosen). Quantizes colors up to 16 colors

        #Alpha Blending
        blend = iaa.AlphaElementwise((0, 1.0), iaa.Grayscale((0, 1.0)))
        #blend depending on which value is greater

        #Contrast augmentors
        clahe = iaa.CLAHE(tile_grid_size_px=((3, 21), [
            0, 2, 3, 4, 5, 6, 7
        ]))  #create a clahe contrast augmentor H=(3,21) and W=(0,7)
        histogram = iaa.HistogramEqualization(
        )  #performs histogram equalization

        #Augmentation list of metadata augmentors
        OneofRed = iaa.OneOf([red])
        OneofGreen = iaa.OneOf([green])
        OneofBlue = iaa.OneOf([blue])
        contrast_n_shit = iaa.OneOf(
            [contrast, brightness, brightness_channels])
        SomeAug = iaa.SomeOf(
            2, [rotate, scale, translation, shear, h_flip, v_flip],
            random_order=True)
        SomeClahe = iaa.SomeOf(
            2, [
                clahe,
                iaa.CLAHE(clip_limit=(1, 10)),
                iaa.CLAHE(tile_grid_size_px=(3, 21)),
                iaa.GammaContrast((0.5, 2.0)),
                iaa.AllChannelsCLAHE(),
                iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True)
            ],
            random_order=True)  #Random selection from clahe augmentors
        edgedetection = iaa.OneOf([
            iaa.EdgeDetect(alpha=(0, 0.7)),
            iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0))
        ])
        # Search in some images either for all edges or for directed edges.These edges are then marked in a black and white image and overlayed with the original image using an alpha of 0 to 0.7.
        canny_filter = iaa.OneOf([
            iaa.Canny(),
            iaa.Canny(alpha=(0.5, 1.0), sobel_kernel_size=[3, 7])
        ])
        #choose one of the 2 canny filter options
        OneofNoise = iaa.OneOf([blur, gauss_noise, laplace_noise])
        Color_1 = iaa.OneOf([
            channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation,
            kmeans_color
        ])
        Color_2 = iaa.OneOf([
            channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation,
            kmeans_color
        ])
        Flip = iaa.OneOf([histogram, v_flip, h_flip])

        #Define the augmentors used in the DA
        Augmentors = [
            SomeAug, SomeClahe, SomeClahe, edgedetection, sharpen,
            canny_filter, OneofRed, OneofGreen, OneofBlue, OneofNoise, Color_1,
            Color_2, Flip, contrast_n_shit
        ]

        for i in range(0, 14):
            img = cv2.imread(test_image)  #read you image
            images = np.array(
                [img for _ in range(14)], dtype=np.uint8
            )  # 12 is the size of the array that will hold 8 different images
            images_aug = Augmentors[i].augment_images(
                images
            )  #alternate between the different augmentors for a test image
            cv2.imwrite(
                os.path.join(output_path,
                             test_image + "new" + str(i) + '.jpg'),
                images_aug[i])  #write all changed images