本文整理汇总了Python中cv2.add方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.add方法的具体用法?Python cv2.add怎么用?Python cv2.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: maximizeContrast
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def maximizeContrast(imgGrayscale):
height, width = imgGrayscale.shape
imgTopHat = np.zeros((height, width, 1), np.uint8)
imgBlackHat = np.zeros((height, width, 1), np.uint8)
structuringElement = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
imgTopHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_TOPHAT, structuringElement)
imgBlackHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_BLACKHAT, structuringElement)
imgGrayscalePlusTopHat = cv2.add(imgGrayscale, imgTopHat)
imgGrayscalePlusTopHatMinusBlackHat = cv2.subtract(imgGrayscalePlusTopHat, imgBlackHat)
return imgGrayscalePlusTopHatMinusBlackHat
# end function
示例2: random_hue_saturation_value
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [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
示例3: detect_shirt
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def detect_shirt(self):
#self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8))
self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8))
cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
fg=cv2.erode(self.dst,None,iterations=2)
#cv2.imshow("fore",fg)
bg=cv2.dilate(self.dst,None,iterations=3)
_,bg=cv2.threshold(bg, 1,128,1)
#cv2.imshow("back",bg)
mark=cv2.add(fg,bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
self.m=cv2.convertScaleAbs(mark32)
_,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#cv2.imshow("final_tshirt",self.m)
cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE)
return self.m,cntr
示例4: randomHueSaturationValue
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [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
示例5: render
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def render(img, res):
res *= 255
res = res.astype(np.uint8)
res = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR) # to white color
res[:, :, 0] = 0 # remove blue channel
res[:, :, 2] = 0 # remove red channel
res = cv2.resize(res, (WIDTH, HEIGHT), interpolation=cv2.INTER_LINEAR) # resize 15x8 to 1280x720
org = cv2.resize(img, (WIDTH, HEIGHT))
added = cv2.add(org, res) # combine input, output
cv2.imshow('result', added)
cv2.waitKey(0)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# print('User Interrupted')
# exit(1)
示例6: visualize
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def visualize(img):
global grid
grid = np.round(grid)
h, w, c = img.shape
box = np.zeros((h, w), dtype=np.uint8)
for i in range(len(grid)):
for j in range(len(grid[i])):
box[i*KERNEL : (i+1)*KERNEL, j*KERNEL : (j+1)*KERNEL] = grid[i][j] * 255
box = cv2.cvtColor(box, cv2.COLOR_GRAY2BGR)
box[:,:,0]=0
box[:,:,2]=0
visual = cv2.add(img, box)
for i in range(0, HEIGHT, KERNEL): # 가로선
cv2.line(visual, (0, i), (WIDTH, i), color=(255,255,255))
for i in range(0, WIDTH, KERNEL): # 세로선
cv2.line(visual, (i, 0), (i, HEIGHT), color=(255,255,255))
return visual
示例7: subtract_bgnd
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def subtract_bgnd(self, image):
# new method using bitwise not
def _remove_func(_img, _func, _bg):
#the reason to use opencv2 instead of numpy is to avoid buffer overflow
#https://stackoverflow.com/questions/45817037/opencv-image-subtraction-vs-numpy-subtraction/45817868
new_img = np.zeros_like(_img); #maybe can do this in place
if image.ndim == 2:
_func(_img, _bg, new_img)
else:
for ii, this_frame in enumerate(_img):
_func(this_frame, _bg, new_img[ii])
return new_img
bg = self.bgnd.astype(np.uint8)
if self.is_light_background:
notbg = ~bg
ss = _remove_func(image, cv2.add, notbg)
else: # fluorescence
ss = _remove_func(image, cv2.subtract, bg)
ss = np.clip( ss ,1,255);
return ss
示例8: _draw_on_top
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def _draw_on_top(self, img, x, y, sub_img, sub_name=''):
h, w, _ = sub_img.shape
mask = sub_img[:, :, 3]
mask_inv = cv2.bitwise_not(mask)
sub_img_ = sub_img[:, :, :3]
background = img[y:y + h, x:x + w]
try:
background = cv2.bitwise_and(background, background, mask=mask_inv)
except cv2.error as e:
raise ThugError(
'Can not draw {}, please try with smaller {}.'.format(
sub_name, sub_name))
foreground = cv2.bitwise_and(sub_img_, sub_img_, mask=mask)
sum_ = cv2.add(background, foreground)
img[y:y + h, x:x + w] = sum_
示例9: get_init_process_img
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def get_init_process_img(roi_img):
"""
对图片进行初始化处理,包括,梯度化,高斯模糊,二值化,腐蚀,膨胀和边缘检测
:param roi_img: ndarray
:return: ndarray
"""
h = cv2.Sobel(roi_img, cv2.CV_32F, 0, 1, -1)
v = cv2.Sobel(roi_img, cv2.CV_32F, 1, 0, -1)
img = cv2.add(h, v)
img = cv2.convertScaleAbs(img)
img = cv2.GaussianBlur(img, (3, 3), 0)
ret, img = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=2)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=2)
img = auto_canny(img)
return img
示例10: _shift_hsv_non_uint8
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def _shift_hsv_non_uint8(img, hue_shift, sat_shift, val_shift):
dtype = img.dtype
img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
hue, sat, val = cv2.split(img)
if hue_shift != 0:
hue = cv2.add(hue, hue_shift)
hue = np.where(hue < 0, hue + 180, hue)
hue = np.where(hue > 180, hue - 180, hue)
hue = hue.astype(dtype)
if sat_shift != 0:
sat = clip(cv2.add(sat, sat_shift), dtype, 255 if dtype == np.uint8 else 1.0)
if val_shift != 0:
val = clip(cv2.add(val, val_shift), dtype, 255 if dtype == np.uint8 else 1.0)
img = cv2.merge((hue, sat, val)).astype(dtype)
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
return img
示例11: _execute
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def _execute(self, *args):
"""
Create a watermark image.
:param agrs: <[RGB}> image to watermark
:return: <RGB> watermarked image
"""
# Add alpha channel if missing
if args[0].shape[2] < 4:
img = np.dstack([args[0], np.ones((512, 512), dtype="uint8") * 255])
f1 = np.asarray([0, 0, 0, 250]) # red color filter
f2 = np.asarray([255, 255, 255, 255])
mask = cv2.bitwise_not(cv2.inRange(self.__watermark, f1, f2))
mask_inv = cv2.bitwise_not(mask)
res1 = cv2.bitwise_and(img, img, mask=mask)
res2 = cv2.bitwise_and(self.__watermark, self.__watermark, mask=mask_inv)
res = cv2.add(res1, res2)
alpha = 0.6
return cv2.addWeighted(res, alpha, img, 1 - alpha, 0)
示例12: getthresholdedimg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def getthresholdedimg(hsv):
yellow = cv2.inRange(hsv, np.array((20, 100, 100)), np.array((30, 255, 255)))
blue = cv2.inRange(hsv, np.array((100, 100, 100)), np.array((120, 255, 255)))
both = cv2.add(yellow, blue)
return both
示例13: _split_channel_images
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def _split_channel_images(self):
blue, green, red = cv2.split(self._image)
split_channel_images = [
red,
green,
blue,
cv2.add(red, green),
cv2.add(red, blue),
cv2.add(green, blue)
]
return split_channel_images
示例14: read
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def read(self, dst=None):
w, h = self.frame_size
if self.bg is None:
buf = np.zeros((h, w, 3), np.uint8)
else:
buf = self.bg.copy()
self.render(buf)
if self.noise > 0.0:
noise = np.zeros((h, w, 3), np.int8)
cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
return True, buf
示例15: read
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import add [as 别名]
def read(self, dst=None):
w, h = self.frame_size
if self.bg is None:
buf = np.zeros((h, w, 3), np.uint8)
else:
buf = self.bg.copy()
self.render(buf)
if self.noise > 0.0:
noise = np.zeros((h, w, 3), np.int8)
cv2.randn(noise, np.zeros(3), np.ones(3) * 255 * self.noise)
buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
return True, buf