Ejemplo n.º 1
0
 def __call__(self, results):
     if np.random.rand() > self.prob:
         return results
     for key in results.get('img_fields', ['img']):
         img = results[key]
         img_contrasted = mmcv.auto_contrast(img)
         results[key] = img_contrasted.astype(img.dtype)
     return results
Ejemplo n.º 2
0
    def test_auto_contrast(self, nb_rand_test=100):
        def _auto_contrast(img, cutoff=0):
            from PIL import Image
            from PIL.ImageOps import autocontrast

            # Image.fromarray defaultly supports RGB, not BGR.
            # convert from BGR to RGB
            img = Image.fromarray(img[..., ::-1], mode='RGB')
            contrasted_img = autocontrast(img, cutoff)
            # convert from RGB to BGR
            return np.asarray(contrasted_img)[..., ::-1]

        img = np.array([[0, 128, 255], [1, 127, 254], [2, 129, 253]],
                       dtype=np.uint8)
        img = np.stack([img, img, img], axis=-1)

        # test case without cut-off
        assert_array_equal(mmcv.auto_contrast(img), _auto_contrast(img))
        # test case with cut-off as int
        assert_array_equal(mmcv.auto_contrast(img, 10),
                           _auto_contrast(img, 10))
        # test case with cut-off as float
        assert_array_equal(mmcv.auto_contrast(img, 12.5),
                           _auto_contrast(img, 12.5))
        # test case with cut-off as tuple
        assert_array_equal(mmcv.auto_contrast(img, (10, 10)),
                           _auto_contrast(img, 10))
        # test case with cut-off with sum over 100
        assert_array_equal(mmcv.auto_contrast(img, 60),
                           _auto_contrast(img, 60))

        # test auto_contrast with randomly sampled images and factors.
        for _ in range(nb_rand_test):
            img = np.clip(
                np.random.uniform(0, 1, (1200, 1000, 3)) * 260, 0,
                255).astype(np.uint8)
            # cut-offs are not set as tuple since in `build.yml`, pillow 6.2.2
            # is installed, which does not support setting low cut-off and high
            #  cut-off differently.
            # With pillow above 8.0.0, cutoff can be set as tuple
            cutoff = np.random.rand() * 100
            assert_array_equal(mmcv.auto_contrast(img, cutoff),
                               _auto_contrast(img, cutoff))