本文整理汇总了Python中cv2.THRESH_BINARY属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.THRESH_BINARY属性的具体用法?Python cv2.THRESH_BINARY怎么用?Python cv2.THRESH_BINARY使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.THRESH_BINARY属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: canny
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def canny(filepathname, left=70, right=140):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v, cv2.COLOR_BGR2GRAY)
s = cv2.Canny(s, left, right)
cv2.imshow('nier',s)
return s
# 圈出最小方矩形框,这里Canny算法后都是白色线条,所以取色范围 127-255 即可。
# ret, binary = cv2.threshold(s,127,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10: # 有约束的画框
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# # cv2.drawContours(s,contours,-1,(0,0,255),3) # 画所有框
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()
示例2: laplacian
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def laplacian(filepathname):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v, cv2.COLOR_BGR2GRAY)
s = cv2.Laplacian(s, cv2.CV_16S, ksize=3)
s = cv2.convertScaleAbs(s)
cv2.imshow('nier',s)
return s
# ret, binary = cv2.threshold(s,40,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10:
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()
示例3: segment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#-----------------
# MAIN FUNCTION
#-----------------
示例4: main
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def main():
capture = cv2.VideoCapture(0)
_, image = capture.read()
previous = image.copy()
while (cv2.waitKey(1) < 0):
_, image = capture.read()
diff = cv2.absdiff(image, previous)
#image = cv2.flip(image, 3)
#image = cv2.norm(image)
_, diff = cv2.threshold(diff, 32, 0, cv2.THRESH_TOZERO)
_, diff = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY)
diff = cv2.medianBlur(diff, 5)
cv2.imshow('video', diff)
previous = image.copy()
capture.release()
cv2.destroyAllWindows()
示例5: prediction
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def prediction(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (21, 21), 0)
if self.avg is None:
self.avg = image.copy().astype(float)
cv2.accumulateWeighted(image, self.avg, 0.5)
frameDelta = cv2.absdiff(image, cv2.convertScaleAbs(self.avg))
thresh = cv2.threshold(
frameDelta, DELTA_THRESH, 255,
cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
self.avg = image.copy().astype(float)
return cnts
示例6: find_squares
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [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
示例7: get_proto_objects_map
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def get_proto_objects_map(self, use_otsu=True):
"""Returns the proto-objects map of an RGB image
This method generates a proto-objects map of an RGB image.
Proto-objects are saliency hot spots, generated by thresholding
the saliency map.
:param use_otsu: flag whether to use Otsu thresholding (True) or
a hardcoded threshold value (False)
:returns: proto-objects map
"""
saliency = self.get_saliency_map()
if use_otsu:
_, img_objects = cv2.threshold(np.uint8(saliency*255), 0, 255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU)
else:
thresh = np.mean(saliency)*255*3
_, img_objects = cv2.threshold(np.uint8(saliency*255), thresh, 255,
cv2.THRESH_BINARY)
return img_objects
示例8: crop_row_detect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def crop_row_detect(image_in):
save_image('0_image_in', image_in)
### Grayscale Transform ###
image_edit = grayscale_transform(image_in)
save_image('1_image_gray', image_edit)
### Binarization ###
_, image_edit = cv2.threshold(image_edit, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
save_image('2_image_bin', image_edit)
### Stripping ###
crop_points = strip_process(image_edit)
save_image('8_crop_points', crop_points)
### Hough Transform ###
crop_lines = crop_point_hough(crop_points)
save_image('9_image_hough', cv2.addWeighted(image_in, 1, crop_lines, 1, 0.0))
return crop_lines
示例9: skeletonize
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [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
示例10: blend_non_transparent
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def blend_non_transparent(sprite, background_img):
gray_overlay = cv2.cvtColor(background_img, cv2.COLOR_BGR2GRAY)
overlay_mask = cv2.threshold(gray_overlay, 1, 255, cv2.THRESH_BINARY)[1]
overlay_mask = cv2.erode(overlay_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))
overlay_mask = cv2.blur(overlay_mask, (3, 3))
background_mask = 255 - overlay_mask
overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)
sprite_part = (sprite * (1 / 255.0)) * (background_mask * (1 / 255.0))
overlay_part = (background_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))
return np.uint8(cv2.addWeighted(sprite_part, 255.0, overlay_part, 255.0, 0.0))
示例11: pre_process_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def pre_process_image(img, skip_dilate=False):
"""Uses a blurring function, adaptive thresholding and dilation to expose the main features of an image."""
# Gaussian blur with a kernal size (height, width) of 9.
# Note that kernal sizes must be positive and odd and the kernel must be square.
proc = cv2.GaussianBlur(img.copy(), (9, 9), 0)
# Adaptive threshold using 11 nearest neighbour pixels
proc = cv2.adaptiveThreshold(proc, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Invert colours, so gridlines have non-zero pixel values.
# Necessary to dilate the image, otherwise will look like erosion instead.
proc = cv2.bitwise_not(proc, proc)
if not skip_dilate:
# Dilate the image to increase the size of the grid lines.
kernel = np.array([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]],np.uint8)
proc = cv2.dilate(proc, kernel)
return proc
示例12: sobelOperT
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [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
示例13: DeleteNotArea
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def DeleteNotArea(self, in_img):
input_gray = cv2.cvtColor(in_img, cv2.COLOR_BGR2GRAY)
w = in_img.shape[1]
h = in_img.shape[0]
tmp_mat = in_img[int(h * 0.1):int(h * 0.85), int(w * 0.15):int(w * 0.85)]
plateType = getPlateType(tmp_mat, True)
if plateType == 'BLUE':
tmp = in_img[int(h * 0.1):int(h * 0.85), int(w * 0.15):int(w * 0.85)]
threadHoldV = ThresholdOtsu(tmp)
_, img_threshold = cv2.threshold(input_gray, threadHoldV, 255, cv2.THRESH_BINARY)
elif plateType == 'YELLOW':
tmp = in_img[int(h * 0.1):int(h * 0.85), int(w * 0.15):int(w * 0.85)]
threadHoldV = ThresholdOtsu(tmp)
_, img_threshold = cv2.threshold(input_gray, threadHoldV, 255, cv2.THRESH_BINARY_INV)
else:
_, img_threshold = cv2.threshold(input_gray, 10, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
top, bottom = clearLiuDing(img_threshold, 0, img_threshold.shape[0] - 1)
posLeft, posRight, flag = bFindLeftRightBound1(img_threshold)
if flag:
in_img = in_img[int(top):int(bottom), int(posLeft):int(w)]
示例14: colorSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def colorSearch(self, src, color, out_rect):
"""
:param src:
:param color:
:param out_rect: minAreaRect
:return: binary
"""
color_morph_width = 10
color_morph_height = 2
match_gray = colorMatch(src, color, False)
_, src_threshold = cv2.threshold(match_gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (color_morph_width, color_morph_height))
src_threshold = cv2.morphologyEx(src_threshold, cv2.MORPH_CLOSE, element)
out = src_threshold.copy()
_, contours, _ = cv2.findContours(src_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
mr = cv2.minAreaRect(cnt)
if self.verifySizes(mr):
out_rect.append(mr)
return out
示例15: sobel
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_BINARY [as 别名]
def sobel(filepathname):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v,cv2.COLOR_BGR2GRAY)
x, y = cv2.Sobel(s,cv2.CV_16S,1,0), cv2.Sobel(s,cv2.CV_16S,0,1)
s = cv2.convertScaleAbs(cv2.subtract(x,y))
s = cv2.blur(s,(9,9))
cv2.imshow('nier',s)
return s
# ret, binary = cv2.threshold(s,40,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10:
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()