opencv-python: opencv GUI does not work

cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE) results in an error:

QObject::moveToThread: Current thread (0xca9110) is not the object's thread (0x1256140).
Cannot move to target thread (0xca9110)

here is the version:

cv2.__version__
Out[3]: '3.3.0'
QtCore.QT_VERSION_STR
Out[7]: '5.6.2'

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 41
  • Comments: 91 (28 by maintainers)

Commits related to this issue

Most upvoted comments

In my case the Qt conflicted with the Qt of the system due to a KDE plugin.

This is how I found it:

export LD_DEBUG=all This allowed me to see all the libraries that are used.

python cvFail.py 2> output.txt I reproduce the problem and capture all the errors (which thanks to LD_DEBUG contain all library dependencies)

unset LD_DEBUG No need to the debug anymore

grep libQt output.txt Found hundreds of references to Qt, but the more interesting was that two different versions appeared: libQtCore-3dbacd8a.so.4.8.7 (from the virtual environment) and libQtCore.so.4 (from the system)

Using less output.txt search for libQtCore.so.4 to know which other library was loading it. I found that was breeze.so, so I search which other library was loading it and it turn out to be libQtCore-3dbacd8a.so.4.8.7.

Conclusion: The OpenCV distributed Qt tries to load breeze.so and this library then loads the system Qt introducing an inconsistency and making the application crash.

Lets find who install breeze.so dpkg -S /usr/lib/kde4/plugins/styles/breeze.so It seems to be a library from a kde style plugin (kde-style-breeze-qt4), so I remove this plugin:

apt-get remove kde-style-breeze-qt4 And my problem was solved! 😃

I hope this helps someone.

I didn’t have this issue at first, then cv2.namedWindow just stopped working for me without me making any system changes. I noticed in dmesg the following: python[29883]: segfault at 0 ip 00007fa8c3c114be sp 00007ffcf5c34740 error 4 in libxcb.so.1.1.0[7fa8c3c07000+21000]

After a quick Google, I found this thread, which recommended removing the libqt5x11extras5 package. I just removed that (and the 121 dependent packages), and it’s working properly again. So like @skvark mentioned, it’s probably due to Qt conflicts.

First, uninstall any versions of OpenCV you may have installed. If you installed using pip:

sudo pip uninstall opencv-python Next, try installing OpenCV using your Linux distro’s package manager. For Ubuntu/Debian, this is:

sudo apt-get install libopencv-dev python-opencv

copy from https://reformatcode.com/code/python/how-to-fix-the-error-quotqobjectmovetothreadquot-in-opencv-in-python

I advise strongly against removing libqt5x11extras5 unless you know exactly what you are doing. It killed my lightdm display manager, and, sadly, didn’t fix the issue for me.

If you have this issue: try to remove libqt5x11extras5 package (see @coxbrandon’s message above) and please report here if that solution works for you. Note: do this at your own risk.

GUI problem with opencv 3.4.0 and ubuntu 16.04 - python 2.7

I’ll try to rebuild the Docker images during next weekend and publish a new release after that.

After some googling with different keywords I found this: https://stackoverflow.com/a/42955557/5795428

It indeed seems that the Qt 4.8.7 which ships with opencv-python somehow conflicts with the system (or some other) Qt installation. I can try to rebuild the Docker images and Qt in them with -qtnamespace Qt flag which should prevent symbol clashes.

Hi folks,

I received the same error when working within a virtual environment in Python 3 and opencv 3.3 The following simple steps solved my issue. Hope it helps.

  1. sudo apt-get autoremove
  2. sudo apt-get autoclean
  3. sudo apt-get update

The precompiled opencv-python packages generated by this repository will not work properly in some GNU/Linux systems. Some solutions:

  • If you need GUI support, build OpenCV python bindings yourself and link the library against your system Qt.
  • If you need GUI support but do not wish to build opencv-python yourself, use opencv-python-headless and install some other Python library which you can use to display images.
  • If you don’t need GUI support, just install opencv-python-headless.

I guess the problem is that Ubuntu 16.04 opencv libraries are 2.4.9.1 and that doesn’t work with opencv-python 3.x. I’ve got the same problem here - the apt-installed opencv-python 2.4.9.1 works fine but pip-installed opencv-python 3.4.0 fails with the very same program (simple imread()/imshow()) with:

QObject::moveToThread: Current thread (0x1d7c6c0) is not the object's thread (0x228e340).
Cannot move to target thread (0x1d7c6c0)
Segmentation fault

Unfortunately opencv-python 2.4.9 doesn’t support Python3 as far as I know so I’m stuck with Python2 for now.

So I just had the same problem and fixed it for my system (ubuntu 16.04).

I checked in my logs recent installations that contained dependencies with libraries that use Qt vim /var/log/apt/history.log

Looking in that file I saw that I recently installed okular and that okular installed libraries that depend on Qt. I removed okular and everything seems to be working now.

sudo apt-get remove okular
sudo apt-get autoremove

I have the same problem with opencv-python 3.4.0.12 (installed via pip) python 3.5.2, ubuntu 16.04.4 LTS, whenever I use cv2.namedWindow or cv2.imshow. No virtualenv, nothing fancy is needed:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.namedWindow('test')
QObject::moveToThread: Current thread (0x1aee720) is not the object's thread (0x1fd1f30).
Cannot move to target thread (0x1aee720)

Segmentation fault (core dumped)

Same is true for opencv-contrib-python.

My problem was caused by QT. before I install QT, my opencv was fine. I uninstalled QT but it did not help. I finally was able to make it work through ā€œsudo apt-get autoremove libqt5x11extras5ā€.

@chutongz libvpx is a dependency of FFmpeg, it’s not related to this issue.

I will repeat myself:

The root of this issue is that opencv-python packages ship with their own Qt build and on some Linux systems there’s a system Qt installation which does not play along with the one supplied with opencv-python.

To my understanding, the only way to avoid this issue completely is to use namespaced Qt build (https://wiki.qt.io/Qt_In_Namespace). It would require modifications to OpenCV GUI code to use such build of Qt. This is not practical and cannot be done in this repository. This is why the issue has been marked with a tag ā€œwontfixā€.

One thing you could try is to check the link order of the dependencies (I don’t know if this works):

export LD_DEBUG=all
python you_test_script.py

If that works, it will print the libs which the linker is accessing. See this for more info: http://www.bnikolic.co.uk/blog/linux-ld-debug.html

If you system Qt appears / is linked before the one supplied with opencv-python then the issue could be probably fixed by changing linker search order.

New Docker images are now up. I’ll publish a new release during next week.

EDIT: Requires more work, the new Qt build is not linked properly…

@coxbrandon thank you very much! Same problem, that solution worked like a charm. Thanks a lot! (I also had that number of dependent packages - on Linux Mint).

@caixiaofan This simply lets you use the opencv binding shipped in apt-get and stop using this repo. IMHO issues in this repo should focus on how to solve problems in this library.

Thanks @NikkoWei. As I don’t want to break my KDE DE I followed your instruction. test code runs perfectly.

It’s risky to remove libqt5x11extras5 or kde-style-breeze-qt4 for environment which need the modules. In my case, I work on Ubuntu with KDE. So libqt5x11extras5 or kde-style-breeze-qt4 are important for my desktop environment.

I found there are Qt conflicts between those in opencv-python package and those in system path. The steps to find the problem are the same as @areche:

  1. Display the library loading problems
    export LD_DEBUG=all
  1. Edit script to cause the problem
# fail.py
import cv2
im = cv2.imread('img.jpg')
cv2.imshow('image',im)
cv2.waitKey(0)
  1. Export the library loading information
    python fail.py 2>output.txt
  1. I found there are two version of Qt are used by loader in output.txt:
    ...

     10836:	symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_;  lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtGui-903938cd.so.4.8.7 [0]
     10836:	symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_;  lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtTest-1183da5d.so.4.8.7 [0]
     10836:	symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_;  lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtCore-6df570dd.so.4.8.7 [0]

     ...

     10836:	symbol=_ZTI14QWindowSurface;  lookup in file=/usr/lib/x86_64-linux-gnu/libQtDBus.so.4 [0]
     10836:	symbol=_ZTI14QWindowSurface;  lookup in file=/usr/lib/x86_64-linux-gnu/libQtCore.so.4 [0]
     10836:	symbol=_ZTI14QWindowSurface;  lookup in file=/usr/lib/x86_64-linux-gnu/libQtGui.so.4 [0]

     ...

As you see, there are two version Qtlib are used: 4.8.7 in the python packages path(I use conda environment) and 4 in the system path. So it’s probably the Qt conflicts.

There are 2 ways to solve this problem:

  1. Rebuild opencv by source to link Qtlib in system path. After make -j8, move the python library of opencv to overwrite the library in your python packages environment

    cp /path/to/build/lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so /home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/cv2.cpython-36m-x86_64-linux-gnu.so
    
  2. Just replace Qtlib in the python packages by Qtlib in system (according to output.txt)

    cp /usr/lib/x86_64-linux-gnu/libQtGui.so.4 ./libQtGui-903938cd.so.4.8.7
    
    cp /usr/lib/x86_64-linux-gnu/libQtCore.so.4 ./libQtCore-6df570dd.so.4.8.7 
    

    I can’t find libQtTest in my system path but it make no difference.

The 2 solution are OK for my case.

In my case, I did almost all the solutions:

  • installing using pip3 install opencv-python
  • installing using apt install python-opencv
  • I followed @areche solution, I end up with kdelibs5-plugins which was an important module for most of my desktop environments, so I give up on uninstalling it.

Finally, I found a solution for my case

  • pip uninstall opencv-python on all instances of my python and virtual environments
  • pip3 uninstall opencv-python on all instances of my python and virtual environments
  • sudo apt remove python-opencv

then I downloaded the most recent opencv, in my case : opencv-4.1.1 , I build it. for building :

  • mkdir build; cd build; ccmake .. then I pressed pressed c, again pressed c, then pressed g to generate make files, then
  • sudo make -j8 after successful building, I copied build\lib\python3\cv2.cpython.xxxxxx.so to usr/local/lib/python3.5/dist-packages/ and renamed it to cv2.so

Do the same for each virtualenv that you have. ( I’m not sure if it is logical, but it worked for me).

then everything worked fine without any error.

For those having this issue in Anaconda installation, just pip uninstall your opencv-python , and install opencv using conda :

conda install -c conda-forge opencv

that should fix your problems.

thanks for your reply about this issue, @skvark, @chutongz, I meet the same error when using opencv-python:QObject::moveToThread: Current thread (0x22e7370) is not the object’s thread (0x238fed0). Cannot move to target thread (0x22e7370)

and I reinstall opencv-python under conda environment, now i can not even import cv2 under anaconda environment. It makes an error like this: ImportError: libavcodec-7625dabe.so.58.6.103: cannot open shared object file: No such file or directory

/usr/local/lib/python3.5/dist-packages/cv2/.libs$ ls libavcodec-eac15e48.so.58.21.104 libQtTest-1183da5d.so.4.8.7 libavformat-b6bcbe33.so.58.17.101 libswresample-b4363bfa.so.3.2.100 libavutil-e1b1a17d.so.56.18.102 libswscale-15b3fdc6.so.5.2.100 libQtCore-9549151f.so.4.8.7 libvpx-81a43c0a.so.5.0.0 libQtGui-6d0f14dd.so.4.8.7 libz-a147dcb0.so.1.2.3

All this files do not exist, How can i fix it ? Thanks for your help

Just for the records: This does not help with the issue related to this package here, but you might solve your problem by installing OpenCV with Python bindings from source, as described here:

https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/

Solved it for me!

Most likely older Qt versions work better together with the Qt 4.8.7 which is bundled in the opencv-python wheels. The only real fix for this is the namespaced Qt build but unfortunately it’s impossible to use it since being compatible with it would require a rewrite of the OpenCV Qt GUI code.

I tried to initially enable GTK but it got too complicated / impossible due to the many dependencies GTK has. These all depedencies must be available or built on the CentOS 5 environment which in turn is used to build the wheels.

CentOS 5 is a very old release. The manylinux1 format requires that the wheels are built on that old version to be compatible with as many as possible GNU/Linux distributions. Manylinux2 (https://github.com/pypa/manylinux/pull/152) will update the requirement to CentOS 6.x, which will be hopefully easier to work with and makes it possible to either switch to Gtk or upgrade Qt to the most recent version.

In any case, it’s most likely to impossible to provide pre-built packages which satisfy the needs of all users on all different platforms. There’s always some compromise which has to be made considering the complexity of OpenCV.

If that is so, does that mean that OpenMV-python will never work with KDE, which is Qt-everything and turned out to be at the root of my problems with OpenMV? If that is the case, this should probably be mentioned somewhere predominantly as it would save lots of people lots of headaches.

On Monday, September 24, 2018 9:15:29 AM PDT Olli-Pekka Heinisuo wrote:

The status is the same as before: OpenCV does not support namespaced Qt builds -> this issue cannot be fixed: https://github.com/skvark/opencv-python/issues/46#issuecomment-404942996

I’m sorry but I’m still unable to replicate this issue. I took a fresh Ubuntu 16.04.3 Virtualbox image and run multiple different tests and even tried to deliberately break the installation. No errors.

Test script which I used:

import cv2
import numpy as np

print("first window 1")

cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE)
black = np.zeros((400, 400, 3), np.uint8)
cv2.imshow('test')
cv2.waitKey(0)

print("second window 2")

cv2.imshow('test', black)
cv2.waitKey(0)

print("third window 3")

cv2.imshow('test', black)
cv2.waitKey(0)

cv2.destroyAllWindows()

Dynamic linking is OK. The static library which ships with these wheels should look like below when inspected through readelf and ldd. RPATH entry should point to the .libs/ folder which should contain these files:

osboxes@osboxes:~/.local/lib/python3.5/site-packages/cv2/.libs$ ls
libavcodec-7625dabe.so.58.6.103   libQtCore-ba1dc80c.so.4.8.7  libswresample-81cb7b3e.so.3.0.101
libavformat-d3ca1b40.so.58.3.100  libQtGui-6d0f14dd.so.4.8.7   libswscale-bc8d848b.so.5.0.101
libavutil-eaec640f.so.56.5.100    libQtTest-1183da5d.so.4.8.7  libz-a147dcb0.so.1.2.3

Readelf:

osboxes@osboxes:~/Desktop$ readelf -d /home/osboxes/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so

Dynamic section at offset 0x15ea1c8 contains 40 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libz-a147dcb0.so.1.2.3]
 0x0000000000000001 (NEEDED)             Shared library: [libavcodec-7625dabe.so.58.6.103]
 0x0000000000000001 (NEEDED)             Shared library: [libavformat-d3ca1b40.so.58.3.100]
 0x0000000000000001 (NEEDED)             Shared library: [libavutil-eaec640f.so.56.5.100]
 0x0000000000000001 (NEEDED)             Shared library: [libswscale-bc8d848b.so.5.0.101]
 0x0000000000000001 (NEEDED)             Shared library: [libQtGui-6d0f14dd.so.4.8.7]
 0x0000000000000001 (NEEDED)             Shared library: [libQtTest-1183da5d.so.4.8.7]
 0x0000000000000001 (NEEDED)             Shared library: [libQtCore-ba1dc80c.so.4.8.7]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/.libs]

ldd:

osboxes@osboxes:~/Desktop$ ldd /home/osboxes/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so	
        linux-vdso.so.1 =>  (0x00007ffc0c7e1000)
	libz-a147dcb0.so.1.2.3 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libz-a147dcb0.so.1.2.3 (0x00007f77571f0000)
	libavcodec-7625dabe.so.58.6.103 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libavcodec-7625dabe.so.58.6.103 (0x00007f7755a6b000)
	libavformat-d3ca1b40.so.58.3.100 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libavformat-d3ca1b40.so.58.3.100 (0x00007f775562a000)
	libavutil-eaec640f.so.56.5.100 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libavutil-eaec640f.so.56.5.100 (0x00007f77553b5000)
	libswscale-bc8d848b.so.5.0.101 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libswscale-bc8d848b.so.5.0.101 (0x00007f775512b000)
	libQtGui-6d0f14dd.so.4.8.7 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libQtGui-6d0f14dd.so.4.8.7 (0x00007f775429a000)
	libQtTest-1183da5d.so.4.8.7 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libQtTest-1183da5d.so.4.8.7 (0x00007f775406d000)
	libQtCore-ba1dc80c.so.4.8.7 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libQtCore-ba1dc80c.so.4.8.7 (0x00007f7753b60000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7753942000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7753725000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f775351c000)
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f775319a000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7752e91000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7752c7a000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f77528b0000)
	/lib64/ld-linux-x86-64.so.2 (0x0000564f070bb000)
	libswresample-81cb7b3e.so.3.0.101 => /home/osboxes/.local/lib/python3.5/site-packages/cv2/.libs/libswresample-81cb7b3e.so.3.0.101 (0x00007f7752692000)
	libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f775248f000)
	libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f775217e000)
	libSM.so.6 => /usr/lib/x86_64-linux-gnu/libSM.so.6 (0x00007f7751f75000)
	libICE.so.6 => /usr/lib/x86_64-linux-gnu/libICE.so.6 (0x00007f7751d5b000)
	libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f7751b51000)
	libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f775193e000)
	libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f7751604000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f7751393000)
	libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f775118e000)
	libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f7750f6c000)
	libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f7750d67000)
	libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f7750b61000)

I did also sudo apt-get install libopencv-dev python-opencv and installed pip install opencv-python next to it (do not do this) but nothing broke.

To be able to fix this or to provide some solution, I still need more information. This means that every thing related to these is important:

  • FULL EXAMPLE of the script which produces this error, one line is not simply enough
  • exact steps which were used to install opencv-python
  • possible other conflicting OpenCV installations
  • anything related to Qt
  • are you using virtual environment or not
  • has the Ubuntu envinroment been modified somehow

Please note that opencv-python versions < 3.3.0.10 do not support GUI functionality and thus support for it cannot be enabled in them without a full rebuild which is impossible because the packages are pre-built. So if you see this error, you are using too old package:

error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script

Releases page contains changelogs for this package: https://github.com/skvark/opencv-python/releases