def test_adjust_contrast(self, nb_rand_test=100): def _adjust_contrast(img, factor): from PIL.ImageEnhance import Contrast from PIL import Image # Image.fromarray defaultly supports RGB, not BGR. # convert from BGR to RGB img = Image.fromarray(img[..., ::-1], mode='RGB') contrasted_img = Contrast(img).enhance(factor) # 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 with factor 1.0 assert_array_equal(mmcv.adjust_contrast(img, 1.), img) # test case with factor 0.0 assert_array_equal(mmcv.adjust_contrast(img, 0.), _adjust_contrast(img, 0.)) # test adjust_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) factor = np.random.uniform() # Note the gap (less_equal 1) between PIL.ImageEnhance.Contrast # and mmcv.adjust_contrast comes from the gap that converts from # a color image to gray image using mmcv or PIL. np.testing.assert_allclose( mmcv.adjust_contrast(img, factor).astype(np.int32), _adjust_contrast(img, factor).astype(np.int32), rtol=0, atol=1)
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_contrasted = mmcv.adjust_contrast(img, factor=1 + magnitude) results[key] = img_contrasted.astype(img.dtype) return results
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
def _adjust_contrast_img(self, results, factor=1.0): """Adjust the image contrast.""" for key in results.get('img_fields', ['img']): img = results[key] results[key] = mmcv.adjust_contrast(img, factor).astype(img.dtype)