Strange cv2 behavior with imshow

TechPerson

I'm using Python 3.8.2 on Kubuntu 20.04.

cv2 is the latest available version, as are NumPy and pip:

Requirement already up-to-date: pip in /usr/local/lib/python3.8/dist-packages (20.1.1)
Requirement already up-to-date: opencv-contrib-python in /usr/local/lib/python3.8/dist-packages (4.3.0.36)
Requirement already satisfied, skipping upgrade: numpy>=1.17.3 in /usr/local/lib/python3.8/dist-packages (from opencv-contrib-python) (1.19.1)
Requirement already up-to-date: numpy in /usr/local/lib/python3.8/dist-packages (1.19.1)

Here is my code:

import numpy as np
import cv2

black_image = np.zeros((480, 640, 3))
cv2.imshow("Test", black_image)
cv2.waitKey()

white_image = np.ones((480, 640, 3))
cv2.imshow("Test", white_image)
cv2.waitKey()

This simple test program, when run in IDLE, sometimes displays a small black image (95x25) instead of the 640x480 black image I passed to imshow. Other times, it will display the proper image. In either case, when this window is closed, OpenCV blocks forever instead of displaying the second image and I have to close the process via the IDLE shell window.

When run in the terminal, the results are the same: sometimes a weird small image of size 95x25, and OpenCV blocks before opening the second window resulting in me having to kill the process (Ctrl+C does nothing, and sending SIGTERM doesn't work either).

I have tried:

Passing 0 to cv2.imshow(). Same results.

Passing 1 to cv2.imshow(). This actually displays the white image as intended, but the black image (or its weird 95x25 substitute) is also displayed. As I understand it, cv2.imshow() is supposed to replace the contents of the already opened window if one exists with the same name.

I remember OpenCV working fine with Python 3.7 and below, although I can't test with that as those versions are no longer installable on my version of Ubuntu.

Both results are reproducible on my machine, but no code change is necessary to switch between them. What's going on here, and how do I fix it?

EDIT: Also tried closing windows (by name and through cv2.destroyAllWindows()) and waiting for specific key presses. The main problem persists: images are not being displayed consistently and OpenCV will sometimes hang forever. Normal programs and other code I've pasted do not work properly.

EDIT 2: Now attached are screenshots of my problem

The first weird window: The first weird window

IDLE hanging with no OpenCV window open after closing the first one: IDLE hanging with no OpenCV window open after closing the first one

Konsole (my terminal emulator) also hanging after closing the first window: Konsole (my terminal emulator) also hanging after closing the first window

Ctrl-C not working to end Python in the terminal: Ctrl-C doesn't stop the process

I can also provide video proof if needed.

TechPerson

After an updated version of Python (3.8.5 specifically) was released, I updated Python, pip, and PyOpenCV. Everything works as intended now, confirming my suspicions that this was an obscure bug not directly caused by my code.

Updated packages:

Requirement already satisfied: pip in ./.local/lib/python3.8/site-packages (20.3.3)
Requirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.8/dist-packages (4.4.0.46)
Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.8/dist-packages (from opencv-contrib-python) (1.19.1)

Maybe this can help someone with outdated packages experiencing the same issues in the future.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related