本文整理汇总了Python中cv2.COLOR_RGB2GRAY属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.COLOR_RGB2GRAY属性的具体用法?Python cv2.COLOR_RGB2GRAY怎么用?Python cv2.COLOR_RGB2GRAY使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.COLOR_RGB2GRAY属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: adjust_contrast
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def adjust_contrast(img, contrast_factor):
"""Adjust contrast of an Image.
Args:
img (CV Image): CV Image 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:
CV Image: Contrast adjusted image.
"""
if not _is_numpy_image(img):
raise TypeError('img should be CV Image. Got {}'.format(type(img)))
im = img.astype(np.float32)
mean = round(cv2.cvtColor(im, cv2.COLOR_RGB2GRAY).mean())
im = (1 - contrast_factor) * mean + contrast_factor * im
im = im.clip(min=0, max=255)
return im.astype(img.dtype)
示例2: adjust_saturation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def adjust_saturation(img, saturation_factor):
"""Adjust color saturation of an image.
Args:
img (CV Image): CV Image to be adjusted.
saturation_factor (float): How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.
Returns:
CV Image: Saturation adjusted image.
"""
if not _is_numpy_image(img):
raise TypeError('img should be CV Image. Got {}'.format(type(img)))
im = img.astype(np.float32)
degenerate = cv2.cvtColor(
cv2.cvtColor(
im,
cv2.COLOR_RGB2GRAY),
cv2.COLOR_GRAY2RGB)
im = (1 - saturation_factor) * degenerate + saturation_factor * im
im = im.clip(min=0, max=255)
return im.astype(img.dtype)
示例3: _augment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def _augment(self, img, r):
old_dtype = img.dtype
if img.ndim == 3:
if self.rgb is not None:
m = cv2.COLOR_RGB2GRAY if self.rgb else cv2.COLOR_BGR2GRAY
grey = cv2.cvtColor(img.astype('float32'), m)
mean = np.mean(grey)
else:
mean = np.mean(img, axis=(0, 1), keepdims=True)
else:
mean = np.mean(img)
img = img * r + mean * (1 - r)
if self.clip or old_dtype == np.uint8:
img = np.clip(img, 0, 255)
return img.astype(old_dtype)
示例4: to_grayscale
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def to_grayscale(img, num_output_channels=1):
"""Convert image to grayscale version of image.
Args:
img (numpy ndarray): Image to be converted to grayscale.
Returns:
numpy ndarray: Grayscale version of the image.
if num_output_channels = 1 : returned image is single channel
if num_output_channels = 3 : returned image is 3 channel with r = g = b
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))
if num_output_channels==1:
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
elif num_output_channels==3:
# much faster than doing cvtColor to go back to gray
img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape)
return img
示例5: __call__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def __call__(self, image, labels=None):
if self.current == 'RGB' and self.to == 'HSV':
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
elif self.current == 'RGB' and self.to == 'GRAY':
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
if self.keep_3ch:
image = np.stack([image] * 3, axis=-1)
elif self.current == 'HSV' and self.to == 'RGB':
image = cv2.cvtColor(image, cv2.COLOR_HSV2RGB)
elif self.current == 'HSV' and self.to == 'GRAY':
image = cv2.cvtColor(image, cv2.COLOR_HSV2GRAY)
if self.keep_3ch:
image = np.stack([image] * 3, axis=-1)
if labels is None:
return image
else:
return image, labels
开发者ID:pierluigiferrari,项目名称:data_generator_object_detection_2d,代码行数:19,代码来源:object_detection_2d_photometric_ops.py
示例6: to_grayscale
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def to_grayscale(img, num_output_channels=1):
"""Convert image to grayscale version of image.
Args:
img (numpy ndarray): Image to be converted to grayscale.
Returns:
numpy ndarray: Grayscale version of the image.
if num_output_channels = 1 : returned image is single channel
if num_output_channels = 3 : returned image is 3 channel with r = g = b
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))
if num_output_channels==1:
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
elif num_output_channels==3:
# much faster than doing cvtColor to go back to gray
img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape)
return img
示例7: keepratio_resize
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def keepratio_resize(self, img):
cur_ratio = img.size[0] / float(img.size[1])
mask_height = self.img_height
mask_width = self.img_width
img = np.array(img)
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
if cur_ratio > self.target_ratio:
cur_target_height = self.img_height
cur_target_width = self.img_width
else:
cur_target_height = self.img_height
cur_target_width = int(self.img_height * cur_ratio)
img = cv2.resize(img, (cur_target_width, cur_target_height))
start_x = int((mask_height - img.shape[0])/2)
start_y = int((mask_width - img.shape[1])/2)
mask = np.zeros([mask_height, mask_width]).astype(np.uint8)
mask[start_x : start_x + img.shape[0], start_y : start_y + img.shape[1]] = img
img = mask
return img
示例8: observation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def observation(self, obs):
if self._key is None:
frame = obs
else:
frame = obs[self._key]
if self._grayscale:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
frame = cv2.resize(
frame, (self._width, self._height), interpolation=cv2.INTER_AREA
)
if self._grayscale:
frame = np.expand_dims(frame, -1)
if self._key is None:
obs = frame
else:
obs = obs.copy()
obs[self._key] = frame
return obs
示例9: computeFeatures
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def computeFeatures(self, video):
"""
todo: improve documentation
Computes SIFT features for a single video.
:param video: a video of shape (n_frames, width, height, channel)
:return: the features, shape ()
"""
descriptor_array = []
for i in range(video.shape[0]):
frame = cv2.cvtColor(video[i], cv2.COLOR_RGB2GRAY).astype('uint8')
_, descriptors = cv2.xfeatures2d.SIFT_create(nfeatures=self.n_descriptors).detectAndCompute(frame, None)
if descriptors is not None:
if descriptors.shape[0] < self.n_descriptors:
descriptors = np.concatenate([descriptors, np.zeros((self.n_descriptors - descriptors.shape[0], 128))], axis=0)
else:
descriptors = descriptors[:self.n_descriptors]
else:
descriptors = np.zeros((self.n_descriptors, 128))
assert descriptors.shape == (self.n_descriptors, 128)
descriptor_array.append(descriptors)
features = np.concatenate(descriptor_array, axis=0)
return features
示例10: sobelOperT
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def sobelOperT(self, img, blursize, morphW, morphH):
'''
No different with sobelOper ?
'''
blur = cv2.GaussianBlur(img, (blursize, blursize), 0, 0, cv2.BORDER_DEFAULT)
if len(blur.shape) == 3:
gray = cv2.cvtColor(blur, cv2.COLOR_RGB2GRAY)
else:
gray = blur
x = cv2.Sobel(gray, cv2.CV_16S, 1, 0, 3)
absX = cv2.convertScaleAbs(x)
grad = cv2.addWeighted(absX, 1, 0, 0, 0)
_, threshold = cv2.threshold(grad, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (morphW, morphH))
threshold = cv2.morphologyEx(threshold, cv2.MORPH_CLOSE, element)
return threshold
示例11: __apply_canny
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def __apply_canny(self, src, ksize=7, sigma=1.2, low_th=10, high_th=70):
"""Apply canny edge detection.
Args:
src (int): Input image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
Returns:
dst (int): Output image.
numpy.ndarray, (720, 1280), 0~1
"""
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
blur_gray = cv2.GaussianBlur(gray,(ksize, ksize), sigma)
dst = cv2.Canny(blur_gray, low_th, high_th) // 255
return dst
示例12: observation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def observation(self, frame):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
frame = cv2.resize(frame, (self.width, self.height), interpolation=cv2.INTER_AREA)
return frame[:, :, None]
示例13: to_grayscale
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def to_grayscale(img, num_output_channels=1):
"""Convert image to grayscale version of image.
Args:
img (CV Image): CV to be converted to grayscale.
Returns:
CV Image: Grayscale version of the image.
if num_output_channels = 1 : returned image is single channel
if num_output_channels = 3 : returned image is 3 channel with r = g = b
"""
if not _is_numpy_image(img):
raise TypeError('img should be CV Image. Got {}'.format(type(img)))
if num_output_channels == 1:
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
elif num_output_channels == 3:
img = cv2.cvtColor(
cv2.cvtColor(
img,
cv2.COLOR_RGB2GRAY),
cv2.COLOR_GRAY2RGB)
else:
raise ValueError('num_output_channels should be either 1 or 3')
return img
示例14: _observation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def _observation(self, frame):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
frame = cv2.resize(frame, (self.width, self.height),
interpolation=cv2.INTER_AREA)
return frame[:, :, None].transpose(2, 0, 1)
示例15: image_search
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import COLOR_RGB2GRAY [as 别名]
def image_search(x_start :int, y_start :int, x_end :int, y_end :int,
img :str, threshold :int, bmp :image =None) -> Optional[Tuple[int, int]]:
"""Search the screen for the supplied picture.
Returns a tuple with x,y-coordinates, or None if result is below
the threshold.
Keyword arguments:
image -- Filename or path to file that you search for.
threshold -- The level of fuzziness to use - a perfect match will be
close to 1, but probably never 1. In my testing use a
value between 0.7-0.95 depending on how strict you wish
to be.
bmp -- a bitmap from the get_bitmap() function, use this if you're
performing multiple different OCR-readings in succession
from the same page. This is to avoid to needlessly get the
same bitmap multiple times. If a bitmap is not passed, the
function will get the bitmap itself. (default None)
"""
if not bmp: bmp = Inputs.get_bitmap()
# Bitmaps are created with a 8px border
search_area = bmp.crop((x_start + 8, y_start + 8,
x_end + 8, y_end + 8))
search_area = numpy.asarray(search_area)
search_area = cv2.cvtColor(search_area, cv2.COLOR_RGB2GRAY)
template = cv2.imread(img, 0)
res = cv2.matchTemplate(search_area, template, cv2.TM_CCOEFF_NORMED)
_, max_val, _, max_loc = cv2.minMaxLoc(res)
if max_val < threshold:
return None
return max_loc