一、发现问题
-
在服务器上(没有图形化界面)python代码如果使用了cv2.imshow()会报错:
cv2.error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:632: error: (-2:Unspecified error) 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 'cvShowImage'
-
如果此时卸载了conda安装的opencv用pip重装opencv-python,运行时也会报错:
qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/public/home/yuchangbingroup/yanliqi/anaconda2/envs/RAFT-pytorch1.6/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.
二、解决办法
这是因为服务器上没有图形化界面,没办法展示图像,因此只要把cv2.imshow()删掉,或者改成imwrite()来保存图像,然后自己下载图像到本地查看即可。
如:
cv2.imshow("img", image_numpy)
改成:
cv2.imwrite("img.png", image_numpy * 255)
此处需要乘上255,因为imshow的范围是(0,1)而图像应为(0,255)。
【PS】同理,cv2.imwrite()改成cv2.imshow()的时候应当除以255。