Python cv2.COLOR_HSV2BGR Examples
The following are 30
code examples of cv2.COLOR_HSV2BGR().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: video2tfrecord.py From video2tfrecord with MIT License | 8 votes |
def compute_dense_optical_flow(prev_image, current_image): old_shape = current_image.shape prev_image_gray = cv2.cvtColor(prev_image, cv2.COLOR_BGR2GRAY) current_image_gray = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY) assert current_image.shape == old_shape hsv = np.zeros_like(prev_image) hsv[..., 1] = 255 flow = None flow = cv2.calcOpticalFlowFarneback(prev=prev_image_gray, next=current_image_gray, flow=flow, pyr_scale=0.8, levels=15, winsize=5, iterations=10, poly_n=5, poly_sigma=0, flags=10) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
Example #2
Source File: train.py From kaggle_carvana_segmentation with MIT License | 7 votes |
def random_hue_saturation_value(image, hue_shift_limit=(-180, 180), sat_shift_limit=(-255, 255), val_shift_limit=(-255, 255)): image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(image) hue_shift = np.random.uniform(hue_shift_limit[0], hue_shift_limit[1]) h = cv2.add(h, hue_shift) sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1]) s = cv2.add(s, sat_shift) val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1]) v = cv2.add(v, val_shift) image = cv2.merge((h, s, v)) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #3
Source File: datasets.py From pruning_yolov3 with GNU General Public License v3.0 | 7 votes |
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): x = (np.random.uniform(-1, 1, 3) * np.array([hgain, sgain, vgain]) + 1).astype(np.float32) # random gains img_hsv = (cv2.cvtColor(img, cv2.COLOR_BGR2HSV) * x.reshape((1, 1, 3))).clip(None, 255).astype(np.uint8) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed # def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): # original version # # SV augmentation by 50% # img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # hue, sat, val # # S = img_hsv[:, :, 1].astype(np.float32) # saturation # V = img_hsv[:, :, 2].astype(np.float32) # value # # a = random.uniform(-1, 1) * sgain + 1 # b = random.uniform(-1, 1) * vgain + 1 # S *= a # V *= b # # img_hsv[:, :, 1] = S if a < 1 else S.clip(None, 255) # img_hsv[:, :, 2] = V if b < 1 else V.clip(None, 255) # cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
Example #4
Source File: datasets.py From yolov3-channel-and-layer-pruning with Apache License 2.0 | 6 votes |
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): x = (np.random.uniform(-1, 1, 3) * np.array([hgain, sgain, vgain]) + 1).astype(np.float32) # random gains img_hsv = (cv2.cvtColor(img, cv2.COLOR_BGR2HSV) * x.reshape((1, 1, 3))).clip(None, 255).astype(np.uint8) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed # def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): # original version # # SV augmentation by 50% # img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # hue, sat, val # # S = img_hsv[:, :, 1].astype(np.float32) # saturation # V = img_hsv[:, :, 2].astype(np.float32) # value # # a = random.uniform(-1, 1) * sgain + 1 # b = random.uniform(-1, 1) * vgain + 1 # S *= a # V *= b # # img_hsv[:, :, 1] = S if a < 1 else S.clip(None, 255) # img_hsv[:, :, 2] = V if b < 1 else V.clip(None, 255) # cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
Example #5
Source File: util.py From CrowdFlow with GNU General Public License v3.0 | 6 votes |
def flow2RGB(flow, max_flow_mag = 5): """ Color-coded visualization of optical flow fields # Arguments flow: array of shape [:,:,2] containing optical flow max_flow_mag: maximal expected flow magnitude used to normalize. If max_flow_mag < 0 the maximal magnitude of the optical flow field will be used """ hsv_mat = np.ones(shape=(flow.shape[0], flow.shape[1], 3), dtype=np.float32) * 255 ee = cv2.sqrt(flow[:, :, 0] * flow[:, :, 0] + flow[:, :, 1] * flow[:, :, 1]) angle = np.arccos(flow[:, :, 0]/ ee) angle[flow[:, :, 0] == 0] = 0 angle[flow[:, :, 1] == 0] = 6.2831853 - angle[flow[:, :, 1] == 0] angle = angle * 180 / 3.141 hsv_mat[:,:,0] = angle if max_flow_mag < 0: max_flow_mag = ee.max() hsv_mat[:,:,1] = ee * 255.0 / max_flow_mag ret, hsv_mat[:,:,1] = cv2.threshold(src=hsv_mat[:,:,1], maxval=255, thresh=255, type=cv2.THRESH_TRUNC ) rgb_mat = cv2.cvtColor(hsv_mat.astype(np.uint8), cv2.COLOR_HSV2BGR) return rgb_mat
Example #6
Source File: image.py From DeepForest with MIT License | 6 votes |
def __call__(self, image): """ Apply a visual effect on the image. Args image: Image to adjust """ if self.contrast_factor: image = adjust_contrast(image, self.contrast_factor) if self.brightness_delta: image = adjust_brightness(image, self.brightness_delta) if self.hue_delta or self.saturation_factor: image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) if self.hue_delta: image = adjust_hue(image, self.hue_delta) if self.saturation_factor: image = adjust_saturation(image, self.saturation_factor) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #7
Source File: visual_augmentation.py From face_landmark with Apache License 2.0 | 6 votes |
def __call__(self, image): if self.contrast_range is not None: contrast_factor = _uniform(self.contrast_range) image = adjust_contrast(image,contrast_factor) if self.brightness_range is not None: brightness_delta = _uniform(self.brightness_range) image = adjust_brightness(image, brightness_delta) if self.hue_range is not None or self.saturation_range is not None: image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) if self.hue_range is not None: hue_delta = _uniform(self.hue_range) image = adjust_hue(image, hue_delta) if self.saturation_range is not None: saturation_factor = _uniform(self.saturation_range) image = adjust_saturation(image, saturation_factor) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #8
Source File: transforms.py From SSD-variants with MIT License | 6 votes |
def __call__(self, img): assert img.ndim == 3 and img.shape[2] == 3 if self.random.random_sample() >= self.prob: return img var = self.random.uniform(-self.var, self.var) to_HSV, from_HSV = [(cv2.COLOR_RGB2HSV, cv2.COLOR_HSV2RGB), (cv2.COLOR_BGR2HSV, cv2.COLOR_HSV2BGR)][self.random.randint(2)] hsv = cv2.cvtColor(img, to_HSV).astype(np.float32) hue = hsv[:, :, 0] / 179. + var hue = hue - np.floor(hue) hsv[:, :, 0] = hue * 179. img = cv2.cvtColor(hsv.astype('uint8'), from_HSV) return img
Example #9
Source File: util.py From CrowdFlow with GNU General Public License v3.0 | 6 votes |
def flow2RGB(flow, max_flow_mag = 5): """ Color-coded visualization of optical flow fields # Arguments flow: array of shape [:,:,2] containing optical flow max_flow_mag: maximal expected flow magnitude used to normalize. If max_flow_mag < 0 the maximal magnitude of the optical flow field will be used """ hsv_mat = np.ones(shape=(flow.shape[0], flow.shape[1], 3), dtype=np.float32) * 255 ee = cv2.sqrt(flow[:, :, 0] * flow[:, :, 0] + flow[:, :, 1] * flow[:, :, 1]) angle = np.arccos(flow[:, :, 0]/ ee) angle[flow[:, :, 0] == 0] = 0 angle[flow[:, :, 1] == 0] = 6.2831853 - angle[flow[:, :, 1] == 0] angle = angle * 180 / 3.141 hsv_mat[:,:,0] = angle if max_flow_mag < 0: max_flow_mag = ee.max() hsv_mat[:,:,1] = ee * 220.0 / max_flow_mag ret, hsv_mat[:,:,1] = cv2.threshold(src=hsv_mat[:,:,1], maxval=255, thresh=255, type=cv2.THRESH_TRUNC ) rgb_mat = cv2.cvtColor(hsv_mat.astype(np.uint8), cv2.COLOR_HSV2BGR) return rgb_mat
Example #10
Source File: cv2_aug_transforms.py From openseg.pytorch with MIT License | 6 votes |
def __call__(self, img, labelmap=None, maskmap=None): assert isinstance(img, np.ndarray) assert labelmap is None or isinstance(labelmap, np.ndarray) assert maskmap is None or isinstance(maskmap, np.ndarray) if random.random() > self.ratio: return img, labelmap, maskmap img = img.astype(np.float32) img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) img[:, :, 0] += random.uniform(-self.delta, self.delta) img[:, :, 0][img[:, :, 0] > 360] -= 360 img[:, :, 0][img[:, :, 0] < 0] += 360 img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR) img = np.clip(img, 0, 255).astype(np.uint8) return img, labelmap, maskmap
Example #11
Source File: mobileface_makeup.py From MobileFace with MIT License | 6 votes |
def face_whiten(self, im_bgr, whiten_rate=0.15): """Face whitening. Parameters ---------- im_bgr: mat The Mat data format of reading from the original image using opencv. whiten_rate: float, default is 0.15 The face whitening rate. Returns ------- type: mat The result of face whitening. """ im_hsv = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2HSV) im_hsv[:,:,-1] = np.minimum(im_hsv[:,:,-1] * (1 + whiten_rate), 255).astype('uint8') im_whiten = cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR) return im_whiten
Example #12
Source File: data.py From FRRN with MIT License | 6 votes |
def augment(self, image, target): """Augments the data. Args: image: The image. target: The target image. Returns: A tuple of augmented image and target image. """ # Sample the color factor. factor = np.random.uniform(self._min_delta, self._max_delta) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hsv_image[:, :, 1] *= factor hsv_image[:, :, 1] = np.clip(hsv_image[:, :, 1], 0.0, 1.0) image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) return image, target
Example #13
Source File: data.py From FRRN with MIT License | 6 votes |
def augment(self, image, target): """Augments the data. Args: image: The image. target: The target image. Returns: A tuple of augmented image and target image. """ # Sample the color factor. factor = np.random.uniform(self._min_delta, self._max_delta) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hsv_image[:, :, 0] += factor # Make sure the values are in [-360, 360]. hsv_image[:, :, 0] += 360 * (hsv_image[:, :, 0] < 360) hsv_image[:, :, 0] -= 360 * (hsv_image[:, :, 0] > 360) image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) return image, target
Example #14
Source File: imgproc.py From graph_distillation with Apache License 2.0 | 6 votes |
def proc_oflow(images): h, w = images.shape[-3:-1] processed_images = [] for image in images: hsv = np.zeros((h, w, 3), dtype=np.uint8) hsv[:, :, 0] = 255 hsv[:, :, 1] = 255 mag, ang = cv2.cartToPolar(image[..., 0], image[..., 1]) hsv[..., 0] = ang*180/np.pi/2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) processed_image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) processed_images.append(processed_image) return np.stack(processed_images)
Example #15
Source File: augmentation.py From faceboxes-tensorflow with Apache License 2.0 | 6 votes |
def __call__(self, image): if self.contrast_range is not None: contrast_factor = _uniform(self.contrast_range) image = adjust_contrast(image,contrast_factor) if self.brightness_range is not None: brightness_delta = _uniform(self.brightness_range) image = adjust_brightness(image, brightness_delta) if self.hue_range is not None or self.saturation_range is not None: image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) if self.hue_range is not None: hue_delta = _uniform(self.hue_range) image = adjust_hue(image, hue_delta) if self.saturation_range is not None: saturation_factor = _uniform(self.saturation_range) image = adjust_saturation(image, saturation_factor) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #16
Source File: train.py From Kaggle-Carvana-Image-Masking-Challenge with MIT License | 6 votes |
def randomHueSaturationValue(image, hue_shift_limit=(-180, 180), sat_shift_limit=(-255, 255), val_shift_limit=(-255, 255), u=0.5): if np.random.random() < u: image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(image) hue_shift = np.random.uniform(hue_shift_limit[0], hue_shift_limit[1]) h = cv2.add(h, hue_shift) sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1]) s = cv2.add(s, sat_shift) val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1]) v = cv2.add(v, val_shift) image = cv2.merge((h, s, v)) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #17
Source File: augmentations.py From nn_tools with MIT License | 5 votes |
def __call__(self, image, *args): BGR_img = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) if len(args): return (BGR_img, *args) else: return BGR_img
Example #18
Source File: process.py From lowpolypy with MIT License | 5 votes |
def saturation(image: np.ndarray, alpha: float, beta: float): hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hsv[..., 1] = cv2.convertScaleAbs(hsv[..., 1], alpha=alpha, beta=beta) image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return image
Example #19
Source File: data_augment.py From M2Det with MIT License | 5 votes |
def _distort(image): def _convert(image, alpha=1, beta=0): tmp = image.astype(float) * alpha + beta tmp[tmp < 0] = 0 tmp[tmp > 255] = 255 image[:] = tmp image = image.copy() if random.randrange(2): _convert(image, beta=random.uniform(-32, 32)) if random.randrange(2): _convert(image, alpha=random.uniform(0.5, 1.5)) image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) if random.randrange(2): tmp = image[:, :, 0].astype(int) + random.randint(-18, 18) tmp %= 180 image[:, :, 0] = tmp if random.randrange(2): _convert(image[:, :, 1], alpha=random.uniform(0.5, 1.5)) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #20
Source File: dataset.py From pytorch-YOLO-v1 with MIT License | 5 votes |
def HSV2BGR(self,img): return cv2.cvtColor(img,cv2.COLOR_HSV2BGR)
Example #21
Source File: img_util.py From CvStudio with MIT License | 5 votes |
def adjust_brightness(src: np.ndarray, brightness_factor): hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) hsv[..., 2] = hsv[..., 2] * brightness_factor dst = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) dst = np.clip(dst, 0, 255) return dst # contrast : 1,100, brightness = 50, 100
Example #22
Source File: genplate.py From deep_learning with MIT License | 5 votes |
def tfactor(img): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) hsv[:, :, 0] = hsv[:, :, 0] * (0.8 + np.random.random() * 0.2); hsv[:, :, 1] = hsv[:, :, 1] * (0.3 + np.random.random() * 0.7); hsv[:, :, 2] = hsv[:, :, 2] * (0.2 + np.random.random() * 0.8); img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return img
Example #23
Source File: augmentations.py From D4LCN with MIT License | 5 votes |
def __call__(self, image, imobj=None): # BGR --> HSV if self.current == 'BGR' and self.transform == 'HSV': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # HSV --> BGR elif self.current == 'HSV' and self.transform == 'BGR': image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) else: raise NotImplementedError return image, imobj
Example #24
Source File: augmentations.py From nn_tools with MIT License | 5 votes |
def __call__(self, image, *args): if self.current == 'BGR' and self.transform == 'HSV': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) elif self.current == 'HSV' and self.transform == 'BGR': image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) else: raise NotImplementedError if len(args): return (image, *args) else: return image
Example #25
Source File: transforms.py From kaggle_carvana_segmentation with MIT License | 5 votes |
def __call__(self, image): if random.random() < self.prob: image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(image) hue_shift = np.random.uniform(self.hue_shift_limit[0], self.hue_shift_limit[1]) h = cv2.add(h, hue_shift) sat_shift = np.random.uniform(self.sat_shift_limit[0], self.sat_shift_limit[1]) s = cv2.add(s, sat_shift) val_shift = np.random.uniform(self.val_shift_limit[0], self.val_shift_limit[1]) v = cv2.add(v, val_shift) image = cv2.merge((h, s, v)) image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) return image
Example #26
Source File: augmentations.py From RefineDet.PyTorch with MIT License | 5 votes |
def __call__(self, image, boxes=None, labels=None): if self.current == 'BGR' and self.transform == 'HSV': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) elif self.current == 'HSV' and self.transform == 'BGR': image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) else: raise NotImplementedError return image, boxes, labels
Example #27
Source File: augmentations.py From refinedet.pytorch with MIT License | 5 votes |
def __call__(self, image, boxes=None, labels=None): if self.current == 'BGR' and self.transform == 'HSV': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) elif self.current == 'HSV' and self.transform == 'BGR': image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) else: raise NotImplementedError return image, boxes, labels
Example #28
Source File: augmentations.py From PytorchToCaffe with MIT License | 5 votes |
def __call__(self, image, *args): if self.current == 'BGR' and self.transform == 'HSV': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) elif self.current == 'HSV' and self.transform == 'BGR': image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) else: raise NotImplementedError if len(args): return (image, *args) else: return image
Example #29
Source File: augmentations.py From PytorchToCaffe with MIT License | 5 votes |
def __call__(self, image, *args): BGR_img = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) if len(args): return (BGR_img, *args) else: return BGR_img
Example #30
Source File: adaptive_hist_equalization.py From image_utility with MIT License | 5 votes |
def main(): # create a CLAHE object (Arguments are optional). clahe = cv.createCLAHE(clipLimit=2, tileGridSize=(4, 4)) # Read in images. target_dir = '/home/robin/Desktop/libvideo_processing/demo/image' file_list = ListGenerator().generate_list(target_dir, ['jpg']) for each_img in file_list: img = cv.imread(each_img) img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) v_a = clahe.apply(img_hsv[:, :, 2]) v_e = cv.equalizeHist(img_hsv[:, :, 2]) img_hsv_a = img_hsv.copy() img_hsv_e = img_hsv.copy() img_hsv_a[:,:,2] = v_a img_hsv_e[:,:,2] = v_e img_adpequalhist = cv.cvtColor(img_hsv_a, cv.COLOR_HSV2BGR) img_ehist = cv.cvtColor(img_hsv_e, cv.COLOR_HSV2BGR) res = np.hstack((img, img_ehist, img_adpequalhist)) # stacking images side-by-side # Show result. cv.imshow("preview", res) cv.waitKey() # Read in a video file. cap = cv.VideoCapture( '/home/robin/Desktop/libvideo_processing/demo/video.mp4') while True: _, img = cap.read() img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) img = cv.resize(img, (0, 0), img, 0.5, 0.5) cl1 = clahe.apply(img) res = np.hstack((img, cl1)) # stacking images side-by-side # Show result. cv.imshow("preview", res) cv.waitKey(30)