opencv assertion_error ai_generated true

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

cv::error: (-215:Assertion failed) !winname.empty() && namedWindow(winname, flags) == 0 in function 'imshow'

ID: opencv/highgui-window-name-duplicate

其他格式: JSON · Markdown 中文 · English
85%修复率
82%置信度
1证据数
2023-04-28首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.1 active
4.9.0 active

根因分析

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

English

imshow() is called with an empty window name or a window name that conflicts with an existing named window due to duplicate creation without proper flags.

generic

官方文档

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

解决方案

  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); }

无效尝试

常见但无效的做法:

  1. Calling namedWindow with a different flag like WINDOW_NORMAL every time 70% 失败

    If the window name already exists, namedWindow returns non-zero but doesn't fail; imshow still sees the conflict.

  2. Using imshow without namedWindow at all 80% 失败

    imshow internally calls namedWindow, so the same assertion can trigger if the name is empty.

  3. Destroying window with destroyWindow before each imshow 85% 失败

    If the window name is empty, destroyWindow does nothing and imshow still fails.