Ask Your Question

cv2 imshow displaying black images sometimes

asked 2020-07-11 15:21:09 -0600

cv-newbie gravatar image

updated 2020-07-12 03:33:50 -0600

supra56 gravatar image

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

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

Sometimes I would get the lovely picture of the original butterfly image, butterfly

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. Any help is appreciated, thanks!

edit retag flag offensive close merge delete


Add Index zero to cv2.waitKey(0)

Edit: I tested it on raspberry pi using linux w/out index. Actually, I don't know If this cv2.waitKey(0) work on UBUNTU

supra56 gravatar imagesupra56 ( 2020-07-12 03:34:52 -0600 )edit

I haven't seen this before, you can try to run the following script under Windows, using IDE can help you see the cause of the error. Maybe it would be better if you read the image using absolute path.Like this:

import cv2
img = cv2.imread('D:/CODE/python/opencv/lena.jpg')
cv2.imshow('ImageWindow', img)
x1290148 gravatar imagex1290148 ( 2020-07-17 05:38:21 -0600 )edit

Having the same trouble. It seems to display correct about 50% of the time. Using named windows doesn't seem to help. I know the image is being read correctly as it plots fine using matplotlib.

ETA: I went back a couple of revisions with the opencv library and it works fine now. I am using Kubuntu 20.04. Doesn't seem to be a widespread problem.

sam556 gravatar imagesam556 ( 2020-07-25 13:55:56 -0600 )edit

I am having the same problem, can you please guide me how you fixed that?

Dipansh gravatar imageDipansh ( 2020-07-30 08:53:09 -0600 )edit

I'm having the exact same problem... my machine is Ubuntu 18.04.4, using python 3.6.9, opencv version 4.3.0

eddie gravatar imageeddie ( 2020-08-03 02:25:42 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2020-08-06 01:57:26 -0600

I installed opencv-python- and the problem seems to have went away. I was using version when having the issues.

edit flag offensive delete link more


I was also having issues with this exact opencv version.. imshow behavior was flaky. Bumping it to 4.4 fixed it.

bergercookie gravatar imagebergercookie ( 2020-09-18 02:23:30 -0600 )edit

answered 2020-09-18 08:49:25 -0600

I had the same problem, after upgrading to 4.4.0 the black box dissapeared

pip3 install python-opencv -U

edit flag offensive delete link more

Question Tools



Asked: 2020-07-11 15:21:09 -0600

Seen: 13,901 times

Last updated: Jul 12 '20