Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv imshow() freezes when updating

For my image processing algorithm I'm using python / OpenCV. The output of my algorithm shall be updated im the same window over and over again.

However sometimes the window freezes and doesn't update at all, but the algorithm is still running and updated the picture a multiple times in the meantime. The window turns dark gray on this Ubuntu machine.

Here is an excerpt of the involved code:

for i in range(0,1000):
    img = loadNextImg()
    procImg = processImg(img)
    cv2.imshow("The result", procImg)
    cv2.waitKey(1)

N.B.: processImg() takes about 1-2 s for its procedures. The line cv2.imshow(procImg) creates the window in first instance (i.e. there is no preceding invocation)

like image 213
user3085931 Avatar asked May 04 '16 21:05

user3085931


People also ask

What is cv2 Imshow ()?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size. Syntax: cv2.imshow(window_name, image)

Why open CV is not working?

You can try the following: Download and install the latest version of python. Restart your terminal. Create a new virtualEnv and install dependencies, check the script below.

How do I close my cv2 Imshow?

waitKey() and cv2. destroyAllWindows(). Inside the cv2. waitKey() function, you can provide any value to close the image and continue with further lines of code.

What is the first argument in a call to cv2 Imshow?

Then, the image is shown using a call to the cv::imshow function. The first argument is the title of the window and the second argument is the cv::Mat object that will be shown.


2 Answers

My suggestion is to use Matplotlib pyplot for displaying the image. I do it the following way.

import matplotlib.pyplot as plt
# load image using cv2....and do processing.
plt.imshow(cv2.cvtColor(image, cv2.BGR2RGB))
# as opencv loads in BGR format by default, we want to show it in RGB.
plt.show()

I know it does not solve the problem of cv2.imshow, but it solves our problem.

like image 193
Dharma Avatar answered Sep 20 '22 22:09

Dharma


Increasing the wait time solves this issue. However in my opinion this is unnecessary time spent on sleeping (20 ms / frame), even though it's not much.

Changing

cv2.waitKey(1)

to

cv2.waitKey(20)

prevents the window from freezing in my case. The duration of this required waiting time may vary on different machines.

like image 39
user3085931 Avatar answered Sep 22 '22 22:09

user3085931