Opencv-python: cv2.imshow() not working consistently

Created on 16 Jul 2020  路  24Comments  路  Source: opencv/opencv-python

Expected behaviour

imshow displays image

Actual behaviour

when calling cv2.imshow(), I mostly get a small window with the correct name but containing only a black screen. It does work sometimes however but it is unpredictable. This issue was not encountered in previous versions of opencv-contrib-python.
error

Steps to reproduce

  • example code

import cv2
import os
import imutils

image = cv2.imread("ref_image.png")

cv2.imshow("test", image)
cv2.waitKey(0)

cv2.destroyAllWindows()

  • operating system : Ubuntu 20.04
  • architecture (e.g. x86): x64
  • python version: 3.8
  • opencv-python version: opencv-contrib-python 4.3.36

Most helpful comment

I patched auditwheel and got Qt 5.15 working. Qt is also built in a namespace so it should not conflict with other systems Qt installations in the future. There is still some font related finetuning to do but it's far more easier to fix than the segmentation fault issue.

All 24 comments

Yeah, probably same issue as here: https://github.com/skvark/opencv-python/issues/356

I have no idea what causes that behavior. Qt4 version is the same, only the base image where the wheels and Qt4 are built was changed from manylinux1 to manylinux2014.

@skvark Do you know any workaround to solve this? it happens on any version of opencv-contrib-python using ubuntu 20

I had a look at the linked OpenCV issue and it seems that this issue is related to Qt conflicts. There is no proper workaround for the latest wheels (see next message for some options). Best option would be to bundle Qt5 to the wheels (Qt4 is very old and does not behave well with other global Qt system installations) but there were major issues when I tried that during the manylinux version upgrade (https://github.com/skvark/opencv-python/issues/309). Auditwheel might as well cause the issue, it bundles the dependencies into the wheels and it might be doing something wrong.

How to avoid this issue until it's solved:

  • downgrade to the previous release which uses manylinux1 (4.2.0 and 3.4.9)
  • use some proper GUI framework to display the images such as PyQt5
  • build OpenCV wheel locally and use it, instructions are in the readme: https://github.com/skvark/opencv-python#manual-builds

I will rebuild the manylinux2014 images and check if that fixes this issue, auditwheel has had some updates since the last build.

That didn't help, I'll need to probably start looking into Qt5 again...

Looks like I got the Qt5 builds working (at least Travis tests are passing), but I need to still check that GUI functions actually work.

Edit: still segfaulting...

I patched auditwheel and got Qt 5.15 working. Qt is also built in a namespace so it should not conflict with other systems Qt installations in the future. There is still some font related finetuning to do but it's far more easier to fix than the segmentation fault issue.

This issue should be now fixed. Fixed wheels (3.4.10.37 and 4.3.0.38) will be soon up in PyPI. It will take probably about ~24 hours for all builds to finish.

Please open a new issue if there are still problems related to GUI functions.

What's the solution finally?

Solution to this issue? Upgrade your opencv-python to version 4.3.0.38 or greater.

upgrading to 4.3.0.38 did not work, still facing the same issue

Please post exact install steps and how you are using opencv-python and please upgrade to 4.4.0. This issue has been fixed, the issue is very likely in your environment.

upgrading to 4.4.0 fixed it, thank you!

I am using the latest conda version for Debian on parrot OS security(updated to latest version)
I'm running my script in a venv with python3.7, qt5.9, OpenCV-python4.4.0.44
Screenshot spider cv output at 2020-10-03 13-12-00
with this: QObject::moveToThread: Current thread (0x55e37f4716b0) is not the object's thread (0x55e37f6733f0).
Cannot move to target thread (0x55e37f4716b0)

OpenCV 4.4 worked to show the image. But the warning is still there.

I am using the latest conda version for Debian on parrot OS security(updated to latest version)
I'm running my script in a venv with python3.7, qt5.9, OpenCV-python4.4.0.44
Screenshot spider cv output at 2020-10-03 13-12-00
with this: QObject::moveToThread: Current thread (0x55e37f4716b0) is not the object's thread (0x55e37f6733f0).
Cannot move to target thread (0x55e37f4716b0)

@ahmad-sulehari Upgrading to 38+ resulted in the same issue as you.
To solve the QObject::moveToThread: problem, only opencv==4.3.0.36 worked for me (following the comment here: #https://github.com/Yuliang-Liu/Curve-Text-Detector/issues/11#issuecomment-762640216)

With 4.3.0.36, and only that version, I no longer get the original warning, but multiple windows open up when reusing imshow(), which was not the case before. Whereas installing 4.4.0.42 reverts back to the original problem of showing that warning message about thread switching that is mentioned above. This seems to be a known issue as described here. So I will really try to get to fix the underlying qt library conflict somehow.

(One thing I do notice is that it's easy to end up with an _opencv-contrib-python_ package that is not the same version as _opencv-python_ itself, which may add to some people's confusion in fiddling version installs).

With 4.3.0.36, and only that version, I no longer get the original warning, but multiple windows open up when reusing imshow(), which was not the case before. Whereas installing 4.4.0.42 reverts back to the original problem of showing that warning message about thread switching that is mentioned above. This seems to be a known issue as described here. So I will really try to get to fix the underlying qt library conflict somehow.

4.3.0.36 is known to have this broken behavior. These packages ship with their own copy of Qt. The Qt issues are always related to some other Qt installation on your system. Unfortunately, the only way to avoid conflicts between other Qt installations completely on Linux is to build this package from sources against your system Qt.

(One thing I do notice is that it's easy to end up with an _opencv-contrib-python_ package that is not the same version as _opencv-python_ itself, which may add to some people's confusion in fiddling version installs).

You should never install both opencv-python and opencv-contrib-python. This will break your environment and may as well cause issues you are seeing. Follow the install instructions in the README and install only one package.

Thanks a lot. Would you happen to recall how to build OpenCV from source in a way that gets picked up within a conda environment?

I don't know about conda, these packages are for PyPI and pip. If you use pip, follow the manual build instructions in the README. Conda has also their own OpenCV packages here: https://github.com/conda-forge/opencv-feedstock

In case it ever helps anyone for that qt compatibility situation, here's how to manage building from source for a conda environment, which I didn't see documented anywhere else.

Use pip install opencv-python==4.2.0.34 . It worked for me. No warning and working consistently without any black image.

got the same problem on 4.5.3.56, OS is Debian 11

Was this page helpful?
0 / 5 - 0 ratings