i use Opencv and Python and i try to create an trackbar for the thresholding
now i want when i close the Window of trackbar the function return to me the value of min threshold or the binary image because i'll use this image in contour detection
but i don't want to use this method (when i push the button ESC like:ch = cv2.waitKey(27)
if ch == 27 or ch == 0x10001b:
break
i want sut push the red button of exit this enter image description here
my code :
#-*- 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()