本文整理匯總了Python中cv2.COLOR_HLS2RGB屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.COLOR_HLS2RGB屬性的具體用法?Python cv2.COLOR_HLS2RGB怎麽用?Python cv2.COLOR_HLS2RGB使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.COLOR_HLS2RGB屬性的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def __call__(self, data):
h, w, c = data.shape
assert c%3 == 0, "input channel = %d, illegal"%c
random_vars = [int(round(self.rng.uniform(-x, x))) for x in self.vars]
base = len(random_vars)
augmented_data = np.zeros(data.shape, )
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(data[:,:,3*i_im:(3*i_im+3)], cv2.COLOR_RGB2HLS)
hls_limits = [180, 255, 255]
for ic in range(0, c):
var = random_vars[ic%base]
limit = hls_limits[ic%base]
augmented_data[:,:,ic] = np.minimum(np.maximum(augmented_data[:,:,ic] + var, 0), limit)
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(augmented_data[:,:,3*i_im:(3*i_im+3)].astype(np.uint8), \
cv2.COLOR_HLS2RGB)
return augmented_data
示例2: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def __call__(self, data, idx=None, copy_id=0):
h, w, c = data.shape
assert c%3 == 0, "input channel = %d, illegal"%c
random_vars = [int(self.rng.uniform(-x, x)) for x in self.vars]
base = len(random_vars)
augmented_data = np.zeros(data.shape, )
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(data[:,:,3*i_im:(3*i_im+3)], cv2.COLOR_RGB2HLS)
hls_limits = [180, 255, 255]
for ic in range(0, c):
var = random_vars[ic%base]
limit = hls_limits[ic%base]
augmented_data[:,:,ic] = np.minimum(np.maximum(augmented_data[:,:,ic] + var, 0), limit)
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(augmented_data[:,:,3*i_im:(3*i_im+3)].astype(np.uint8), \
cv2.COLOR_HLS2RGB)
return augmented_data
示例3: __call__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def __call__(self, data):
h, w, c = data.shape
assert c%3 == 0, "input channel = %d, illegal"%c
random_vars = [int(round(self.rng.uniform(-x, x))) for x in self.vars]
base = len(random_vars)
augmented_data = np.zeros(data.shape, )
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(data[:,:,3*i_im:(3*i_im+3)], cv2.COLOR_RGB2HLS)
hls_limits = [180, 255, 255]
for ic in range(0, c):
var = random_vars[ic%base]
limit = hls_limits[ic%base]
augmented_data[:,:,ic] = np.minimum(np.maximum(augmented_data[:,:,ic] + var, 0), limit)
for i_im in range(0, int(c/3)):
augmented_data[:,:,3*i_im:(3*i_im+3)] = \
cv2.cvtColor(augmented_data[:,:,3*i_im:(3*i_im+3)].astype(np.uint8), \
cv2.COLOR_HLS2RGB)
return augmented_data
示例4: test_basic_functionality
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def test_basic_functionality(self):
# basic functionality test
aug = iaa.FastSnowyLandscape(
lightness_threshold=100,
lightness_multiplier=2.0)
image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8)
image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
mask = (image_hls[..., 1] < 100)
expected = np.copy(image_hls).astype(np.float32)
expected[..., 1][mask] *= 2.0
expected = np.clip(np.round(expected), 0, 255).astype(np.uint8)
expected = cv2.cvtColor(expected, cv2.COLOR_HLS2RGB)
observed = aug.augment_image(image)
assert np.array_equal(observed, expected)
示例5: test_vary_lightness_threshold
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def test_vary_lightness_threshold(self):
# test when varying lightness_threshold between images
image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8)
image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
aug = iaa.FastSnowyLandscape(
lightness_threshold=_TwoValueParam(75, 125),
lightness_multiplier=2.0)
mask = (image_hls[..., 1] < 75)
expected1 = np.copy(image_hls).astype(np.float64)
expected1[..., 1][mask] *= 2.0
expected1 = np.clip(np.round(expected1), 0, 255).astype(np.uint8)
expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB)
mask = (image_hls[..., 1] < 125)
expected2 = np.copy(image_hls).astype(np.float64)
expected2[..., 1][mask] *= 2.0
expected2 = np.clip(np.round(expected2), 0, 255).astype(np.uint8)
expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB)
observed = aug.augment_images([image] * 4)
assert np.array_equal(observed[0], expected1)
assert np.array_equal(observed[1], expected2)
assert np.array_equal(observed[2], expected1)
assert np.array_equal(observed[3], expected2)
示例6: test_vary_lightness_multiplier
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def test_vary_lightness_multiplier(self):
# test when varying lightness_multiplier between images
image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8)
image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
aug = iaa.FastSnowyLandscape(
lightness_threshold=100,
lightness_multiplier=_TwoValueParam(1.5, 2.0))
mask = (image_hls[..., 1] < 100)
expected1 = np.copy(image_hls).astype(np.float64)
expected1[..., 1][mask] *= 1.5
expected1 = np.clip(np.round(expected1), 0, 255).astype(np.uint8)
expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB)
mask = (image_hls[..., 1] < 100)
expected2 = np.copy(image_hls).astype(np.float64)
expected2[..., 1][mask] *= 2.0
expected2 = np.clip(np.round(expected2), 0, 255).astype(np.uint8)
expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB)
observed = aug.augment_images([image] * 4)
assert np.array_equal(observed[0], expected1)
assert np.array_equal(observed[1], expected2)
assert np.array_equal(observed[2], expected1)
assert np.array_equal(observed[3], expected2)
示例7: random_shadow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def random_shadow(image):
"""
Generates and adds random shadow
"""
# (x1, y1) and (x2, y2) forms a line
# xm, ym gives all the locations of the image
x1, y1 = IMAGE_WIDTH * np.random.rand(), 0
x2, y2 = IMAGE_WIDTH * np.random.rand(), IMAGE_HEIGHT
xm, ym = np.mgrid[0:IMAGE_HEIGHT, 0:IMAGE_WIDTH]
# mathematically speaking, we want to set 1 below the line and zero otherwise
# Our coordinate is up side down. So, the above the line:
# (ym-y1)/(xm-x1) > (y2-y1)/(x2-x1)
# as x2 == x1 causes zero-division problem, we'll write it in the below form:
# (ym-y1)*(x2-x1) - (y2-y1)*(xm-x1) > 0
mask = np.zeros_like(image[:, :, 1])
mask[(ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1) > 0] = 1
# choose which side should have shadow and adjust saturation
cond = mask == np.random.randint(2)
s_ratio = np.random.uniform(low=0.2, high=0.5)
# adjust Saturation in HLS(Hue, Light, Saturation)
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
hls[:, :, 1][cond] = hls[:, :, 1][cond] * s_ratio
return cv2.cvtColor(hls, cv2.COLOR_HLS2RGB)
示例8: random_color_warp
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def random_color_warp(image, d_h=None, d_s=None, d_l=None):
""" Given an RGB image [H x W x 3], add random hue, saturation and luminosity to the image
Code adapted from: https://github.com/yuxng/PoseCNN/blob/master/lib/utils/blob.py
"""
H, W, _ = image.shape
image_color_warped = np.zeros_like(image)
# Set random hue, luminosity and saturation which ranges from -0.1 to 0.1
if d_h is None:
d_h = (random.random() - 0.5) * 0.2 * 256
if d_l is None:
d_l = (random.random() - 0.5) * 0.2 * 256
if d_s is None:
d_s = (random.random() - 0.5) * 0.2 * 256
# Convert the RGB to HLS
hls = cv2.cvtColor(image.round().astype(np.uint8), cv2.COLOR_RGB2HLS)
h, l, s = cv2.split(hls)
# Add the values to the image H, L, S
new_h = (np.round((h + d_h)) % 256).astype(np.uint8)
new_l = np.round(np.clip(l + d_l, 0, 255)).astype(np.uint8)
new_s = np.round(np.clip(s + d_s, 0, 255)).astype(np.uint8)
# Convert the HLS to RGB
new_hls = cv2.merge((new_h, new_l, new_s)).astype(np.uint8)
new_im = cv2.cvtColor(new_hls, cv2.COLOR_HLS2RGB)
image_color_warped = new_im.astype(np.float32)
return image_color_warped
示例9: random_shadow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def random_shadow(image):
"""
Generates and adds random shadow
"""
# (x1, y1) and (x2, y2) forms a line
# xm, ym gives all the locations of the image
x1, y1 = IMAGE_WIDTH * np.random.rand(), 0
x2, y2 = IMAGE_WIDTH * np.random.rand(), IMAGE_HEIGHT
xm, ym = np.mgrid[0:IMAGE_HEIGHT, 0:IMAGE_WIDTH]
# mathematically speaking, we want to set 1 below the line and zero otherwise
# Our coordinate is up side down. So, the above the line:
# (ym-y1)/(xm-x1) > (y2-y1)/(x2-x1)
# as x2 == x1 causes zero-division problem, we'll write it in the below form:
# (ym-y1)*(x2-x1) - (y2-y1)*(xm-x1) > 0
mask = np.zeros_like(image[:, :, 1])
mask[(ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1) > 0] = 1
# choose which side should have shadow and adjust saturation
cond = mask == np.random.randint(2)
s_ratio = np.random.uniform(low=0.2, high=0.5)
# adjust Saturation in HLS(Hue, Light, Saturation)
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
hls[:, :, 1][cond] = hls[:, :, 1][cond] * s_ratio
return cv2.cvtColor(hls, cv2.COLOR_HLS2RGB)
示例10: add_shadow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def add_shadow(img, vertices_list):
"""Add shadows to the image.
From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library
Args:
img (numpy.ndarray):
vertices_list (list):
Returns:
numpy.ndarray:
"""
non_rgb_warning(img)
input_dtype = img.dtype
needs_float = False
if input_dtype == np.float32:
img = from_float(img, dtype=np.dtype("uint8"))
needs_float = True
elif input_dtype not in (np.uint8, np.float32):
raise ValueError("Unexpected dtype {} for RandomSnow augmentation".format(input_dtype))
image_hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
mask = np.zeros_like(img)
# adding all shadow polygons on empty mask, single 255 denotes only red channel
for vertices in vertices_list:
cv2.fillPoly(mask, vertices, 255)
# if red channel is hot, image's "Lightness" channel's brightness is lowered
red_max_value_ind = mask[:, :, 0] == 255
image_hls[:, :, 1][red_max_value_ind] = image_hls[:, :, 1][red_max_value_ind] * 0.5
image_rgb = cv2.cvtColor(image_hls, cv2.COLOR_HLS2RGB)
if needs_float:
image_rgb = to_float(image_rgb, max_value=255)
return image_rgb
示例11: add_snow
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def add_snow(img, snow_point, brightness_coeff):
"""Bleaches out pixels, imitation snow.
From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library
Args:
img (numpy.ndarray): Image.
snow_point: Number of show points.
brightness_coeff: Brightness coefficient.
Returns:
numpy.ndarray: Image.
"""
non_rgb_warning(img)
input_dtype = img.dtype
needs_float = False
snow_point *= 127.5 # = 255 / 2
snow_point += 85 # = 255 / 3
if input_dtype == np.float32:
img = from_float(img, dtype=np.dtype("uint8"))
needs_float = True
elif input_dtype not in (np.uint8, np.float32):
raise ValueError("Unexpected dtype {} for RandomSnow augmentation".format(input_dtype))
image_HLS = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
image_HLS = np.array(image_HLS, dtype=np.float32)
image_HLS[:, :, 1][image_HLS[:, :, 1] < snow_point] *= brightness_coeff
image_HLS[:, :, 1] = clip(image_HLS[:, :, 1], np.uint8, 255)
image_HLS = np.array(image_HLS, dtype=np.uint8)
image_RGB = cv2.cvtColor(image_HLS, cv2.COLOR_HLS2RGB)
if needs_float:
image_RGB = to_float(image_RGB, max_value=255)
return image_RGB
示例12: add_rain
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def add_rain(img, slant, drop_length, drop_width, drop_color, blur_value, brightness_coefficient, rain_drops):
"""
From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library
Args:
img (numpy.ndarray): Image.
slant (int):
drop_length:
drop_width:
drop_color:
blur_value (int): Rainy view are blurry.
brightness_coefficient (float): Rainy days are usually shady.
rain_drops:
Returns:
numpy.ndarray: Image.
"""
non_rgb_warning(img)
input_dtype = img.dtype
needs_float = False
if input_dtype == np.float32:
img = from_float(img, dtype=np.dtype("uint8"))
needs_float = True
elif input_dtype not in (np.uint8, np.float32):
raise ValueError("Unexpected dtype {} for RandomSnow augmentation".format(input_dtype))
image = img.copy()
for (rain_drop_x0, rain_drop_y0) in rain_drops:
rain_drop_x1 = rain_drop_x0 + slant
rain_drop_y1 = rain_drop_y0 + drop_length
cv2.line(image, (rain_drop_x0, rain_drop_y0), (rain_drop_x1, rain_drop_y1), drop_color, drop_width)
image = cv2.blur(image, (blur_value, blur_value)) # rainy view are blurry
image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS).astype(np.float32)
image_hls[:, :, 1] *= brightness_coefficient
image_rgb = cv2.cvtColor(image_hls.astype(np.uint8), cv2.COLOR_HLS2RGB)
if needs_float:
image_rgb = to_float(image_rgb, max_value=255)
return image_rgb
示例13: iso_noise
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import COLOR_HLS2RGB [as 別名]
def iso_noise(image, color_shift=0.05, intensity=0.5, random_state=None, **kwargs):
"""
Apply poisson noise to image to simulate camera sensor noise.
Args:
image (numpy.ndarray): Input image, currently, only RGB, uint8 images are supported.
color_shift (float):
intensity (float): Multiplication factor for noise values. Values of ~0.5 are produce noticeable,
yet acceptable level of noise.
random_state:
**kwargs:
Returns:
numpy.ndarray: Noised image
"""
if image.dtype != np.uint8:
raise TypeError("Image must have uint8 channel type")
if is_grayscale_image(image):
raise TypeError("Image must be RGB")
if random_state is None:
random_state = np.random.RandomState(42)
one_over_255 = float(1.0 / 255.0)
image = np.multiply(image, one_over_255, dtype=np.float32)
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
_, stddev = cv2.meanStdDev(hls)
luminance_noise = random_state.poisson(stddev[1] * intensity * 255, size=hls.shape[:2])
color_noise = random_state.normal(0, color_shift * 360 * intensity, size=hls.shape[:2])
hue = hls[..., 0]
hue += color_noise
hue[hue < 0] += 360
hue[hue > 360] -= 360
luminance = hls[..., 1]
luminance += (luminance_noise / 255) * (1.0 - luminance)
image = cv2.cvtColor(hls, cv2.COLOR_HLS2RGB) * 255
return image.astype(np.uint8)