# cv::error: (-215:断言失败) !_tracker.empty() && _tracker->isInitialized() 在函数 'cv::TrackerNano::update' 中

- **ID:** `opencv/tracker-nano-init-failed`
- **领域:** opencv
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

在调用 update 方法之前，TrackerNano 对象未使用边界框正确初始化，或者跟踪器在初始化后被销毁。

## 版本兼容性

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

## 解决方案

1. ```
   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);
   ```
2. ```
   Check if the tracker is initialized before update: if (tracker->isInitialized()) { tracker->update(frame, bbox); } else { tracker->init(frame, bbox); }
   ```
3. ```
   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** — TrackerNano is part of opencv_contrib, but if it's already available (error shows the class exists), recompiling won't fix initialization logic. (90% 失败率)
- **Call tracker->init(frame, cv::Rect()) with an empty rectangle** — An empty rectangle (0 width or height) will cause the init to fail silently, and isInitialized() will return false. (85% 失败率)
- **Use a different tracker type like TrackerCSRT** — This avoids the error but doesn't fix the underlying issue with TrackerNano initialization. The same mistake may occur with other trackers. (50% 失败率)
