yes.
program sketch:
import cv2 as cv
blur_radius = 0
def draw():
# option:
#blur_radius = cv.getTrackbarPos("blur_strength", "blur")
# then you won't need the global `blur_radius` and assignment in the callback below
blur = ... do blur using blur_radius ...
cv.imshow("blur", blur)
... do canny ...
... imshow the canny result ...
# absolutely NO waitKey here
def trackbar_callback(pos): # and maybe userdata, see docs
# using getTrackbarPos (above), these two lines need to be removed:
global blur_radius
blur_radius = pos
# always:
draw()
if __name__ == '__main__':
cv.namedWindow("blur")
cv.namedWindow("canny")
cv.createTrackbar("blur_strength", "blur", ..., onChange=trackbar_callback)
draw() # first call to show *something*
# main loop does nothing but waitKey
# waitKey does all event and callback processing
while True:
key = cv.waitKey(1000)
if key == -1:
continue # it was a timeout
elif key in (13, 27): # ENTER, ESC
break
else:
print("key code", key)
cv.destroyAllWindows()
for this simple case, you could pass the trackbar position into the draw() call… but that’s not extensible.
when you add more control elements, the state of those values needs to be kept somewhere. if you don’t, you would have to query the positions of all trackbars in the draw() call (see the “option”).