本文整理汇总了Python中cv2.THRESH_TOZERO属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.THRESH_TOZERO属性的具体用法?Python cv2.THRESH_TOZERO怎么用?Python cv2.THRESH_TOZERO使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.THRESH_TOZERO属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [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()
示例2: binary_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [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
# 边缘检测
示例3: main
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def main():
threshold = 0
max_value = 255
image = cv2.imread("../data/7.1.08.tiff", 0)
# when applying OTSU threshold, set threshold to 0.
_, output1 = cv2.threshold(image, threshold, max_value, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_, output2 = cv2.threshold(image, threshold, max_value, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
_, output3 = cv2.threshold(image, threshold, max_value, cv2.THRESH_TOZERO + cv2.THRESH_OTSU)
_, output4 = cv2.threshold(image, threshold, max_value, cv2.THRESH_TOZERO_INV + cv2.THRESH_OTSU)
_, output5 = cv2.threshold(image, threshold, max_value, cv2.THRESH_TRUNC + cv2.THRESH_OTSU)
images = [image, output1, output2, output3, output4, output5]
titles = ["Orignals", "Binary", "Binary Inverse", "TOZERO", "TOZERO INV", "TRUNC"]
for i in range(6):
plt.subplot(3, 2, i + 1)
plt.imshow(images[i], cmap='gray')
plt.title(titles[i])
plt.show()
示例4: get_estimated_box
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def get_estimated_box(heatmap, option):
gray_heatmap = cv2.cvtColor(heatmap.astype('uint8'), cv2.COLOR_RGB2GRAY)
threshold_value = int(np.max(gray_heatmap) * option.cam_threshold)
_, thresholded_gray_heatmap = cv2.threshold(gray_heatmap, threshold_value,
255, cv2.THRESH_TOZERO)
_, contours, _ = cv2.findContours(thresholded_gray_heatmap,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
return [0, 0, 1, 1]
c = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(c)
estimated_box = [x, y, x + w, y + h]
return estimated_box
示例5: extract_color
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [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
示例6: onmouse
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def onmouse(event, x, y, flags, param):
global drag_start, sel
if event == cv2.EVENT_LBUTTONDOWN:
drag_start = x, y
sel = 0,0,0,0
elif event == cv2.EVENT_LBUTTONUP:
if sel[2] > sel[0] and sel[3] > sel[1]:
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]
result = cv2.matchTemplate(gray,patch,cv2.TM_CCOEFF_NORMED)
result = np.abs(result)**3
val, result = cv2.threshold(result, 0.01, 0, cv2.THRESH_TOZERO)
result8 = cv2.normalize(result,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
cv2.imshow("result", result8)
drag_start = None
elif drag_start:
#print flags
if flags & cv2.EVENT_FLAG_LBUTTON:
minpos = min(drag_start[0], x), min(drag_start[1], y)
maxpos = max(drag_start[0], x), max(drag_start[1], y)
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
cv2.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
cv2.imshow("gray", img)
else:
print("selection is complete")
drag_start = None
示例7: template_match
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def template_match(source_image, template_image, region_center, option=0):
""" template match
@param source_image: np.array(input source image)
@param template_image: np.array(input template image)
@param region_center: list(if not None, it means source_image is
part of origin target image, otherwise, it is origin target image)
@param option: int(if it is not zero, source_image and template_image will
be global thresholding)
@return max_val: float(the max match value)
@return [x,y]: list(the best match position)
"""
template_width = template_image.shape[1]
template_height = template_image.shape[0]
[source_width,source_height] = [source_image.shape[1],source_image.shape[0]]
width = source_width - template_width + 1
height = source_height - template_height + 1
if width < 1 or height < 1: return None
if option == 0:
[s_thresh, t_thresh] = [source_image, template_image]
else:
s_ret,s_thresh = cv2.threshold(source_image,200,255,cv2.THRESH_TOZERO)
t_ret,t_thresh = cv2.threshold(template_image,200,255,cv2.THRESH_TOZERO)
'''template match'''
result = cv2.matchTemplate(s_thresh, t_thresh, cv2.cv.CV_TM_CCORR_NORMED)
(min_val, max_val, minloc, maxloc) = cv2.minMaxLoc(result)
if len(region_center):
x = int(maxloc[0]+region_center[0]-source_width/2)
y = int(maxloc[1]+region_center[1]-source_height/2)
else:
[x,y] = maxloc
return max_val, [x,y]
示例8: template_match
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def template_match(source_image, template_image, region_center, option=0):
""" template match
@param source_image: np.array(input source image)
@param template_image: np.array(input template image)
@param region_center: list(if not None, it means source_image is
part of origin target image, otherwise, it is origin target image)
@param option: int(if it is not zero, source_image and template_image will
be global thresholding)
@return max_val: float(the max match value)
@return [x,y]: list(the best match position)
"""
template_width = template_image.shape[1]
template_height = template_image.shape[0]
[source_width,source_height] = [source_image.shape[1],source_image.shape[0]]
width = source_width - template_width + 1
height = source_height - template_height + 1
if width < 1 or height < 1: return None
if option == 0:
[s_thresh, t_thresh] = [source_image, template_image]
else:
s_ret,s_thresh = cv2.threshold(source_image,200,255,cv2.THRESH_TOZERO)
t_ret,t_thresh = cv2.threshold(template_image,200,255,cv2.THRESH_TOZERO)
'''template match'''
result = cv2.matchTemplate(s_thresh, t_thresh, cv2.cv.CV_TM_CCORR_NORMED)
(min_val, max_val, minloc, maxloc) = cv2.minMaxLoc(result)
if len(region_center):
x = int(maxloc[0]+region_center[0]-source_width/2)
y = int(maxloc[1]+region_center[1]-source_height/2)
else:
[x,y] = maxloc
return max_val, [x,y]
#rotate template match
示例9: generate_template_gray_and_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def generate_template_gray_and_mask(self, watermark_template_filename):
"""
处理水印模板,生成对应的检索位图和掩码位图
检索位图
即处理后的灰度图,去除了非文字部分
:param watermark_template_filename: 水印模板图片文件名称
:return: x1, y1, x2, y2
"""
# 水印模板原图
img = cv2.imread(watermark_template_filename)
# 灰度图、掩码图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_TOZERO + cv2.THRESH_OTSU)
_, mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
mask = self.dilate(mask) # 使得掩码膨胀一圈,以免留下边缘没有被修复
#mask = self.dilate(mask) # 使得掩码膨胀一圈,以免留下边缘没有被修复
# 水印模板原图去除非文字部分
img = cv2.bitwise_and(img, img, mask=mask)
# 后面修图时需要用到三个通道
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
self.watermark_template_gray_img = gray
self.watermark_template_mask_img = mask
self.watermark_template_h = img.shape[0]
self.watermark_template_w = img.shape[1]
# cv2.imwrite('watermark-template-gray.jpg', gray)
# cv2.imwrite('watermark-template-mask.jpg', mask)
return gray, mask
示例10: mask_depth_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def mask_depth_image(depth_image, min_depth, max_depth):
""" mask out-of-range pixel to zero """
# print ('mask min max', min_depth, max_depth)
ret, depth_image = cv2.threshold(depth_image, min_depth, 100000, cv2.THRESH_TOZERO)
ret, depth_image = cv2.threshold(depth_image, max_depth, 100000, cv2.THRESH_TOZERO_INV)
depth_image = np.expand_dims(depth_image, 2)
return depth_image
示例11: onmouse
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def onmouse(event, x, y, flags, param):
global drag_start, sel
if event == cv.EVENT_LBUTTONDOWN:
drag_start = x, y
sel = 0,0,0,0
elif event == cv.EVENT_LBUTTONUP:
if sel[2] > sel[0] and sel[3] > sel[1]:
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]
result = cv.matchTemplate(gray,patch,cv.TM_CCOEFF_NORMED)
result = np.abs(result)**3
val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)
result8 = cv.normalize(result,None,0,255,cv.NORM_MINMAX,cv.CV_8U)
cv.imshow("result", result8)
drag_start = None
elif drag_start:
#print flags
if flags & cv.EVENT_FLAG_LBUTTON:
minpos = min(drag_start[0], x), min(drag_start[1], y)
maxpos = max(drag_start[0], x), max(drag_start[1], y)
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
cv.imshow("gray", img)
else:
print "selection is complete"
drag_start = None
示例12: onmouse
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def onmouse(event, x, y, flags, param):
global drag_start, sel
if event == cv2.EVENT_LBUTTONDOWN:
drag_start = x, y
sel = 0,0,0,0
elif event == cv2.EVENT_LBUTTONUP:
if sel[2] > sel[0] and sel[3] > sel[1]:
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]
result = cv2.matchTemplate(gray,patch,cv2.TM_CCOEFF_NORMED)
result = np.abs(result)**3
val, result = cv2.threshold(result, 0.01, 0, cv2.THRESH_TOZERO)
result8 = cv2.normalize(result,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
cv2.imshow("result", result8)
drag_start = None
elif drag_start:
#print flags
if flags & cv2.EVENT_FLAG_LBUTTON:
minpos = min(drag_start[0], x), min(drag_start[1], y)
maxpos = max(drag_start[0], x), max(drag_start[1], y)
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
cv2.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
cv2.imshow("gray", img)
else:
print "selection is complete"
drag_start = None
示例13: _pharynx_orient
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import THRESH_TOZERO [as 别名]
def _pharynx_orient(worm_img, min_blob_area):#, min_dist_btw_peaks=5):
#%%
blur = cv2.GaussianBlur(worm_img,(5,5),0)
#ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_TOZERO+cv2.THRESH_OTSU)
th, worm_mask = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
worm_cnt, cnt_area = binaryMask2Contour(worm_mask, min_blob_area=min_blob_area)
worm_mask = np.zeros_like(worm_mask)
cv2.drawContours(worm_mask, [worm_cnt.astype(np.int32)], 0, 1, -1)
local_maxi = peak_local_max(blur,
indices=True,
labels=worm_mask)
#%%
markers = np.zeros_like(worm_mask, dtype=np.uint8)
kernel = np.ones((3,3),np.uint8)
for x in local_maxi:
markers[x[0], x[1]] = 1
markers = cv2.dilate(markers,kernel,iterations = 1)
markers = ndi.label(markers)[0]
#strel = ndi.generate_binary_structure(3, 3)
#markers = binary_dilation(markers, iterations=3)
labels = watershed(-blur, markers, mask=worm_mask)
props = regionprops(labels)
#sort coordinates by area (the larger area is the head)
props = sorted(props, key=lambda x: x.area, reverse=True)
peaks_dict = {labels[x[0], x[1]]:x[::-1] for x in local_maxi}
peaks_coords = np.array([peaks_dict[x.label] for x in props])
if DEBUG:
plt.figure()
plt.subplot(1,3,1)
plt.imshow(markers, cmap='gray', interpolation='none')
plt.subplot(1,3,2)
plt.imshow(labels)
plt.subplot(1,3,3)
plt.imshow(blur, cmap='gray', interpolation='none')
for x,y in peaks_coords:
plt.plot(x,y , 'or')
if len(props) != 2:
return np.full((2,2), np.nan) #invalid points return empty
#%%
return peaks_coords