mirror of
https://github.com/opencv/opencv.git
synced 2026-11-16 06:17:53 -06:00
video(TrackerNano): fix target scale and penalty calculation - #30353 This PR fixes an algorithmic bug and two precision issues in `cv::TrackerNanoImpl::update`: Resolves #27190 ### 1. Fix scale penalty calculation (`targetPos` -> `targetSz`) In the Siamese tracking scale penalty calculation: ```cpp // BEFORE (Bug): Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetPos[0], targetPos[1]); // AFTER (Fix): Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetSz[0], targetSz[1]); ``` `sizeCal` takes `(width, height)`, but `targetPos` holds the target's center pixel coordinates `(x, y)` in the full frame. Passing `targetPos` normalizes candidate scale by screen position rather than object size, breaking translation invariance and heavily dampening candidate scores as the target translates across the frame. Furthermore, `targetSz` is explicitly multiplied by `scale_z` before this calculation (line 252) and divided back afterwards (line 323). The only reason `targetSz` was brought into the search patch coordinate frame was for this exact line. Passing `targetPos` was apparently a typo. ### 2. Fix integer division in search crop scaling ```cpp // BEFORE: instanceSize (255) and exemplarSize (127) are const int float sx = sz * (instanceSize / exemplarSize); // Evaluates to sz * 2 instead of 2.007874f // AFTER: float sx = sz * (float(instanceSize) / exemplarSize); ``` ### 3. Preserve subpixel floating-point precision in `targetSzSum` Removed the `(int)` truncation from `targetSzSum` in `update()`: ```cpp // BEFORE: int targetSzSum = (int)(targetSz[0] + targetSz[1]); // AFTER: float targetSzSum = targetSz[0] + targetSz[1]; ``` --- ### Verification and Test Results Tested on a sequence where an object expands from 60x60 to 138x138 across 40 frames while translating across the frame: * **Before Fix (`targetPos`):** * Final Tracked Size: `133x131` * Final Area Ratio (Tracked / Ground Truth): **0.915** (persistent size lag)