0

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()
  • Could you clarify your initial description of what you actually need? The code is simple and easy to read, but not the problem. I have added a solution per to my current understanding, but if I have understood question incorrectly, lemme know. – saurabheights Mar 15 '17 at 20:29
  • @saurabheights i want to get the value of thresholding then i will use this image in detection of contour – ouamane zahra Mar 15 '17 at 21:05
  • Yes, I can see that from reading the code. Please read your question(not the code part, but the initial problem). Write it in proper English, it's not clear what the problem is. – saurabheights Mar 15 '17 at 21:54
  • like this it's okey ? – ouamane zahra Mar 15 '17 at 22:40
  • I think it's more interpretable. I think you want to close the trackbar window and store the result in a variable to process after your while loop. But what is the issue with using the escape key? Note that, waitKey is more of a helper function to allow GUI to respond and thus is needed. And storing threshold value is simple, just read the Trackbar position after while loop ends and before destroyAllWindows. – saurabheights Mar 15 '17 at 22:49
  • If you still want to detect whether the window is closed, try this:- http://stackoverflow.com/questions/35003476/opencv-python-how-to-detect-if-a-window-is-closed, but I don't see the hate towards escape key, poor she/he. – saurabheights Mar 15 '17 at 22:49
  • thanks @saurabheights that what i need ^^ – ouamane zahra Mar 17 '17 at 00:08

0 Answers0