DanGoodrick
DanGoodrick

Reputation: 3198

How do can I use OpenCV imshow to display the most recent image in a loop without keyboard feedback

I am running an image processing loop with OpenCV in Python and I would like to display the most recent image mask in my imshow window. So whenever the loop calculates a new mask, it updates the imshow window (at about 6Hz). However, I can't get imshow to return control without waiting for a keyboard interrupt. Any suggestions? Is there a better library to use for this?

Upvotes: 2

Views: 13095

Answers (2)

GPPK
GPPK

Reputation: 6666

Without code this is a guess. BUT!

I imagine you are currently using cv2.waitKey to wait until there is a keyboard input:

cv2.waitKey(33)
if k==27:    # Esc key to stop
    break

What you need to do is use cv2.waitKey to wait a set amount of time, say 1 ms.

# Wait 1 milliseconds. Specifying 0 means forever, so we don't want that
cv2.waitKey(1)

Upvotes: 3

Mark Corrigan
Mark Corrigan

Reputation: 544

What you could do is log the image to a folder in the loop's directory if it's not totally essential for the image to be displayed in real time.

cv2.imwrite('image_logs/image_' + str(image_count) + '.jpeg', image)

For instance. Keeping track of images can easily be done with a counter.

You could also use waitkey - which will delay for the number of milliseconds in the parenthesis. Sometimes I have problems with this though (I use an rpi, quite slow!) so I tend to go for the logging option.

cv2.waitKey(50) #wait for 50ms

Upvotes: 1

Related Questions