OpenCV cv2.imshow displaying black images sometimes

Viewed 1559

I am trying out OpenCV on ubuntu 20.04, python 3.7. I have run the following script

import cv2
img = cv2.imread('butterfly.jpg')
cv2.imshow('ImageWindow', img)
cv2.waitKey()

Sometimes I would get the lovely picture of the original butterfly image, but sometimes I would get a small black window.

The behavior is a bit random, and I am not sure what is causing this issue.

2 Answers

I had the same issue in opencv version 4.3.0. The fix is to upgrade to a newer version (in my case 4.4.0):

pip install --upgrade opencv-python

You can try to do

cv2.waitKey(0)

worked for me.

Related