本文整理汇总了Python中cv2.THRESH_OTSU属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.THRESH_OTSU属性的具体用法?Python cv2.THRESH_OTSU怎么用?Python cv2.THRESH_OTSU使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.THRESH_OTSU属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_proto_objects_map
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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
示例2: crop_row_detect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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
示例3: skeletonize
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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: sobelOperT
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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
示例5: DeleteNotArea
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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)]
示例6: colorSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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
示例7: param_filter
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def param_filter(self, frame):
# Apply pre-blur according to trackbar value.
if self.pre_blur_val == 1:
frame = cv2.GaussianBlur(frame, (5, 5), 0)
elif self.pre_blur_val == 2:
frame = cv2.medianBlur(frame, 5)
# Apply a thresholding method according to trackbar value.
if self.thresh_flag:
_, frame = cv2.threshold(frame, 127, 255, cv2.THRESH_BINARY)
else:
_, frame = cv2.threshold(frame, 127, 255, cv2.THRESH_OTSU)
# Apply post-blur according to trackbar value.
if self.post_blur_val:
frame = cv2.medianBlur(frame, 5)
return frame
# Apply filterrs to frame according to contour parameters.
示例8: verticalEdgeDetection
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def verticalEdgeDetection(image):
image_sobel = cv2.Sobel(image.copy(),cv2.CV_8U,1,0)
# image = auto_canny(image_sobel)
# img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT
# canny_image = auto_canny(image)
flag,thres = cv2.threshold(image_sobel,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY)
print(flag)
flag,thres = cv2.threshold(image_sobel,int(flag*0.7),255,cv2.THRESH_BINARY)
# thres = simpleThres(image_sobel)
kernal = np.ones(shape=(3,15))
thres = cv2.morphologyEx(thres,cv2.MORPH_CLOSE,kernal)
return thres
#确定粗略的左右边界
示例9: compare_ssim_debug
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def compare_ssim_debug(image_a, image_b, color=(255, 0, 0)):
"""
Args:
image_a, image_b: opencv image or PIL.Image
color: (r, g, b) eg: (255, 0, 0) for red
Refs:
https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/
"""
ima, imb = conv2cv(image_a), conv2cv(image_b)
score, diff = compare_ssim(ima, imb, full=True)
diff = (diff * 255).astype('uint8')
_, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cv2color = tuple(reversed(color))
im = ima.copy()
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(im, (x, y), (x+w, y+h), cv2color, 2)
# todo: show image
cv2pil(im).show()
return im
示例10: extracttext
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def extracttext(imgpath, preprocess):
if imgpath.startswith('http://') or imgpath.startswith('https://') or imgpath.startswith('ftp://'):
image = url_to_image(imgpath)
else:
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif preprocess == "blur":
gray = cv2.medianBlur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
return {"text": text}
示例11: remove_background
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def remove_background(img):
""" Remove noise using OTSU's method.
:param img: The image to be processed
:return: The normalized image
"""
img = img.astype(np.uint8)
# Binarize the image using OTSU's algorithm. This is used to find the center
# of mass of the image, and find the threshold to remove background noise
threshold, _ = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Remove noise - anything higher than the threshold. Note that the image is still grayscale
img[img > threshold] = 255
return img
示例12: binarize_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def binarize_image(input_image, threshold_value=177):
"""
Convert input_image to binary representation
:param input_image: image
:type input_image: numpy.ndarray
:param threshold_value: value to be used as a
threshold
:type threshold_value: int
:return: image in binary form
:rtype: numpy.ndarray
"""
gray_image = grayscale_image(input_image)
bin_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
_, bin_image = cv2.threshold(bin_image,
threshold_value,
255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return bin_image
示例13: binary_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def binary_image(self,img):
# 应用5种不同的阈值方法
# ret, th1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
# ret, th2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
# ret, th3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
# ret, th4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
# ret, th5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)
# titles = ['Gray', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
# images = [img_gray, th1, th2, th3, th4, th5]
# 使用Matplotlib显示
# for i in range(6):
# plt.subplot(2, 3, i + 1)
# plt.imshow(images[i], 'gray')
# plt.title(titles[i], fontsize=8)
# plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
# plt.show()
# Otsu阈值
_, th = cv2.threshold(img, 0, 255, cv2.THRESH_TOZERO + cv2.THRESH_OTSU)
cv2.imshow('Binary image', th)
return th
# 边缘检测
示例14: detect_shirt
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [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
示例15: threshold
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_OTSU [as 别名]
def threshold(self):
src = self.cv_read_img(self.src_file)
if src is None:
return
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# 这个函数的第一个参数就是原图像,原图像应该是灰度图。
# 第二个参数就是用来对像素值进行分类的阈值。
# 第三个参数就是当像素值高于(有时是小于)阈值时应该被赋予的新的像素值
# 第四个参数来决定阈值方法,见threshold_simple()
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
ret, dst = cv.threshold(gray, 127, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
self.decode_and_show_dst(dst)
# Canny边缘检测