报错
在带有桌面的ubuntu 16.04中,使用cv2.imshow()
函数,报错信息如下
No protocol specified
qt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/wangxin/anaconda3/envs/pytorch1.8_py3.7/lib/python3.7/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb, eglfs, minimal, minimalegl, offscreen, vnc.
Aborted (core dumped)
解决方案
下面的方法亲测成功。
1. 更换较低版本的opencv-python
参考博客《qt.qpa.xcb: could not connect to display解决方案》,更具体来说,参考链接https://stackoverflow.com/questions/60032540/opencv-cv2-imshow-is-not-working-because-of-the-qt
先卸载当前版本opencv-python,再安装较低版本opencv-python==4.1.0.25
pip uninstall opencv-python
pip install opencv-python==4.1.0.25 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
此时,再使用cv2.imshow()
函数,依然会出现报错信息,如下
No protocol specified
: cannnot connect to X server : 0
2. 设置X server
参考博客《解决xhost: unable to open display》
切回到root账号下,在root账号下,输入如下命令
export DISPLAY=:0.0
xhost +
出现access control disabled, clients can connect from any host
即为成功。
上面,export DISPLAY=:0.0
意为将图片展示在本机。