Skip to content

Commit

Permalink
Added jitter filter on Tracked boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Mar 16, 2021
1 parent 1f4d33d commit a33497a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
42 changes: 27 additions & 15 deletions src/CVTracker.cpp
Expand Up @@ -178,30 +178,42 @@ bool CVTracker::trackFrame(cv::Mat &frame, size_t frameId){
float fw = frame.size().width;
float fh = frame.size().height;

std::vector<cv::Rect> bboxes = {bbox};
std::vector<float> confidence = {1.0};
std::vector<int> classId = {1};

sort.update(bboxes, frameId, sqrt(pow(frame.rows, 2) + pow(frame.cols, 2)), confidence, classId);

for(auto TBox : sort.frameTrackingResult)
bbox = TBox.box;

cv::Rect2d filtered_box = filter_box_jitter(frameId);
// Add new frame data
trackedDataById[frameId] = FrameData(frameId, 0, (bbox.x)/fw,
(bbox.y)/fh,
(bbox.x+bbox.width)/fw,
(bbox.y+bbox.height)/fh);
trackedDataById[frameId] = FrameData(frameId, 0, (filtered_box.x)/fw,
(filtered_box.y)/fh,
(filtered_box.x+filtered_box.width)/fw,
(filtered_box.y+filtered_box.height)/fh);
}
else
{
// Add new frame data
trackedDataById[frameId] = FrameData(frameId);
// Copy the last frame data if the tracker get lost
trackedDataById[frameId] = trackedDataById[frameId-1];
}

return ok;
}

cv::Rect2d CVTracker::filter_box_jitter(size_t frameId){
// get tracked data for the previous frame
float last_box_width = trackedDataById[frameId-1].x2 - trackedDataById[frameId-1].x1;
float last_box_height = trackedDataById[frameId-1].y2 - trackedDataById[frameId-1].y1;

float curr_box_width = bbox.width;
float curr_box_height = bbox.height;
// keep the last width and height if the difference is less than 1%
float threshold = 0.01;

cv::Rect2d filtered_box = bbox;
if(std::abs(1-(curr_box_width/last_box_width)) <= threshold){
filtered_box.width = last_box_width;
}
if(std::abs(1-(curr_box_height/last_box_height)) <= threshold){
filtered_box.height = last_box_height;
}
return filtered_box;
}

bool CVTracker::SaveTrackedData(){
// Create tracker message
pb_tracker::Tracker trackerMessage;
Expand Down
3 changes: 2 additions & 1 deletion src/CVTracker.h
Expand Up @@ -122,7 +122,8 @@ namespace openshot
// Track object in the hole clip or in a given interval
// If start, end and process_interval are passed as argument, clip will be processed in [start,end)
void trackClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);

// Filter current bounding box jitter
cv::Rect2d filter_box_jitter(size_t frameId);
// Get tracked data for a given frame
FrameData GetTrackedData(size_t frameId);

Expand Down

0 comments on commit a33497a

Please sign in to comment.