video: isolate TrackerVit crops from parent images

This commit is contained in:
Shih Kai Siang
2026-10-26 23:17:43 +08:00
parent fa3037533e
commit 58e52e9d3c
2 changed files with 29 additions and 3 deletions

View File

@@ -107,12 +107,13 @@ static int crop_image(const Mat& src, Mat& dst, Rect box, int factor)
int x1_pad = std::max(0, -x1);
int y1_pad = std::max(0, -y1);
int x2_pad = std::max(x2 - src.size[1] + 1, 0);
int y2_pad = std::max(y2 - src.size[0] + 1, 0);
int x2_pad = std::max(x2 - src.size[1], 0);
int y2_pad = std::max(y2 - src.size[0], 0);
Rect roi(x1 + x1_pad, y1 + y1_pad, x2 - x2_pad - x1 - x1_pad, y2 - y2_pad - y1 - y1_pad);
Mat im_crop = src(roi);
copyMakeBorder(im_crop, dst, y1_pad, y2_pad, x1_pad, x2_pad, BORDER_CONSTANT);
// Keep padding inside src when src is itself an ROI of a larger image.
copyMakeBorder(im_crop, dst, y1_pad, y2_pad, x1_pad, x2_pad, BORDER_CONSTANT | BORDER_ISOLATED);
return crop_sz;
}

View File

@@ -224,4 +224,29 @@ TEST(vittrack, bbox_inside_tiny_image)
ASSERT_EQ(roi & imgRect, roi);
}
TEST(vittrack, roi_image_matches_clone)
{
std::string model = cvtest::findDataFile("dnn/onnx/models/vitTracker.onnx");
cv::TrackerVit::Params params;
params.net = model;
params.tracking_score_threshold = 0.f;
Mat parent(160, 160, CV_8UC3, Scalar(255, 0, 0));
Mat image = parent(Rect(16, 16, 128, 128));
image.setTo(Scalar::all(0));
Mat clone = image.clone();
Rect initial(100, 100, 20, 20);
Ptr<TrackerVit> roiTracker = TrackerVit::create(params);
Ptr<TrackerVit> cloneTracker = TrackerVit::create(params);
roiTracker->init(image, initial);
cloneTracker->init(clone, initial);
Rect roiResult, cloneResult;
ASSERT_TRUE(roiTracker->update(image, roiResult));
ASSERT_TRUE(cloneTracker->update(clone, cloneResult));
EXPECT_EQ(roiResult, cloneResult);
EXPECT_NEAR(roiTracker->getTrackingScore(), cloneTracker->getTrackingScore(), 1e-6f);
}
}} // namespace opencv_test::