def test_rgb2gray(self): in_img = np.random.rand(10, 10, 3).astype(np.float32) out_img = mmcv.rgb2gray(in_img) computed_gray = (in_img[:, :, 0] * 0.299 + in_img[:, :, 1] * 0.587 + in_img[:, :, 2] * 0.114) assert_array_almost_equal(out_img, computed_gray, decimal=4) out_img_3d = mmcv.rgb2gray(in_img, True) assert out_img_3d.shape == (10, 10, 1) assert_array_almost_equal(out_img_3d[..., 0], out_img, decimal=4)
def __call__(self, results): """ Args: img (ndarray): Image to be converted to grayscale. Returns: ndarray: Randomly grayscaled image. """ for key in results.get('img_fields', ['img']): img = results[key] num_output_channels = img.shape[2] if random.random() < self.gray_prob: if num_output_channels > 1: img = mmcv.rgb2gray(img)[:, :, None] results[key] = np.dstack( [img for _ in range(num_output_channels)]) return results results[key] = img return results
mmcv.imshow(img, win_name='test image', wait_time=200) if flag_4: # Color space conversion """ Supported conversion methods: bgr2gray gray2bgr bgr2rgb rgb2bgr bgr2hsv hsv2bgr """ img = mmcv.imread("asset/a.jpg") img1 = mmcv.bgr2rgb(img) img2 = mmcv.rgb2gray(img1) img3 = mmcv.bgr2hsv(img) mmcv.imshow(img1) mmcv.imshow(img2) mmcv.imshow(img3) if flag_5: # Resize """ There are three resize methods. All imresize_* methods have an argument return_scale, if this argument is False, then the return value is merely the resized image, otherwise is a tuple (resized_img, scale). """ img = mmcv.imread("asset/a.jpg") dst_img = mmcv.imread("asset/b.jpg") # resize to a given size resized_img = mmcv.imresize(img, (1000, 600), return_scale=True)