Opencv imshow() freezes when updating

user3085931 picture user3085931 · May 4, 2016 · Viewed 17.6k times · Source

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)

Answer

Dharma picture Dharma · Sep 28, 2017

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.