Ejemplo n.º 1
0
    def test_adjust_brightness(self, nb_rand_test=100):
        def _adjust_brightness(img, factor):
            # adjust the brightness of image using
            # PIL.ImageEnhance.Brightness
            from PIL.ImageEnhance import Brightness
            from PIL import Image
            img = Image.fromarray(img)
            brightened_img = Brightness(img).enhance(factor)
            return np.asarray(brightened_img)

        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 with factor 1.0
        assert_array_equal(mmcv.adjust_brightness(img, 1.), img)
        # test case with factor 0.0
        assert_array_equal(mmcv.adjust_brightness(img, 0.), np.zeros_like(img))
        # test adjust_brightness with randomly sampled images and factors.
        for _ in range(nb_rand_test):
            img = np.clip(
                np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0,
                255).astype(np.uint8)
            factor = np.random.uniform()
            np.testing.assert_allclose(
                mmcv.adjust_brightness(img, factor).astype(np.int32),
                _adjust_brightness(img, factor).astype(np.int32),
                rtol=0,
                atol=1)
Ejemplo n.º 2
0
 def __call__(self, results):
     if np.random.rand() > self.prob:
         return results
     magnitude = random_negative(self.magnitude, self.random_negative_prob)
     for key in results.get('img_fields', ['img']):
         img = results[key]
         img_brightened = mmcv.adjust_brightness(img, factor=1 + magnitude)
         results[key] = img_brightened.astype(img.dtype)
     return results
Ejemplo n.º 3
0
 def __call__(self, results):
     for i, modal in enumerate(results['modality']):
         if modal == 'rgb':
             video = results['video'][i]
             bright = np.random.uniform(max(0, 1 - self.brightness),
                                        1 + self.brightness)
             contrast = np.random.uniform(max(0, 1 - self.contrast),
                                          1 + self.contrast)
             video = mmcv.adjust_brightness(video.astype(np.int32), bright)
             num_frames = video.shape[0]
             video = video.astype(np.uint8).reshape(-1, video.shape[2], 3)
             video = mmcv.adjust_contrast(video, contrast).reshape(
                 num_frames, -1, video.shape[1], 3)
             results['video'][i] = video
     return results
Ejemplo n.º 4
0
 def _adjust_brightness_img(self, results, factor=1.0):
     """Adjust the brightness of image."""
     for key in results.get('img_fields', ['img']):
         img = results[key]
         results[key] = mmcv.adjust_brightness(img,
                                               factor).astype(img.dtype)