def test_bgr2hsv(self): in_img = np.random.rand(10, 10, 3).astype(np.float32) out_img = mmcv.bgr2hsv(in_img) argmax = in_img.argmax(axis=2) computed_hsv = np.empty_like(in_img, dtype=in_img.dtype) for i in range(in_img.shape[0]): for j in range(in_img.shape[1]): b = in_img[i, j, 0] g = in_img[i, j, 1] r = in_img[i, j, 2] v = max(r, g, b) s = (v - min(r, g, b)) / v if v != 0 else 0 if argmax[i, j] == 0: h = 240 + 60 * (r - g) / (v - min(r, g, b)) elif argmax[i, j] == 1: h = 120 + 60 * (b - r) / (v - min(r, g, b)) else: h = 60 * (g - b) / (v - min(r, g, b)) if h < 0: h += 360 computed_hsv[i, j, :] = [h, s, v] assert_array_almost_equal(out_img, computed_hsv, decimal=2)
def __call__(self, results): """Call function to perform photometric distortion on images. Args: results (dict): Result dict from loading pipeline. Returns: dict: Result dict with images distorted. """ img = results['img'] # random brightness img = self.brightness(img) # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: img = self.contrast(img) hsv_mode = random.randint(4) if hsv_mode: # random saturation/hue distortion img = mmcv.bgr2hsv(img) if hsv_mode == 1 or hsv_mode == 3: img = self.saturation(img) if hsv_mode == 2 or hsv_mode == 3: img = self.hue(img) img = mmcv.hsv2bgr(img) # random contrast if mode == 0: img = self.contrast(img) # randomly swap channels self.swap_channels(img) results['img'] = img return results
def __call__(self, results): img = results['img'] if 'ref_img' in results: ref_img = results['ref_img'] else: ref_img = None # random brightness if random.randint(2): delta = random.uniform(-self.brightness_delta, self.brightness_delta) img += delta if ref_img is not None: ref_img += delta # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha if ref_img is not None: ref_img *= alpha # convert color from BGR to HSV img = mmcv.bgr2hsv(img) if ref_img is not None: ref_img = mmcv.bgr2hsv(ref_img) # random saturation if random.randint(2): random_saturation = random.uniform(self.saturation_lower, self.saturation_upper) img[..., 1] *= random_saturation if ref_img is not None: ref_img[..., 1] *= random_saturation # random hue if random.randint(2): random_hue = random.uniform(-self.hue_delta, self.hue_delta) img[..., 0] += random_hue img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 if ref_img is not None: ref_img[..., 0] += random_hue ref_img[..., 0][ref_img[..., 0] > 360] -= 360 ref_img[..., 0][ref_img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) if ref_img is not None: ref_img = mmcv.hsv2bgr(ref_img) # random contrast if mode == 0: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha if ref_img is not None: ref_img *= alpha # randomly swap channels if random.randint(2): random_permute = random.permutation(3) img = img[..., random_permute] if ref_img is not None: ref_img = ref_img[..., random_permute] results['img'] = img if ref_img is not None: results['ref_img'] = ref_img return results
def __call__(self, img, boxes, labels, masks=None): if self.color_choose == 0: if random.uniform() < self.gray_p: gray = mmcv.bgr2gray(img) img = mmcv.gray2bgr(gray) return img, boxes, labels, masks # random brightness if random.randint(2): delta = random.uniform(-self.brightness_delta, self.brightness_delta) img += delta # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha # convert color from BGR to HSV img = mmcv.bgr2hsv(img) # random saturation if random.randint(2): img[..., 1] *= random.uniform(self.saturation_lower, self.saturation_upper) # random hue if random.randint(2): img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) # random contrast if mode == 0: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha # randomly swap channels if random.randint(2): img = img[..., random.permutation(3)] else: if self.color_choose == 1: # random brightness if random.randint(2): delta = random.uniform(-self.brightness_delta, self.brightness_delta) img += delta elif self.color_choose == 2: # random contrast first if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha else: # convert color from BGR to HSV img = mmcv.bgr2hsv(img) if self.color_choose == 3: # random saturation if random.randint(2): img[..., 1] *= random.uniform(self.saturation_lower, self.saturation_upper) if self.color_choose == 4: # random hue if random.randint(2): img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) return img, boxes, labels, masks
def __call__(self, results): print('start') import copy img = copy.deepcopy(results['img']) print(img.shape) img[0,0,0]+=1 print('loaded') # random brightness if random.randint(2): print('a') delta = random.uniform(-self.brightness_delta, self.brightness_delta) print(delta) print(img.shape) print('success') print(delta.shape) img += delta print('exit') print('first') # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: if random.randint(2): print('exit1') alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha print(img.shape) # convert color from BGR to HSV print('bgr') img = mmcv.bgr2hsv(img) print('mid') # random saturation if random.randint(2): img[..., 1] *= 2#random.uniform(self.saturation_lower, #self.saturation_upper) print('exit2') # random hue if random.randint(2): img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 print(img.shape) # convert color from HSV to BGR img = mmcv.hsv2bgr(img) # random contrast if mode == 0: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha print(img.shape) # randomly swap channels if random.randint(2): img = img[..., random.permutation(3)] results['img'] = img print('end') return results
def __call__(self, results): print('photo metric distortion') i += 1 img = results['img'] template_img = results['template_img'] # random brightness if random.randint(2): delta = random.uniform(-self.brightness_delta, self.brightness_delta) img += delta template_img += delta # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha template_img *= alpha # convert color from BGR to HSV img = mmcv.bgr2hsv(img) template_img = mmcv.bgr2hsv(template_img) # random saturation if random.randint(2): i += 1 img[..., 1] *= random.uniform(self.saturation_lower, self.saturation_upper) template_img[..., 1] *= random.uniform(self.saturation_lower, self.saturation_upper) # random hue if random.randint(2): i += 1 img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 template_img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) template_img[..., 0][template_img[..., 0] > 360] -= 360 template_img[..., 0][template_img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) template_img = mmcv.hsv2bgr(template_img) # random contrast if mode == 0: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha template_img *= alpha # randomly swap channels if random.randint(2): i += 1 img = img[..., random.permutation(3)] template_img = template_img[..., random.permutation(3)] results['img'] = img results['template_img'] = template_img return results
def __call__(self, results): """Call function to perform photometric distortion on images. Args: results (dict): Result dict from loading pipeline. Returns: dict: Result dict with images distorted. """ if 'img_fields' in results: assert results['img_fields'] == ['img'], \ 'Only single img_fields is allowed' img = results['img'] assert img.dtype == np.float32, \ 'PhotoMetricDistortion needs the input image of dtype np.float32,'\ ' please set "to_float32=True" in "LoadImageFromFile" pipeline' # random brightness if random.randint(2): delta = random.uniform(-self.brightness_delta, self.brightness_delta) img += delta # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = random.randint(2) if mode == 1: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha # convert color from BGR to HSV img = mmcv.bgr2hsv(img) # random saturation if random.randint(2): img[..., 1] *= random.uniform(self.saturation_lower, self.saturation_upper) # random hue if random.randint(2): img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta) img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) # random contrast if mode == 0: if random.randint(2): alpha = random.uniform(self.contrast_lower, self.contrast_upper) img *= alpha # randomly swap channels if random.randint(2): img = img[..., random.permutation(3)] results['img'] = img return results
def __call__(self, results, state=None): if state is None: delta_randint = random.randint(2) delta = random.uniform(-self.brightness_delta, self.brightness_delta) mode = random.randint(2) mode_randint = random.randint(2) alpha = random.uniform(self.contrast_lower, self.contrast_upper) sat_randint = random.randint(2) sat_multiplier = random.uniform(self.saturation_lower, self.saturation_upper) hue_randint = random.randint(2) hue_multiplier = random.uniform(-self.hue_delta, self.hue_delta) contrast_randint = random.randint(2) contrast_alpha = random.uniform(self.contrast_lower, self.contrast_upper) perm_randint = random.randint(2) perm = random.permutation(3) state = dict(delta_randint=delta_randint, delta=delta, mode=mode, mode_randint=mode_randint, alpha=alpha, sat_randint=sat_randint, sat_multiplier=sat_multiplier, hue_randint=hue_randint, hue_multiplier=hue_multiplier, contrast_randint=contrast_randint, contrast_alpha=contrast_alpha, perm_randint=perm_randint, perm=perm) else: delta_randint = state['delta_randint'] delta = state['delta'] mode = state['mode'] mode_randint = state['mode_randint'] alpha = state['alpha'] sat_randint = state['sat_randint'] sat_multiplier = state['sat_multiplier'] hue_randint = state['hue_randint'] hue_multiplier = state['hue_multiplier'] contrast_randint = state['contrast_randint'] contrast_alpha = state['contrast_alpha'] perm_randint = state['perm_randint'] perm = state['perm'] img = results['img'] # random brightness if delta_randint: delta = delta img += delta # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = mode if mode == 1: if mode_randint: alpha = alpha img *= alpha # convert color from BGR to HSV img = mmcv.bgr2hsv(img) # random saturation if sat_randint: img[..., 1] *= sat_multiplier # random hue if hue_randint: img[..., 0] += hue_multiplier img[..., 0][img[..., 0] > 360] -= 360 img[..., 0][img[..., 0] < 0] += 360 # convert color from HSV to BGR img = mmcv.hsv2bgr(img) # random contrast if mode == 0: if contrast_randint: alpha = contrast_alpha img *= alpha # randomly swap channels if perm_randint: img = img[..., perm] results['img'] = img return results, state
def random_photo_metric_distortion(self, img): # https://github.com/open-mmlab/mmdetection/blob/a054aef422d650a644459bdc232248eefa8ae8b7/mmdet/datasets/extra_aug.py#L8 # brightness_delta=32 # contrast_range=(0.5, 1.5), # saturation_range=(0.5, 1.5), # hue_delta=18) # brightness_delta = photo_metric_distortion['brightness_delta'] # contrast_lower, contrast_upper = photo_metric_distortion['contrast_range'] # saturation_lower, saturation_upper = photo_metric_distortion['saturation_range'] # hue_delta = self.photo_metric_distortion['hue_delta'] brightness_delta = 32 contrast_lower, contrast_upper = (0.5, 1.5) saturation_lower, saturation_upper = (0.5, 1.5) hue_delta = 18 # change datatype before do photo metric distortion img = img.astype(np.float32) # random brightness if np.random.randint(2): delta = np.random.uniform(-brightness_delta, brightness_delta) img += delta img = img.clip(min=0, max=255) # mode == 0 --> do random contrast first # mode == 1 --> do random contrast last mode = np.random.randint(2) if mode == 1: if np.random.randint(2): alpha = np.random.uniform(contrast_lower, contrast_upper) img *= alpha img = img.clip(min=0, max=255) # convert color from BGR to HSV img = mmcv.bgr2hsv(img) # random saturation if np.random.randint(2): img[..., 1] *= np.random.uniform(saturation_lower, saturation_upper) img[..., 1] = img[..., 1].clip(min=0, max=1) # random hue if np.random.randint(2): img[..., 0] += np.random.uniform(-hue_delta, hue_delta) # img[..., 0][img[..., 0] > 360] -= 360 # img[..., 0][img[..., 0] < 0] += 360 img[..., 0] = img[..., 0].clip(min=0, max=360) # convert color from HSV to BGR img = mmcv.hsv2bgr(img) # random contrast if mode == 0: if np.random.randint(2): alpha = np.random.uniform(contrast_lower, contrast_upper) img *= alpha img = img.clip(min=0, max=255) # randomly swap channels # if np.random.randint(2): # img = img[..., np.random.permutation(3)] return img.astype(np.uint8)
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) mmcv.imshow(resized_img[0])