本文整理匯總了Python中cv2.COLOR_HSV2BGR屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.COLOR_HSV2BGR屬性的具體用法?Python cv2.COLOR_HSV2BGR怎麽用?Python cv2.COLOR_HSV2BGR使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.COLOR_HSV2BGR屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: augment_hsv
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例2: proc_oflow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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)
示例3: compute_dense_optical_flow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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)
示例4: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例5: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例6: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例7: face_whiten
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例8: augment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例9: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例10: flow2RGB
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例11: random_hue_saturation_value
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例12: randomHueSaturationValue
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例13: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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
示例14: _augment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
def _augment(self, img, hue):
m = cv2.COLOR_BGR2HSV if not self.rgb else cv2.COLOR_RGB2HSV
hsv = cv2.cvtColor(img, m)
# https://docs.opencv.org/3.2.0/de/d25/imgproc_color_conversions.html#color_convert_rgb_hsv
if hsv.dtype.itemsize == 1:
# OpenCV uses 0-179 for 8-bit images
hsv[..., 0] = (hsv[..., 0] + hue) % 180
else:
# OpenCV uses 0-360 for floating point images
hsv[..., 0] = (hsv[..., 0] + 2 * hue) % 360
m = cv2.COLOR_HSV2BGR if not self.rgb else cv2.COLOR_HSV2RGB
img = cv2.cvtColor(hsv, m)
return img
示例15: _distort
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HSV2BGR [as 別名]
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