# cv::error: (-215:断言失败) !winname.empty() && namedWindow(winname, flags) == 0 在函数 'imshow' 中

- **ID:** `opencv/highgui-window-name-duplicate`
- **领域:** opencv
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

imshow() 调用时使用了空窗口名称，或者由于未使用正确标志重复创建而导致窗口名称冲突。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.1 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

1. ```
   Always provide a non-empty, unique window name: cv::imshow("Display Window", img); or use cv::namedWindow("MyWindow", cv::WINDOW_AUTOSIZE); cv::imshow("MyWindow", img);
   ```
2. ```
   Check if window exists before creating: if (cv::getWindowProperty("MyWindow", cv::WND_PROP_VISIBLE) < 0) { cv::namedWindow("MyWindow", cv::WINDOW_AUTOSIZE); }
   ```

## 无效尝试

- **Calling namedWindow with a different flag like WINDOW_NORMAL every time** — If the window name already exists, namedWindow returns non-zero but doesn't fail; imshow still sees the conflict. (70% 失败率)
- **Using imshow without namedWindow at all** — imshow internally calls namedWindow, so the same assertion can trigger if the name is empty. (80% 失败率)
- **Destroying window with destroyWindow before each imshow** — If the window name is empty, destroyWindow does nothing and imshow still fails. (85% 失败率)
