我使用Opencv和Python,我尝试为阈值创建一个轨迹栏
现在我想当我关闭轨迹栏的窗口时,函数会返回最小阈值或二进制图像的值,因为我会在轮廓检测中使用此图像
但我不想使用这种方法(当我按下按钮ESC时:ch = cv2.waitKey(27)
if ch == 27 or ch == 0x10001b:
break
我希望sut按下退出的红色按钮enter image description here
我的代码:
#-*- coding: utf-8 -*-
import numpy as np
import cv2
# cv2.threshold(src, thresh, maxval, type[, dst])
Image = cv2.imread("114.png")
cv2.namedWindow("Main", cv2.WINDOW_NORMAL)
cv2.imshow("Main", Image)
# Extraction of Blue channel
b = Image[:,:,0]
cv2.namedWindow("Only Blue Channel", cv2.WINDOW_NORMAL)
cv2.imshow("Only Blue Channel", b)
# Callback Function for Trackbar (but do not any work)
def nothing(*arg):
pass
# Generate trackbar Window Name
TrackbarName = "Trackbar"
# Make Window and Trackbar
cv2.namedWindow("window", cv2.WINDOW_NORMAL)
cv2.createTrackbar(TrackbarName, "window", 0, 250, nothing)
img_threshed = np.zeros(b.shape, np.uint8)
while True:
# Get kernel size in trackbar
TrackbarPos = cv2.getTrackbarPos(TrackbarName, "window")
# Apply dilation
limit = TrackbarPos
ret,img_threshed = cv2.threshold(b,limit,255,cv2.THRESH_BINARY)
# Show in window
cv2.imshow("window", img_threshed)
# Expanding borders of the objects
kernel = np.ones((9, 9),np.uint8)
img_dilated = cv2.dilate(img_threshed, kernel)
cv2.namedWindow("Dilated Blue Channel", cv2.WINDOW_NORMAL)
cv2.imshow("Dilated Blue Channel", img_dilated)
# Retrieving contours by subtraction base objects from the expanded objects
img_contours = img_dilated - img_threshed
cv2.namedWindow("Contours", cv2.WINDOW_NORMAL)
cv2.imshow("Contours", img_contours)
cv2.destroyAllWindows()