From b84ed06e079fa3814ca7f2eb0bee8a99d731ea83 Mon Sep 17 00:00:00 2001 From: kvrban <33060804+kvrban@users.noreply.github.com> Date: Wed, 21 Oct 2026 07:34:57 +0200 Subject: [PATCH] Merge pull request #30353 from kvrban:5.x 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) --- modules/video/src/tracking/tracker_nano.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/video/src/tracking/tracker_nano.cpp b/modules/video/src/tracking/tracker_nano.cpp index 86c7e12785..ca612dd70b 100644 --- a/modules/video/src/tracking/tracker_nano.cpp +++ b/modules/video/src/tracking/tracker_nano.cpp @@ -139,9 +139,8 @@ protected: Mat grid2searchX, grid2searchY; dnn::Net backbone, neckhead; - Mat image; - void getSubwindow(Mat& dstCrop, Mat& srcImg, int originalSz, int resizeSz); + void getSubwindow(Mat& dstCrop, const Mat& srcImg, int originalSz, int resizeSz); void generateGrids(); }; @@ -173,7 +172,7 @@ void TrackerNanoImpl::init(InputArray image_, const Rect &boundingBox_) { scoreSize = (instanceSize - exemplarSize) / trackState.totalStride + 8; trackState = trackerConfig(); - image = image_.getMat().clone(); + const Mat image = image_.getMat(); // convert Rect2d from left-up to center. targetPos[0] = float(boundingBox_.x) + float(boundingBox_.width) * 0.5f; @@ -202,9 +201,8 @@ void TrackerNanoImpl::init(InputArray image_, const Rect &boundingBox_) generateGrids(); } -void TrackerNanoImpl::getSubwindow(Mat& dstCrop, Mat& srcImg, int originalSz, int resizeSz) +void TrackerNanoImpl::getSubwindow(Mat& dstCrop, const Mat& srcImg, int originalSz, int resizeSz) { - Scalar avgChans = mean(srcImg); Size imgSz = srcImg.size(); int c = (originalSz + 1) / 2; @@ -232,6 +230,8 @@ void TrackerNanoImpl::getSubwindow(Mat& dstCrop, Mat& srcImg, int originalSz, in } else // Crop image with padding, and the padding value is avgChans { + // The mean over the whole frame is only needed for the padding value. + Scalar avgChans = mean(srcImg); cv::Mat tmpMat; cv::copyMakeBorder(srcImg, tmpMat, top_pad, bottom_pad, left_pad, right_pad, cv::BORDER_CONSTANT, avgChans); cropImg = tmpMat(cv::Rect(context_xmin, context_ymin, context_xmax - context_xmin + 1, context_ymax - context_ymin + 1)); @@ -241,14 +241,14 @@ void TrackerNanoImpl::getSubwindow(Mat& dstCrop, Mat& srcImg, int originalSz, in bool TrackerNanoImpl::update(InputArray image_, Rect &boundingBoxRes) { - image = image_.getMat().clone(); - int targetSzSum = (int)(targetSz[0] + targetSz[1]); + const Mat image = image_.getMat(); + float targetSzSum = targetSz[0] + targetSz[1]; float wc = targetSz[0] + trackState.contextAmount * targetSzSum; float hc = targetSz[1] + trackState.contextAmount * targetSzSum; float sz = cv::sqrt(wc * hc); float scale_z = exemplarSize / sz; - float sx = sz * (instanceSize / exemplarSize); + float sx = sz * (float(instanceSize) / exemplarSize); targetSz[0] *= scale_z; targetSz[1] *= scale_z; @@ -284,7 +284,7 @@ bool TrackerNanoImpl::update(InputArray image_, Rect &boundingBoxRes) // size penalty // scale penalty - Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetPos[0], targetPos[1]); + Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetSz[0], targetSz[1]); elementReciprocalMax(sc); // ratio penalty