cv::error: (-215:断言失败) !_tracker.empty() && _tracker->isInitialized() 在函数 'cv::TrackerNano::update' 中
cv::error: (-215:Assertion failed) !_tracker.empty() && _tracker->isInitialized() in function 'cv::TrackerNano::update'
ID: opencv/tracker-nano-init-failed
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| opencv-4.8.0 | active | — | — | — |
| opencv-4.9.0 | active | — | — | — |
| opencv-4.10.0 | active | — | — | — |
根因分析
在调用 update 方法之前,TrackerNano 对象未使用边界框正确初始化,或者跟踪器在初始化后被销毁。
English
The TrackerNano object was not properly initialized with a bounding box before calling the update method, or the tracker was destroyed after initialization.
官方文档
https://docs.opencv.org/4.x/d0/d0a/classcv_1_1TrackerNano.html解决方案
-
Initialize the tracker with a valid bounding box before update: cv::Ptr<cv::TrackerNano> tracker = cv::TrackerNano::create(); cv::Rect2d bbox = cv::selectROI(frame); tracker->init(frame, bbox); Then in the loop: tracker->update(frame, bbox);
-
Check if the tracker is initialized before update: if (tracker->isInitialized()) { tracker->update(frame, bbox); } else { tracker->init(frame, bbox); } -
Ensure the frame passed to init is the same size and type as subsequent frames. Convert all frames to CV_8UC3: frame.convertTo(frame, CV_8UC3);
无效尝试
常见但无效的做法:
-
Reinstall OpenCV with contrib modules enabled
90% 失败
TrackerNano is part of opencv_contrib, but if it's already available (error shows the class exists), recompiling won't fix initialization logic.
-
Call tracker->init(frame, cv::Rect()) with an empty rectangle
85% 失败
An empty rectangle (0 width or height) will cause the init to fail silently, and isInitialized() will return false.
-
Use a different tracker type like TrackerCSRT
50% 失败
This avoids the error but doesn't fix the underlying issue with TrackerNano initialization. The same mistake may occur with other trackers.