Correct_Historian
Correct_Historian

Reputation: 11

Problem displaying image opencv+python using imshow + waitKey

I have recently upgraded my python/opencv for a project to python 3.7 + opencv 4.3.0 and now I have an issue with opencvs imshow. I am running Ubuntu 18.04 and am using conda venvs.

I tried to rerun this piece of code multiple times and half the time it correctly displays the white image, and half the time it displays the image below (1). The printed output (2) is always the same. I tried changing it from waitKey(0) to waitKey(1000) but that doesn't make a difference. Still about half the time a tiny black image is all I see.

Does anybody know how to debug this? I tried looking at Pycharms log files but they dont have any more details. I also tried running it straight from the command line but that gives the same issue. Also tried to remove the environment and created a fresh one, reinstalled opencv and got the same issues. When I create a 3.6 environment I don't have the issue, but that's no longer an option. I need python3.7 for some other packages that don't support certain features in 3.6.

I received a warning that libcanberra gtk was missing, and found that in another post that it could cause issues. So I installed it using sudo apt install libcanberra-gtk-module libcanberra-gtk3-module and the warning went away. Sadly the issue did not...

import numpy as np
import cv2
if __name__ == '__main__':
     img = np.ones((255, 255, 3), dtype=np.uint8)*255
     print(img.shape)
     print(img.dtype)
     print(img.min())
     print(img.max())
     cv2.imshow("i", img)
     cv2.waitKey(0)

screenshot of the code + result

console output

Upvotes: 1

Views: 475

Answers (2)

PrasoonNIELIT
PrasoonNIELIT

Reputation: 1

I solved it in this way:

if cv2.waitKey(0):
    pass

Upvotes: 0

Burak
Burak

Reputation: 2495

I think there is no problem. The left-upper part of the image is white as it is supposed to be. The rest is undefined. I recommend equating green and blue channels to zero and leaving only red channel as 255 in order to make sure that is the case.

Upvotes: 0

Related Questions