本文整理匯總了Python中cv2.bitwise_or方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.bitwise_or方法的具體用法?Python cv2.bitwise_or怎麽用?Python cv2.bitwise_or使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.bitwise_or方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: contour_filter
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [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: remove_other_color
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [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
示例3: skeletonize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def skeletonize(image_in):
'''Inputs and grayscale image and outputs a binary skeleton image'''
size = np.size(image_in)
skel = np.zeros(image_in.shape, np.uint8)
ret, image_edit = cv2.threshold(image_in, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
done = False
while not done:
eroded = cv2.erode(image_edit, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(image_edit, temp)
skel = cv2.bitwise_or(skel, temp)
image_edit = eroded.copy()
zeros = size - cv2.countNonZero(image_edit)
if zeros == size:
done = True
return skel
示例4: binarize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def binarize(self, src):
"""Image binarization.
Args:
src (int): Input image BGR.
numpy.ndarray, (256, 512, 3), 0~255
Returns:
dst (int): Output image.
numpy.ndarray, (256, 512), 0~1
"""
# resize
if src.shape[0] != self.__height or src.shape[1] != self.__width:
src = cv2.resize(src, (self.__width, self.__height), interpolation=cv2.INTER_LINEAR)
# image thresholding
image_binary_canny = self.__apply_canny(src)
image_binary_mthreshold = self.__apply_multi_threshold(src)
image_binary_all = cv2.bitwise_or(image_binary_canny, image_binary_mthreshold)
# mask top and bottom
dst = self.__mask_image(image_binary_all)
return dst
示例5: paste_patch
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def paste_patch(self,pos,w,paste_img):
pos_x = int(pos.x()/self.scale)
pos_y = int(pos.y()/self.scale)
w = int(w)
paste_img = cv2.resize(paste_img,(self.img_size,self.img_size))
mask = np.full((self.img_size, self.img_size), 0 ,dtype = np.uint8)
mask = cv2.rectangle(mask,(pos_x,pos_y),( pos_x + w , pos_y + w ),(255,255,255),thickness=-1)
img = self.img
foreground_img = cv2.bitwise_or(paste_img,paste_img,mask=mask)
back_mask = cv2.bitwise_not(mask)
background_img = cv2.bitwise_or(img, img, mask = back_mask)
self.img = cv2.bitwise_or(foreground_img,background_img)
示例6: add_background
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def add_background(img, img_box, img_background=None):
if img_background is None:
# create random image
img_background = draw_random_img(img.shape)
else:
# check if we have to resize img_background
if img_background.shape != img.shape:
#print('resizing img background')
(h, w) = img.shape[:2]
img_background = cv2.resize(img_background,(w,h))
# check if we have to convert to gray image
if img.ndim == 2:
img_background = cv2.cvtColor(img_background,cv2.COLOR_RGB2GRAY)
#print('img.shape:',img.shape,', img_background.shape:',img_background.shape)
mask = mask_from_polygon(img.shape,img_box)
inverse_mask = cv2.bitwise_not(mask)
img_background = cv2.bitwise_or(img_background, img_background, mask=inverse_mask)
# combine foreground+background
final = cv2.bitwise_or(img, img_background)
return final
示例7: skeletize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def skeletize(img):
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
done = False
while not done:
eroded = cv2.erode(img, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img = eroded.copy()
zeroes = size - cv2.countNonZero(img)
if zeroes == size:
done = True
return skel
示例8: extract_color
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
if h_th_low > h_th_up:
ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY)
ret, h_dst_2 = cv2.threshold(h, h_th_up, 255, cv2.THRESH_BINARY_INV)
dst = cv2.bitwise_or(h_dst_1, h_dst_2)
else:
ret, dst = cv2.threshold(h, h_th_low, 255, cv2.THRESH_TOZERO)
ret, dst = cv2.threshold(dst, h_th_up, 255, cv2.THRESH_TOZERO_INV)
ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)
ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
dst = cv2.bitwise_and(dst, s_dst)
dst = cv2.bitwise_and(dst, v_dst)
return dst
示例9: skeletonize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def skeletonize(img):
""" OpenCV function to return a skeletonized version of img, a Mat object"""
# hat tip to http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/
img = img.copy() # don't clobber original
skel = img.copy()
skel[:,:] = 0
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
while True:
eroded = cv2.morphologyEx(img, cv2.MORPH_ERODE, kernel)
temp = cv2.morphologyEx(eroded, cv2.MORPH_DILATE, kernel)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img[:,:] = eroded[:,:]
if cv2.countNonZero(img) == 0:
break
return skel
示例10: _maskBackground
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def _maskBackground(self, img, mask_corners = True):
h,w,c = np.shape(img)
blur_img=cv2.blur(img, (5,5))
hsv = cv2.cvtColor(blur_img, cv2.COLOR_BGR2HSV)
lower_color = np.array([22,28,26])
upper_color = np.array([103,255,255])
# create binary mask by finding background color range
mask = cv2.inRange(hsv, self.lower_mask_color, self.upper_mask_color)
# remove the corners from mask since they are prone to illumination problems
if(mask_corners):
circle_mask = np.zeros((h, w), np.uint8)
circle_mask[:, :] = 255
cv2.circle(circle_mask,(w/2, h/2), min(w/2, h/2), 0, -1)
mask = cv2.bitwise_or(mask,circle_mask)
# invert mask to get white objects on black background
#inverse_mask = 255 - mask
if self._interactive: cv2.imshow("binary mask", mask)
if self._interactive: cv2.waitKey(0)
return mask
示例11: logical_or
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def logical_or(bin_img1, bin_img2):
"""Join two images using the bitwise OR operator.
Inputs:
bin_img1 = Binary image data to be compared to bin_img2
bin_img2 = Binary image data to be compared to bin_img1
Returns:
merged = joined binary image
:param bin_img1: numpy.ndarray
:param bin_img2: numpy.ndarray
:return merged: numpy.ndarray
"""
params.device += 1
merged = cv2.bitwise_or(bin_img1, bin_img2)
if params.debug == 'print':
print_image(merged, os.path.join(params.debug_outdir, str(params.device) + '_or_joined.png'))
elif params.debug == 'plot':
plot_image(merged, cmap='gray')
return merged
示例12: overlay_masks
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def overlay_masks(self, image, masks, classes, ids=None):
colors = self.compute_colors_for_labels(classes).tolist()
segments_image = image.copy()
aggregated_mask = np.zeros(image.shape[:2], dtype=np.uint8)
aggregated_colored_mask = np.zeros(image.shape, dtype=np.uint8)
black = np.zeros(3, dtype=np.uint8)
for i, (mask, color) in enumerate(zip(masks, colors)):
color_idx = i if ids is None else ids[i]
mask_color = self.instance_color_palette[color_idx % len(self.instance_color_palette)].tolist()
cv2.bitwise_or(aggregated_mask, mask, dst=aggregated_mask)
cv2.bitwise_or(aggregated_colored_mask, np.asarray(mask_color, dtype=np.uint8),
dst=aggregated_colored_mask, mask=mask)
# Fill the area occupied by all instances with a colored instances mask image.
cv2.bitwise_and(segments_image, black, dst=segments_image, mask=aggregated_mask)
cv2.bitwise_or(segments_image, aggregated_colored_mask, dst=segments_image, mask=aggregated_mask)
# Blend original image with the one, where instances are colored.
# As a result instances masks become transparent.
cv2.addWeighted(image, 0.5, segments_image, 0.5, 0, dst=image)
return image
示例13: handsegment
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [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
示例14: combine_images
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def combine_images():
im1 = cv2.imread('/Users/fogleman/Workspace/maptiles/zoom18t.png')
im2 = cv2.imread('/Users/fogleman/Workspace/maptiles/zoom19t.png')
im3 = cv2.imread('/Users/fogleman/Workspace/maptiles/zoom20s.png')
im1 = isolate_buildings(im1)
im2 = isolate_buildings(im2)
im3 = isolate_buildings(im3)
im = cv2.bitwise_or(cv2.bitwise_or(im1, im2), im3)
return im
示例15: mask_overlay
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import bitwise_or [as 別名]
def mask_overlay(src, mask):
"""Image overlaying.
Args:
src (int): Input image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
mask (int): Input image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
Returns:
dst (int): Output image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
"""
# binarize
gray = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
ret, mask_bynary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
mask_bynary = mask_bynary // 255
# resize
if src.shape[0] != mask.shape[0] or src.shape[1] != mask.shape[1]:
mask = cv2.resize(mask, (src.shape[1], src.shape[0]), interpolation=cv2.INTER_LINEAR)
mask_bynary = cv2.resize(mask_bynary, (src.shape[1], src.shape[0]), interpolation=cv2.INTER_LINEAR)
# prepare white image
mask_white = np.dstack((mask_bynary*255, mask_bynary*255, mask_bynary*255))
# combine src and thresholded image
not_mask = cv2.bitwise_not(mask_white)
src_masked = cv2.bitwise_and(src, not_mask)
dst = cv2.bitwise_or(src_masked, mask)
return dst