本文整理汇总了Python中cv2.bitwise_and方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.bitwise_and方法的具体用法?Python cv2.bitwise_and怎么用?Python cv2.bitwise_and使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.bitwise_and方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contour_filter
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def contour_filter(self, frame):
_, contours, _ = cv2.findContours(frame,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
new_frame = np.zeros(frame.shape, np.uint8)
for i, contour in enumerate(contours):
c_area = cv2.contourArea(contour)
if self.contour_min_area <= c_area <= self.contour_max_area:
mask = np.zeros(frame.shape, np.uint8)
cv2.drawContours(mask, contours, i, 255, cv2.FILLED)
mask = cv2.bitwise_and(frame, mask)
new_frame = cv2.bitwise_or(new_frame, mask)
frame = new_frame
if self.contour_disp_flag:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
cv2.drawContours(frame, contours, -1, (255, 0, 0), 1)
return frame
# A number of methods corresponding to the various trackbars available.
示例2: SMGetSalientRegion
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def SMGetSalientRegion(self, src):
# get a binarized saliency map
binarized_SM = self.SMGetBinarizedSM(src)
# GrabCut
img = src.copy()
mask = np.where(
(binarized_SM != 0), cv2.GC_PR_FGD, cv2.GC_PR_BGD).astype('uint8')
bgdmodel = np.zeros((1, 65), np.float64)
fgdmodel = np.zeros((1, 65), np.float64)
rect = (0, 0, 1, 1) # dummy
iterCount = 1
cv2.grabCut(img, mask=mask, rect=rect, bgdModel=bgdmodel,
fgdModel=fgdmodel, iterCount=iterCount, mode=cv2.GC_INIT_WITH_MASK)
# post-processing
mask_out = np.where(
(mask == cv2.GC_FGD) + (mask == cv2.GC_PR_FGD), 255, 0).astype('uint8')
output = cv2.bitwise_and(img, img, mask=mask_out)
return output
示例3: standard_test
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def standard_test(self):
for fnum in range(self.start_fnum, self.stop_fnum):
frame = util.get_frame(self.capture, fnum)
frame = frame[280:, :]
frame_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(frame_HSV, (self.low_H, self.low_S, self.low_V),
(self.high_H, self.high_S, self.high_V))
res = cv2.bitwise_and(frame, frame, mask=mask)
res_inv = cv2.bitwise_and(frame, frame, mask=cv2.bitwise_not(mask))
cv2.imshow(self.window_name, mask)
cv2.imshow('Video Capture AND', res)
cv2.imshow('Video Capture INV', res_inv)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
# A number of methods corresponding to the various trackbars available.
示例4: remove_other_color
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def remove_other_color(img):
frame = cv2.GaussianBlur(img, (3,3), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([100,128,0])
upper_blue = np.array([215,255,255])
# Threshold the HSV image to get only blue colors
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
lower_white = np.array([0,0,128], dtype=np.uint8)
upper_white = np.array([255,255,255], dtype=np.uint8)
# Threshold the HSV image to get only blue colors
mask_white = cv2.inRange(hsv, lower_white, upper_white)
lower_black = np.array([0,0,0], dtype=np.uint8)
upper_black = np.array([170,150,50], dtype=np.uint8)
mask_black = cv2.inRange(hsv, lower_black, upper_black)
mask_1 = cv2.bitwise_or(mask_blue, mask_white)
mask = cv2.bitwise_or(mask_1, mask_black)
# Bitwise-AND mask and original image
#res = cv2.bitwise_and(frame,frame, mask= mask)
return mask
示例5: _pre_process_input_minimal
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def _pre_process_input_minimal(self, img, mask, t, darker_fg=True):
if self._buff_grey is None:
self._buff_grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if mask is None:
mask = np.ones_like(self._buff_grey) * 255
cv2.cvtColor(img,cv2.COLOR_BGR2GRAY, self._buff_grey)
cv2.erode(self._buff_grey, self._erode_kern, dst=self._buff_grey)
if darker_fg:
cv2.subtract(255, self._buff_grey, self._buff_grey)
if mask is not None:
cv2.bitwise_and(self._buff_grey, mask, self._buff_grey)
return self._buff_grey
示例6: backprojection
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def backprojection(target, roihist):
'''图像预处理'''
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
# Now convolute with circular disc
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))
cv2.filter2D(dst,-1,disc,dst)
# threshold and binary AND
ret,binary = cv2.threshold(dst,80,255,0)
# 创建 核
kernel = np.ones((5,5), np.uint8)
iter_time = 1
# 闭运算
binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel,iterations=iter_time)
thresh = cv2.merge((binary,binary,binary))
target_filter = cv2.bitwise_and(target,thresh)
return binary, target_filter
示例7: getSignature
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def getSignature(img):
imgSize = np.shape(img)
gImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Adaptive Thresholding requires the blocksize to be odd and bigger than 1
blockSize = 1 / 8 * imgSize[0] / 2 * 2 + 1
if blockSize <= 1:
blockSize = imgSize[0] / 2 * 2 + 1
const = 10
mask = cv2.adaptiveThreshold(gImg, maxValue = 255, adaptiveMethod = cv2.ADAPTIVE_THRESH_MEAN_C, thresholdType = cv2.THRESH_BINARY, blockSize = blockSize, C = const)
rmask = cv2.bitwise_not(mask)
return (cv2.bitwise_and(img, img, mask=rmask), rmask)
# First Prompt
示例8: diffImg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def diffImg(self, t0, t1, t2):
d1 = cv.absdiff(t2, t1)
d2 = cv.absdiff(t1, t0)
return cv.bitwise_and(d1, d2)
示例9: handsegment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def handsegment(frame):
lower, upper = boundaries[0]
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
mask1 = cv2.inRange(frame, lower, upper)
lower, upper = boundaries[1]
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
mask2 = cv2.inRange(frame, lower, upper)
# for i,(lower, upper) in enumerate(boundaries):
# # create NumPy arrays from the boundaries
# lower = np.array(lower, dtype = "uint8")
# upper = np.array(upper, dtype = "uint8")
# # find the colors within the specified boundaries and apply
# # the mask
# if(i==0):
# print "Harish"
# mask1 = cv2.inRange(frame, lower, upper)
# else:
# print "Aadi"
# mask2 = cv2.inRange(frame, lower, upper)
mask = cv2.bitwise_or(mask1, mask2)
output = cv2.bitwise_and(frame, frame, mask=mask)
# show the images
# cv2.imshow("images", mask)
# cv2.imshow("images", output)
return output
示例10: remove_bg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def remove_bg(frame):
fg_mask=bg_model.apply(frame)
kernel = np.ones((3,3),np.uint8)
fg_mask=cv2.erode(fg_mask,kernel,iterations = 1)
frame=cv2.bitwise_and(frame,frame,mask=fg_mask)
return frame
# ------------------------ BEGIN ------------------------ #
# Camera
示例11: diff_remove_bg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def diff_remove_bg(img0, img, img1): # removes the background but requires three images
d1 = diff(img0, img)
d2 = diff(img, img1)
return cv2.bitwise_and(d1, d2)
# img1=cv2.imread('subtract1.jpg')
示例12: motion_detection
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def motion_detection(camera, folder, until):
"""
Uses 3 frames to look for motion, can't remember where
I found it but it gives better result than my first try
with comparing 2 frames.
"""
utils.clear_directory(folder)
# Need to get 2 images to start with
previous_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY)
current_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY)
purple = (140, 25, 71)
while True:
now = datetime.datetime.now()
_, image = camera.read()
gray_image = cv2.cvtColor(image, cv2.cv.CV_RGB2GRAY)
difference1 = cv2.absdiff(previous_image, gray_image)
difference2 = cv2.absdiff(current_image, gray_image)
result = cv2.bitwise_and(difference1, difference2)
# Basic threshold, turn the bitwise_and into a black or white (haha)
# result, white (255) being a motion
_, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY)
# Let's show a square around the detected motion in the original pic
low_point, high_point = utils.find_motion_boundaries(result.tolist())
if low_point is not None and high_point is not None:
cv2.rectangle(image, low_point, high_point, purple, 3)
print 'Motion detected ! Taking picture'
utils.save_image(image, folder, now)
previous_image = current_image
current_image = gray_image
if utils.time_over(until, now):
break
del(camera)
示例13: roi
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def roi(img, vertices):
#blank mask:
mask = np.zeros_like(img)
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, vertices, 255)
#returning the image only where mask pixels are nonzero
masked = cv2.bitwise_and(img, mask)
return masked
示例14: roi
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def roi(img, vertices):
mask = np.zeros_like(img)
cv2.fillPoly(mask, vertices, 255)
masked = cv2.bitwise_and(img, mask)
return masked
示例15: preprocessing_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import bitwise_and [as 别名]
def preprocessing_image(self, frame):
height, width = frame.shape[:2]
mask = np.zeros((height, width), np.uint8)
cv2.rectangle(mask, (width / 3 + 10, height / 3 - 130),
(width * 2 / 3 + 10, height * 2 / 3 - 10), [255, 255, 255], thickness=-1)
masked_img = cv2.bitwise_and(frame, frame, mask=mask)
return masked_img