打开 windows 时在 WSL 中使用 OpenCV 时出现问题

Problem using OpenCV in WSL, when opening windows

我正在使用 WSL,当 运行 某些 Python OpenCV 代码需要打开不同的 window 时,它会抛出错误。 这是代码:

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)

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

Using the GUI for enabling Windows features

  1. Open the Start Menu and search Turn Windows features on or off
  2. Select Windows Subsystem for Linux
  3. Click OK
  4. Restart your computer when prompted

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

pip3 install --upgrade opencv-python==4.3.0.36

我遇到了类似的问题。按照大多数人的建议,我尝试放置一个符号 link 并安装新包。我更改了 QT_PLUGIN_PATH,我也导出了 AT_DEBUG_PLUGINS=1。这些选项似乎对我不起作用。我在 Windows 10 上使用 WSL2,运行ning 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)

尝试 运行ning 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/

希望这对外面的人有所帮助。祝你好运。