def test_imequalize(self, nb_rand_test=100): def _imequalize(img): # equalize the image using PIL.ImageOps.equalize from PIL import ImageOps, Image img = Image.fromarray(img) equalized_img = np.asarray(ImageOps.equalize(img)) return equalized_img img = np.array([[0, 128, 255], [1, 127, 254], [2, 129, 253]], dtype=np.uint8) img = np.stack([img, img, img], axis=-1) equalized_img = mmcv.imequalize(img) assert_array_equal(equalized_img, _imequalize(img)) # test equalize with case step=0 img = np.array([[0, 0, 0], [120, 120, 120], [255, 255, 255]], dtype=np.uint8) img = np.stack([img, img, img], axis=-1) assert_array_equal(mmcv.imequalize(img), img) # test equalize with randomly sampled image. for _ in range(nb_rand_test): img = np.clip( np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0, 255).astype(np.uint8) equalized_img = mmcv.imequalize(img) assert_array_equal(equalized_img, _imequalize(img))
def __call__(self, results): if np.random.rand() > self.prob: return results for key in results.get('img_fields', ['img']): img = results[key] img_equalized = mmcv.imequalize(img) results[key] = img_equalized.astype(img.dtype) return results
def _imequalize(self, results): """Equalizes the histogram of one image.""" for key in results.get('img_fields', ['img']): img = results[key] results[key] = mmcv.imequalize(img).astype(img.dtype)