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'
On which operating system? This is probably a bug in OpenCV or in your code and there's nothing I can do about it.
And no need to report Qt version, wheels have Qt 4.8.7 embedded inside them. They do not use your system's Qt version.
Are you sure you are using the prebuilt wheels?
yepp, using prebuilt wheels. It is on ubuntu 14.04
Works fine on Ubuntu 14.04:

Can you post more code?
I get the same error when attempting cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE), but only when using python3.5
I'm on Ubuntu 16.04.
@braindevices @TechnoConserve can you provide more information about your Ubuntu environment, the output of which -a python3, which -a pip, pip freeze, env (Always remember to check for any secret information that shouldn't be shared such as API keys or other items before you post the output of env) , and apt list --installed would all prove useful.
uname -a
Linux ave-GL552VW 4.10.0-35-generic #39~16.04.1-Ubuntu SMP Wed Sep 13 09:02:42 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
which -a python3
/home/ave/venv/opencv/bin/python3
/home/ave/venv/potato/bin/python3
/usr/bin/python3
which -a pip
/home/ave/venv/potato/bin/pip
/home/ave/.local/bin/pip
/usr/local/bin/pip
/usr/bin/pip
That does seem puzzling to me since it only picked up pip in one of my virtual environments regardless of whether I have the opencv virtual environment active or not.
pip freeze
apparmor==2.10.95
apparmor.click==0.3.13.1
apt-xapian-index==0.47
apturl==0.5.2
awscli==1.11.155
beautifulsoup4==4.6.0
blinker==1.3
botocore==1.7.13
Brlapi==0.6.4
certifi==2017.7.27.1
chardet==3.0.4
checkbox-support==0.22
click==0.4.43+16.4.20170613.0ubuntu1
colorama==0.3.7
command-not-found==0.3
cryptography==1.2.3
defer==1.0.6
Django==1.11.5
django-modelcluster==3.1
django-taggit==0.22.1
django-treebeard==4.1.2
djangorestframework==3.6.4
docutils==0.14
feedparser==5.1.3
guacamole==0.9.2
html5lib==0.999
httplib2==0.9.1
idna==2.6
Jinja2==2.8
jmespath==0.9.3
language-selector==0.1
LibAppArmor==2.10.95
louis==2.6.4
lxml==3.5.0
Mako==1.0.3
MarkupSafe==0.23
numpy==1.13.1
oauthlib==1.0.3
onboard==1.2.0
opencv-python==3.3.0.10
padme==1.1.1
pexpect==4.0.1
Pillow==3.1.2
pkg-resources==0.0.0
plainbox==0.25
psycopg2==2.7.3.1
ptyprocess==0.5
pyasn1==0.3.5
pycups==1.9.73
pycurl==7.43.0
pygobject==3.20.0
PyJWT==1.3.0
pyparsing==2.0.3
python-apt==1.1.0b1
python-dateutil==2.6.1
python-debian==0.1.27
python-gnupg==0.3.8
python-systemd==231
pytz==2017.2
pyxdg==0.25
PyYAML==3.12
reportlab==3.3.0
requests==2.18.4
rsa==3.4.2
s3transfer==0.1.11
sessioninstaller==0.0.0
six==1.11.0
system-image==3.1
system-service==0.3
ubuntu-drivers-common==0.0.0
ufw==0.35
unattended-upgrades==0.1
Unidecode==0.4.21
unity-scope-calculator==0.1
unity-scope-chromiumbookmarks==0.1
unity-scope-colourlovers==0.1
unity-scope-devhelp==0.1
unity-scope-firefoxbookmarks==0.1
unity-scope-manpages==0.1
unity-scope-openclipart==0.1
unity-scope-texdoc==0.1
unity-scope-tomboy==0.1
unity-scope-virtualbox==0.1
unity-scope-yelp==0.1
unity-scope-zotero==0.1
urllib3==1.22
usb-creator==0.3.0
uWSGI==2.0.15
wagtail==1.12.2
Willow==0.4
xdiagnose==3.8.4.1
xkit==0.0.0
XlsxWriter==0.7.3
env
XDG_VTNR=7
KDE_MULTIHEAD=false
SSH_AGENT_PID=2482
XDG_SESSION_ID=2
PAM_KWALLET5_LOGIN=/tmp/kwallet5_ave.socket
TERM=xterm
SHELL=/bin/bash
KONSOLE_DBUS_SERVICE=:1.31
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/ave/.gtkrc-2.0:/home/ave/.config/gtkrc-2.0
KONSOLE_PROFILE_NAME=Green
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
GS_LIB=/home/ave/.fonts
GTK_RC_FILES=/etc/gtk/gtkrc:/home/ave/.gtkrc:/home/ave/.config/gtkrc
WINDOWID=58720261
OLDPWD=/home/ave/PycharmProjects/potato-project/potato
SHELL_SESSION_ID=b8195832c1774000aa88ccc122cc6b7b
GTK_MODULES=gail:atk-bridge
XDG_SESSION_CLASS=user
KDE_FULL_SESSION=true
USER=ave
QT_ACCESSIBILITY=1
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
SSH_AUTH_SOCK=/tmp/ssh-YXVwNQB4fLBl/agent.2436
SESSION_MANAGER=local/ave-GL552VW:@/tmp/.ICE-unix/2732,unix/ave-GL552VW:/tmp/.ICE-unix/2732
DEFAULTS_PATH=/usr/share/gconf//usr/share/xsessions/plasma.default.path
VIRTUAL_ENV=/home/ave/venv/opencv
XDG_CONFIG_DIRS=/etc/xdg/xdg-/usr/share/xsessions/plasma:/etc/xdg:/usr/share/kubuntu-default-settings/kf5-settings
DESKTOP_SESSION=/usr/share/xsessions/plasma
PATH=/home/ave/venv/opencv/bin:/home/ave/venv/potato/bin:/home/ave/bin:/home/ave/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
QT_IM_MODULE=compose
QT_QPA_PLATFORMTHEME=kde
PWD=/home/ave/venv
XDG_SESSION_TYPE=x11
LANG=en_US.UTF-8
KDE_SESSION_UID=1000
MANDATORY_PATH=/usr/share/gconf//usr/share/xsessions/plasma.mandatory.path
PS1=(opencv) \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
KONSOLE_DBUS_SESSION=/Sessions/1
GTK2_MODULES=overlay-scrollbar
COLORFGBG=15;0
HOME=/home/ave
SHLVL=2
XDG_SEAT=seat0
KDE_SESSION_VERSION=5
LANGUAGE=
XCURSOR_THEME=breeze_cursors
LOGNAME=ave
XDG_SESSION_DESKTOP=KDE
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-UiuOcMuIpB,guid=2d3de9776ef25a2171e6481959c98ad4
XDG_DATA_DIRS=/usr/share//usr/share/xsessions/plasma:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
LESSOPEN=| /usr/bin/lesspipe %s
XDG_RUNTIME_DIR=/run/user/1000
DISPLAY=:0
PROFILEHOME=
XDG_CURRENT_DESKTOP=KDE
PAM_KWALLET_LOGIN=/tmp/kwallet_ave.socket
LESSCLOSE=/usr/bin/lesspipe %s %s
ZADDR=zcbZsZdkHfGqbgUjZuM7KiJ7x2ayXLLbXfB8eryRqatW64hhqRK7W74Whjq95jFHKzyTYbnTFnNZBRM8cJ9Q6hVcGGWd7BT
XAUTHORITY=/tmp/xauth-1000-_0
_=/usr/bin/env
Same error (also on 16.04 with python 3.5)
Same error on Ubuntu 16.04 with python 3.5. Beaks at cv2.imshow('Test', img)
Same error on Ubuntu 16.04 with python 2.7. Prints hundreds of error:
QObject::moveToThread: Current thread (0x5563aa165a20) is not the object's thread (0x5563aa278b20).
Cannot move to target thread (0x5563aa165a20)
When calling cv2.waitKey() for the first time in a process. No other effects but the messages are very annoying.
cv2.__version__ = 3.3.0
Same here, opencv 3.3.0 and ubuntu 16.04
Trying cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE) with 3.3.0.10 returns the same QObject error, but using 3.2.0.8 returns
<ipython-input-3-2efaecccc8a8> in <module>()
----> 1 cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE)
error: /io/opencv/modules/highgui/src/window.cpp:565: 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 in function cvNamedWindow
And I already have installed libgtk2.0-dev and pkg-config
Same here, opencv 3.3.0.10 and ubuntu 16.04 with python 3.5.2 cv2.imshow("image", image)
GUI problem with opencv 3.4.0 and ubuntu 16.04 - python 2.7
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
@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.
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.
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
Hi. Having the same problem!
- OS: Ubuntu 16.04 LTS
- Trying to run the script posted by @skvark: `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()`
-
To install OpenCv, I am using a Virtual Environment using Python 3.5. After activating this environment, and using pip install, I installed opencv-python using pip install opencv-python.
-
Related with PyQT4, I installed it using sudo-apt get install and downloading source and runing make install inside the environment, but for some reason, I'm not finding this packaged listed in the File->Settings-> Project Interpreter section of my PyCharm IDE. I dont know if this may be the problem
The error that is showing to me is:
QObject::moveToThread: Current thread (0x1697100) is not the object's thread (0x170e9a0). Cannot move to target thread (0x1697100)
when it gets to the execution of cv2.imshow('test')
Thank you for your help! I'm really stucked in here!
@saeedranjbar12 I run into this problem too, So how to solve it?
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.
As I have written earlier, I can't replicate this behaviour with clean Ubuntu 16.04.4 image. Those two lines of code are not enough. I need more information about the environment because issue is most likely in some combination of other installed software (like PyQt) and opencv-python. OpenCV binaries which ship with these packages are built with the standard OpenCV toolchain and it's unlikely that there's something wrong with them. This seems to be very focused on Ubuntu and some rare cases under it which makes it very hard to replicate and debug the issue.