How to update imshow() window for Python OpenCV CV2

Question:

My current program will output an image to the user and based on user input, readjust the image as necessary.

Long story short, I am trying to find circular objects in an image file. I will be using the Hough Circle Transform. However, because many of my circles in the image are not “perfect circles”, I am doing an algorithm that “guesses” the radii of the circles. However, I want to allow the user to readjust the radii as necessary.

Is there some way to ask the user for input and then based on the user input, readjust the window in imshow()? Right now, imshow() refuses to show the actual window until I use cv2.waitKey(0), at which point I can’t ask for user input until the window is destroyed.

Asked By: Alwin Hui

||

Answers:

You can call imshow repeatedly without destroying it. And yes, you will likely need waitKey, just don’t call it with 0 or it will wait indefinitely. Call it with a 1 to wait just 1 millisecond and ensure the image redraws.

Try something like:

while True:
    cv2.imshow('image', img)
    cv2.waitKey(1)
    radius = input('Input radius')
    # recalculate image with new radius here...
Answered By: 101
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.