服务器上OpenCV报错

一、发现问题

  1. 在服务器上(没有图形化界面)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'

  2. 如果此时卸载了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。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值