本文整理汇总了Python中cv2.LUT属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.LUT属性的具体用法?Python cv2.LUT怎么用?Python cv2.LUT使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.LUT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: adjust_brightness
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def adjust_brightness(img, brightness_factor):
"""Adjust brightness of an Image.
Args:
img (numpy ndarray): numpy ndarray to be adjusted.
brightness_factor (float): How much to adjust the brightness. Can be
any non negative number. 0 gives a black image, 1 gives the
original image while 2 increases the brightness by a factor of 2.
Returns:
numpy ndarray: Brightness adjusted image.
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
table = np.array([ i*brightness_factor for i in range (0,256)]).clip(0,255).astype('uint8')
# same thing but a bit slower
# cv2.convertScaleAbs(img, alpha=brightness_factor, beta=0)
if img.shape[2]==1:
return cv2.LUT(img, table)[:,:,np.newaxis]
else:
return cv2.LUT(img, table)
示例2: adjust_contrast
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def adjust_contrast(img, contrast_factor):
"""Adjust contrast of an mage.
Args:
img (numpy ndarray): numpy ndarray to be adjusted.
contrast_factor (float): How much to adjust the contrast. Can be any
non negative number. 0 gives a solid gray image, 1 gives the
original image while 2 increases the contrast by a factor of 2.
Returns:
numpy ndarray: Contrast adjusted image.
"""
# much faster to use the LUT construction than anything else I've tried
# it's because you have to change dtypes multiple times
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
table = np.array([ (i-74)*contrast_factor+74 for i in range (0,256)]).clip(0,255).astype('uint8')
# enhancer = ImageEnhance.Contrast(img)
# img = enhancer.enhance(contrast_factor)
if img.shape[2]==1:
return cv2.LUT(img, table)[:,:,np.newaxis]
else:
return cv2.LUT(img,table)
示例3: adjust_brightness
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def adjust_brightness(img, brightness_factor):
"""Adjust brightness of an Image.
Args:
img (numpy ndarray): numpy ndarray to be adjusted.
brightness_factor (float): How much to adjust the brightness. Can be
any non negative number. 0 gives a black image, 1 gives the
original image while 2 increases the brightness by a factor of 2.
Returns:
numpy ndarray: Brightness adjusted image.
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
table = np.array([ i*brightness_factor for i in range (0,256)]).clip(0,255).astype('uint8')
# same thing but a bit slower
# cv2.convertScaleAbs(img, alpha=brightness_factor, beta=0)
if img.shape[2] == 1:
return cv2.LUT(img, table)[:,:,np.newaxis]
else:
return cv2.LUT(img, table)
示例4: adjust_contrast
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def adjust_contrast(img, contrast_factor):
"""Adjust contrast of an mage.
Args:
img (numpy ndarray): numpy ndarray to be adjusted.
contrast_factor (float): How much to adjust the contrast. Can be any
non negative number. 0 gives a solid gray image, 1 gives the
original image while 2 increases the contrast by a factor of 2.
Returns:
numpy ndarray: Contrast adjusted image.
"""
# much faster to use the LUT construction than anything else I've tried
# it's because you have to change dtypes multiple times
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
table = np.array([ (i-74)*contrast_factor+74 for i in range (0,256)]).clip(0,255).astype('uint8')
# enhancer = ImageEnhance.Contrast(img)
# img = enhancer.enhance(contrast_factor)
if img.shape[2] == 1:
return cv2.LUT(img, table)[:,:,np.newaxis]
else:
return cv2.LUT(img, table)
示例5: gamma_correction
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def gamma_correction(img,bits):
"""
Image gamma correction with random gamma
:param img: input image
:param bits: number of bits to represent a single color value
:returns: corrected image
"""
MAX = get_max(bits)
gamma = np.random.randint(1,20)/10.
invGamma = 1.0 / gamma
table = np.array([((i / float(MAX)) ** invGamma) * MAX
for i in np.arange(0, (MAX+1))])
return cv2.LUT(img, table)
示例6: gamma_trans
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def gamma_trans(self, img, gamma):
"""Face gamma correction.
Parameters
----------
img: mat
The Mat data format of reading from the original image using opencv.
gamma: float
The gamma value for gamma correction.
Returns
-------
type: mat
Face BGR image after gamma correction.
"""
gamma_table = [np.power(x / 255.0, gamma)*255.0 for x in range(256)]
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
return cv2.LUT(img, gamma_table)
示例7: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def __init__(self, value=None, value_hue=None, value_saturation=None,
per_channel=False, from_colorspace="RGB",
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(AddToHueAndSaturation, self).__init__(
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)
if value is None and value_hue is None and value_saturation is None:
value_hue = (-40, 40)
value_saturation = (-40, 40)
self.value = self._handle_value_arg(value, value_hue, value_saturation)
self.value_hue = self._handle_value_hue_arg(value_hue)
self.value_saturation = self._handle_value_saturation_arg(
value_saturation)
self.per_channel = iap.handle_probability_param(per_channel,
"per_channel")
self.from_colorspace = from_colorspace
self.backend = "cv2"
# precompute tables for cv2.LUT
if self.backend == "cv2" and AddToHueAndSaturation._LUT_CACHE is None:
AddToHueAndSaturation._LUT_CACHE = self._generate_lut_table()
示例8: apply_custom_colormap
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def apply_custom_colormap(source_image, cmap):
assert source_image.dtype == np.uint8, 'must be np.uint8 image'
# Squeeze image to turn it to black and white
if source_image.ndim == 3:
source_image = source_image.squeeze(-1)
# Initialize the matplotlib color map
sm = plt.cm.ScalarMappable(cmap=cmap)
# Obtain linear color range
color_range = sm.to_rgba(np.linspace(0, 1, 256))[:, 0:3] # color range RGBA => RGB
color_range = (color_range*255.0).astype(np.uint8) # [0,1] => [0,255]
color_range = np.squeeze(np.dstack(
[color_range[:, 2], color_range[:, 1], color_range[:, 0]]), 0) # RGB => BGR
# Apply colormap for each channel individually
channels = [cv2.LUT(source_image, color_range[:, i]) for i in range(3)]
return np.dstack(channels)
示例9: remap_color
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def remap_color(img, bg, center, max):
def get_lut(img, bg, center, max):
ma = np.max(img)
# me = np.mean(img)
# th = np.mean([ma, me]) * 1.5
th = ma / 2
gap = 10
channels = [[], [], []]
range2 = ma - int(th)
for i in range(3):
channels[i].append(np.linspace(bg[i] - gap, center[i] - gap, int(th)).astype(np.uint8))
channels[i].append(np.linspace(center[i] - gap, max[i] + gap, range2).astype(np.uint8))
channels[i].append([max[i] + gap] * (256 - sum(map(len, channels[i]))))
channels[i] = np.hstack(channels[i])
return np.dstack(channels)
# img = adjust_gamma(img, 5.)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
if np.mean(gray) > 100:
return img
lut = get_lut(img, bg, center, max)
res = cv2.LUT(img, lut).astype(np.uint8)
return res
示例10: get_foreground_background_probs
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def get_foreground_background_probs(frame,obj_rect,num_bins,bin_mapping=None):
frame=frame.astype(np.uint8)
surr_hist = cv2.calcHist([frame],[0, 1, 2], None, [num_bins,num_bins,num_bins],
[0, 256, 0, 256, 0, 256])
x,y,w,h=obj_rect
if x+w>frame.shape[1]-1:
w=(frame.shape[1]-1)-x
if y+h>frame.shape[0]-1:
h=(frame.shape[0]-1)-y
x=int(max(x,0))
y=int(max(y,0))
obj_win=frame[y:y+h+1,x:x+w+1]
obj_hist = cv2.calcHist([obj_win], [0, 1, 2], None, [num_bins, num_bins, num_bins], [0, 256, 0, 256, 0, 256])
prob_lut = (obj_hist + 1) / (surr_hist + 2)
prob_map = None
if bin_mapping is not None:
frame_bin = cv2.LUT(frame, bin_mapping).astype(np.int64)
prob_map = prob_lut[frame_bin[:, :, 0], frame_bin[:, :, 1], frame_bin[:, :, 2]]
return prob_lut,prob_map
示例11: _shift_hsv_uint8
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def _shift_hsv_uint8(img, hue_shift, sat_shift, val_shift):
dtype = img.dtype
img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
hue, sat, val = cv2.split(img)
lut_hue = np.arange(0, 256, dtype=np.int16)
lut_hue = np.mod(lut_hue + hue_shift, 180).astype(dtype)
lut_sat = np.arange(0, 256, dtype=np.int16)
lut_sat = np.clip(lut_sat + sat_shift, 0, 255).astype(dtype)
lut_val = np.arange(0, 256, dtype=np.int16)
lut_val = np.clip(lut_val + val_shift, 0, 255).astype(dtype)
hue = cv2.LUT(hue, lut_hue)
sat = cv2.LUT(sat, lut_sat)
val = cv2.LUT(val, lut_val)
img = cv2.merge((hue, sat, val)).astype(dtype)
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
return img
示例12: _equalize_pil
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def _equalize_pil(img, mask=None):
histogram = cv2.calcHist([img], [0], mask, [256], (0, 256)).ravel()
h = [_f for _f in histogram if _f]
if len(h) <= 1:
return img.copy()
step = np.sum(h[:-1]) // 255
if not step:
return img.copy()
lut = np.empty(256, dtype=np.uint8)
n = step // 2
for i in range(256):
lut[i] = min(n // step, 255)
n += histogram[i]
return cv2.LUT(img, np.array(lut))
示例13: _brightness_contrast_adjust_uint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def _brightness_contrast_adjust_uint(img, alpha=1, beta=0, beta_by_max=False):
dtype = np.dtype("uint8")
max_value = MAX_VALUES_BY_DTYPE[dtype]
lut = np.arange(0, max_value + 1).astype("float32")
if alpha != 1:
lut *= alpha
if beta != 0:
if beta_by_max:
lut += beta * max_value
else:
lut += beta * np.mean(img)
lut = np.clip(lut, 0, max_value).astype(dtype)
img = cv2.LUT(img, lut)
return img
示例14: _multiply_uint8_optimized
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def _multiply_uint8_optimized(img, multiplier):
if is_grayscale_image(img) or len(multiplier) == 1:
multiplier = multiplier[0]
lut = np.arange(0, 256, dtype=np.float32)
lut *= multiplier
lut = clip(lut, np.uint8, MAX_VALUES_BY_DTYPE[img.dtype])
func = _maybe_process_in_chunks(cv2.LUT, lut=lut)
return func(img)
channels = img.shape[-1]
lut = [np.arange(0, 256, dtype=np.float32)] * channels
lut = np.stack(lut, axis=-1)
lut *= multiplier
lut = clip(lut, np.uint8, MAX_VALUES_BY_DTYPE[img.dtype])
images = []
for i in range(channels):
func = _maybe_process_in_chunks(cv2.LUT, lut=lut[:, i])
images.append(func(img[:, :, i]))
return np.stack(images, axis=-1)
示例15: _augment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import LUT [as 别名]
def _augment(self, img, gamma):
old_dtype = img.dtype
lut = ((np.arange(256, dtype='float32') / 255) ** (1. / (1. + gamma)) * 255).astype('uint8')
img = np.clip(img, 0, 255).astype('uint8')
ret = cv2.LUT(img, lut).astype(old_dtype)
if img.ndim == 3 and ret.ndim == 2:
ret = ret[:, :, np.newaxis]
return ret