-2 opencv system_error ai_generated partial

cv::error: (-2:未指定错误) 无法创建窗口 在函数 'cv::namedWindow' 中

cv::error: (-2:Unspecified error) Could not create window in function 'cv::namedWindow'

ID: opencv/highgui-window-could-not-be-created

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2024-09-18首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.8.0 active
4.9.0 active

根因分析

显示服务器(X11、Wayland 或 macOS Quartz)不可用,或窗口管理器未运行。

English

The display server (X11, Wayland, or macOS Quartz) is not available, or the window manager is not running.

generic

官方文档

https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5afdf8410934fd099df085bd3b1c0d6a

解决方案

  1. import os
    os.environ['OPENCV_IO_ENABLE_OPENCL'] = '0'
    # Or simply avoid namedWindow calls; save images to disk instead:
    cv2.imwrite('output.jpg', img)
    # For debugging, print image shape instead of displaying.
  2. sudo apt-get install xvfb
    Xvfb :99 -screen 0 1024x768x24 &
    export DISPLAY=:99
    # Then run your OpenCV script

无效尝试

常见但无效的做法:

  1. 50% 失败

    Qt backend also requires a display server; if no display is available, it will still fail.

  2. 70% 失败

    If no X server is running, setting DISPLAY won't create one; the variable only works if X is already active.

  3. 80% 失败

    Root privileges do not enable a display server; the error is about display availability, not permissions.