本文整理汇总了Python中cv2.split方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.split方法的具体用法?Python cv2.split怎么用?Python cv2.split使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.split方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_squares
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def find_squares(img):
img = cv2.GaussianBlur(img, (5, 5), 0)
squares = []
for gray in cv2.split(img):
for thrs in xrange(0, 255, 26):
if thrs == 0:
bin = cv2.Canny(gray, 0, 50, apertureSize=5)
bin = cv2.dilate(bin, None)
else:
retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
cnt = cnt.reshape(-1, 2)
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
if max_cos < 0.1:
squares.append(cnt)
return squares
示例2: overlay_transparent_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def overlay_transparent_image(bg, fg, x1, y1):
# bg is 3 RGB
# fg is 4 RGBA
bg = bg.copy()
fg = fg.copy()
h, w = fg.shape[:2]
t = bg[y1:y1 + h, x1:x1 + w]
b, g, r, a = cv2.split(fg)
mask = cv2.merge((a, a, a))
fg = cv2.merge((b, g, r))
overlaid = alpha_blend(t, fg, mask)
bg[y1:y1 + h, x1:x1 + w] = overlaid
return bg
示例3: cal_rgb_confidence
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def cal_rgb_confidence(img_src_rgb, img_sch_rgb):
"""同大小彩图计算相似度."""
# BGR三通道心理学权重:
weight = (0.114, 0.587, 0.299)
src_bgr, sch_bgr = cv2.split(img_src_rgb), cv2.split(img_sch_rgb)
# 计算BGR三通道的confidence,存入bgr_confidence:
bgr_confidence = [0, 0, 0]
for i in range(3):
res_temp = cv2.matchTemplate(src_bgr[i], sch_bgr[i], cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res_temp)
bgr_confidence[i] = max_val
# 加权可信度
weighted_confidence = bgr_confidence[0] * weight[0] + bgr_confidence[1] * weight[1] + bgr_confidence[2] * weight[2]
return weighted_confidence
示例4: get_image_and_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def get_image_and_mask(img_location, gray_flag):
# Load image from file with alpha channel (UNCHANGED flag). If an alpha
# channel does not exist, just return the base image.
img = cv2.imread(img_location, cv2.IMREAD_UNCHANGED)
if img.shape[2] <= 3:
return img, None
# Create an alpha channel matrix with values between 0-255. Then
# threshold the alpha channel to create a binary mask.
channels = cv2.split(img)
mask = np.array(channels[3])
_, mask = cv2.threshold(mask, 250, 255, cv2.THRESH_BINARY)
# Convert image and mask to grayscale or BGR based on input flag.
if gray_flag:
img = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
else:
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
return img, mask
# Resize an image and mask based on an input scale ratio.
示例5: get_img_random_crop
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def get_img_random_crop(src, resize=512, crop=256):
'''Get & resize image and random crop'''
img = get_img(src)
img = resize_to(img, resize=resize)
offset_h = random.randint(0, (img.shape[0]-crop))
offset_w = random.randint(0, (img.shape[1]-crop))
img = img[offset_h:offset_h+crop, offset_w:offset_w+crop, :]
return img
# def preserve_colors(content_rgb, styled_rgb):
# """Extract luminance from styled image and apply colors from content"""
# if content_rgb.shape != styled_rgb.shape:
# new_shape = (content_rgb.shape[1], content_rgb.shape[0])
# styled_rgb = cv2.resize(styled_rgb, new_shape)
# styled_yuv = cv2.cvtColor(styled_rgb, cv2.COLOR_RGB2YUV)
# Y_s, U_s, V_s = cv2.split(styled_yuv)
# image_YUV = cv2.cvtColor(content_rgb, cv2.COLOR_RGB2YUV)
# Y_i, U_i, V_i = cv2.split(image_YUV)
# styled_rgb = cv2.cvtColor(np.stack([Y_s, U_i, V_i], axis=-1), cv2.COLOR_YUV2RGB)
# return styled_rgb
示例6: brightness
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def brightness(self, im):
'''
Return the brightness of an image
Args:
im(numpy): image
Returns:
float, average brightness of an image
'''
im_hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(im_hsv)
height, weight = v.shape[:2]
total_bright = 0
for i in v:
total_bright = total_bright + sum(i)
return float(total_bright) / (height * weight)
示例7: image_stats
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def image_stats(image):
"""
Parameters:
-------
image: NumPy array
OpenCV image in L*a*b* color space
Returns:
-------
Tuple of mean and standard deviations for the L*, a*, and b*
channels, respectively
"""
# compute the mean and standard deviation of each channel
(l, a, b) = cv2.split(image)
(lMean, lStd) = (l.mean(), l.std())
(aMean, aStd) = (a.mean(), a.std())
(bMean, bStd) = (b.mean(), b.std())
# return the color statistics
return (lMean, lStd, aMean, aStd, bMean, bStd)
示例8: convert_to_original_colors
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def convert_to_original_colors(content_img, stylized_img):
content_img = postprocess(content_img)
stylized_img = postprocess(stylized_img)
if args.color_convert_type == 'yuv':
cvt_type = cv2.COLOR_BGR2YUV
inv_cvt_type = cv2.COLOR_YUV2BGR
elif args.color_convert_type == 'ycrcb':
cvt_type = cv2.COLOR_BGR2YCR_CB
inv_cvt_type = cv2.COLOR_YCR_CB2BGR
elif args.color_convert_type == 'luv':
cvt_type = cv2.COLOR_BGR2LUV
inv_cvt_type = cv2.COLOR_LUV2BGR
elif args.color_convert_type == 'lab':
cvt_type = cv2.COLOR_BGR2LAB
inv_cvt_type = cv2.COLOR_LAB2BGR
content_cvt = cv2.cvtColor(content_img, cvt_type)
stylized_cvt = cv2.cvtColor(stylized_img, cvt_type)
c1, _, _ = cv2.split(stylized_cvt)
_, c2, c3 = cv2.split(content_cvt)
merged = cv2.merge((c1, c2, c3))
dst = cv2.cvtColor(merged, inv_cvt_type).astype(np.float32)
dst = preprocess(dst)
return dst
示例9: random_hue_saturation_value
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [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
示例10: brightness
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def brightness(im):
'''
Return the brightness of an image
Args:
im(numpy): image
Returns:
float, average brightness of an image
'''
im_hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(im_hsv)
height, weight = v.shape[:2]
total_bright = 0
for i in v:
total_bright = total_bright+sum(i)
return float(total_bright)/(height*weight)
示例11: get_alpha
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def get_alpha(imtmp, bgval=1.):
h, w = imtmp.shape[:2]
alpha = (~np.all(imtmp == bgval, axis=2)).astype(imtmp.dtype)
b_channel, g_channel, r_channel = cv2.split(imtmp)
im_RGBA = cv2.merge((b_channel, g_channel, r_channel, alpha.astype(
imtmp.dtype)))
return im_RGBA
示例12: append_alpha
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def append_alpha(imtmp):
alpha = np.ones_like(imtmp[:, :, 0]).astype(imtmp.dtype)
if np.issubdtype(imtmp.dtype, np.uint8):
alpha = alpha * 255
b_channel, g_channel, r_channel = cv2.split(imtmp)
im_RGBA = cv2.merge((b_channel, g_channel, r_channel, alpha))
return im_RGBA
示例13: color_histogram_of_test_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def color_histogram_of_test_image(test_src_image):
# load the image
image = test_src_image
chans = cv2.split(image)
colors = ('b', 'g', 'r')
features = []
feature_data = ''
counter = 0
for (chan, color) in zip(chans, colors):
counter = counter + 1
hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
features.extend(hist)
# find the peak pixel values for R, G, and B
elem = np.argmax(hist)
if counter == 1:
blue = str(elem)
elif counter == 2:
green = str(elem)
elif counter == 3:
red = str(elem)
feature_data = red + ',' + green + ',' + blue
with open(current_path + '/utils/color_recognition_module/'
+ 'test.data', 'w') as myfile:
myfile.write(feature_data)
示例14: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def __init__(self, config):
name = inflection.underscore(type(self).__name__)
self.adjust = tuple(map(int, config.get('augmentation', name).split()))
示例15: __call__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import split [as 别名]
def __call__(self, hsv):
h, s, v = cv2.split(hsv)
adjust = random.randint(*self.adjust)
h = h.astype(np.int) + adjust
h = np.clip(h, 0, 179).astype(hsv.dtype)
return cv2.merge((h, s, v))