本文整理匯總了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邊緣檢測