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

- **ID:** `opencv/highgui-window-could-not-be-created`
- **领域:** opencv
- **类别:** system_error
- **错误码:** `-2`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

显示服务器（X11、Wayland 或 macOS Quartz）不可用，或窗口管理器未运行。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — Qt backend also requires a display server; if no display is available, it will still fail. (50% 失败率)
- **** — If no X server is running, setting DISPLAY won't create one; the variable only works if X is already active. (70% 失败率)
- **** — Root privileges do not enable a display server; the error is about display availability, not permissions. (80% 失败率)
