【问题标题】:Problem using OpenCV in WSL, when opening windows打开窗口时在 WSL 中使用 OpenCV 时出现问题
【发布时间】:2021-05-02 11:16:00
【问题描述】:

我正在使用 WSL,当使用 OpenCV 运行一些需要打开不同窗口的 Python 代码时,它会引发错误。 这是代码:

import cv2 as cv

image = cv.imread("images/pic.jpg")
cv.imshow(0)
cv.waitKey(0)

这是我得到的错误

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/user/.local/lib/python3.8/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.

Aborted (core dumped)

【问题讨论】:

    标签: python linux opencv computer-vision windows-subsystem-for-linux


    【解决方案1】:

    我遇到了类似的问题。正如大多数人推荐的那样,我尝试放置一个符号链接并安装新包。我更改了 QT_PLUGIN_PATH,也导出了 AT_DEBUG_PLUGINS=1。这些选项似乎对我不起作用。我在带有 WSL2 的 Windows 10 上运行未显示的 OpenCV cv2.image。据说错误与 qt 相关(如 #josh merrian 所述)。

        qt.qpa.xcb: could not connect to display 
        qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/user/.local/lib/python3.8/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.
        
        Aborted (core dumped)
    

    尝试运行 xeyes。

        sudo apt-get install x11-apps
        xeyes
    

    它运行了吗?如果不是,您可能遇到与我相同的问题。

    在我的例子中,我了解到在 WSL2 中我们需要一个 Xserver 来部署面向 Ubuntu 的图形应用程序。我阅读了有关 Xming 和 VcSrv 的信息。我尝试了 VcS​​rv,它对我有用。

    见:https://www.stat.ipb.ac.id/agusms/index.php/2019/01/15/how-to-run-graphical-linux-applications-on-bash-on-ubuntu-on-windows-10/

    希望这对那里的人有所帮助。祝你好运。

    【讨论】:

      【解决方案2】:

      升级 OpenCV,使用 pip 使用以下命令

      pip3 install --upgrade opencv-python==4.3.0.36
      

      【讨论】:

        【解决方案3】:

        您需要为 WSL 上的 GUI 应用程序启用您的环境。

        Using the GUI for enabling Windows features

        1. 打开开始菜单并搜索打开或关闭 Windows 功能
        2. 选择适用于 Linux 的 Windows 子系统
        3. 点击确定
        4. 出现提示时重新启动计算机

        【讨论】:

        • 你安装过qt版本吗?也许你可以试试install libqt5gui5
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-20
        • 1970-01-01
        • 2016-03-06
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多