Skip to content

cv2.imshow() not working consistently #362

Closed

Description

@GokulNarayan

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

Activity

  1. skvark commented on Jul 16, 2020

    @skvark
    Member

    Yeah, probably same issue as here: #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.

  2. skvark commented on Jul 18, 2020

    @skvark
    Member

    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 (#309). Auditwheel might as well cause the issue, it bundles the dependencies into the wheels and it might be doing something wrong.

  3. skvark commented on Jul 18, 2020

    @skvark
    Member

    How to avoid this issue until it's solved:

  4. skvark commented on Jul 20, 2020

    @skvark
    Member

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

  5. skvark commented on Jul 21, 2020

    @skvark
    Member

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

  6. skvark commented on Jul 29, 2020

    @skvark
    Member

    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...

  7. skvark commented on Aug 6, 2020

    @skvark
    Member

    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.

  8. skvark commented on Aug 9, 2020

    @skvark
    Member

    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.

  9. ParthikB commented on Aug 12, 2020

    @ParthikB

    What's the solution finally?

  10. skvark commented on Aug 12, 2020

    @skvark
    Member

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

  11. SiddhantKumar14 commented on Sep 15, 2020

    @SiddhantKumar14

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

  12. skvark commented on Sep 15, 2020

    @skvark
    Member

    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.

  13. SiddhantKumar14 commented on Sep 15, 2020

    @SiddhantKumar14

    upgrading to 4.4.0 fixed it, thank you!

  14. ahmad-sulehari commented on Oct 6, 2020

    @ahmad-sulehari

    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)

  15. ganler commented on Nov 15, 2020

    @ganler

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

  16. ikram-hamizi commented on Apr 25, 2021

    @ikram-hamizi

    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: #Yuliang-Liu/Curve-Text-Detector#11 (comment))

  17. matanox commented on May 24, 2021

    @matanox

    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).

  18. skvark commented on May 24, 2021

    @skvark
    Member

    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.

  19. matanox commented on May 24, 2021

    @matanox

    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?

  20. skvark commented on May 24, 2021

    @skvark
    Member

    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

  21. matanox commented on May 25, 2021

    @matanox

    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.

  22. pk16514 commented on Jul 29, 2021

    @pk16514

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

  23. leosongwei commented on Sep 2, 2021

    @leosongwei

    got the same problem on 4.5.3.56, OS is Debian 11

  24. vocdex commented on Apr 21, 2022

    @vocdex

    Using the latest OpenCV at the moment(4.5.5.64) Ubuntu 18.04 and Python 3.8, but this behaviour is still happening. Any suggestions?

  25. angeliquemccl commented on Jul 14, 2022

    @angeliquemccl

    If it everything is update per the previous comments. I found out it could be the image size, try using:

    def rescaleFram(frame, scale=0.75):

    14.23 seconds in the video

    https://www.youtube.com/watch?v=oXlwWbU8l2o

  26. apolloo43 commented on Mar 24, 2024

    @apolloo43

    it same as me.. i using python 3.8 , ubuntu 20.04 and opencv version 4.8.0.76 and same problem happen.. are there any solution???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions