From 2f977e1fd06ac51120b12fbbb6e36956dff25030 Mon Sep 17 00:00:00 2001 From: Pedro Senna Date: Fri, 1 Jun 2018 21:09:58 -0400 Subject: [PATCH 1/2] VOT2018 updates: partial feedback, adaptative model update and bugfix --- .gitignore | 2 +- CMakeLists.txt | 2 +- kfebtracker.cpp | 10 +++++----- main.cpp | 2 +- main_cam.cpp | 3 +-- trackers/ASMS/colotracker.cpp | 9 +++++++-- trackers/ASMS/colotracker.h | 2 +- trackers/ASMS/histogram.cpp | 9 ++++----- trackers/ASMS/histogram.h | 2 +- trackers/btracker.h | 10 ++++++++++ trackers/kcf/adjust.cpp | 9 +++++++++ trackers/kcf/adjust.h | 1 + trackers/kcf/kcf.cpp | 6 +++--- trackers/kcf/kcf.h | 2 +- trackers/tasms.cpp | 8 +++++--- trackers/tasms.h | 2 ++ trackers/tkcf.cpp | 15 +++++---------- trackers/tkcf.h | 1 + trackers/tncc.cpp | 3 ++- 19 files changed, 61 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index bff2d15..b19f7b0 100644 --- a/.gitignore +++ b/.gitignore @@ -73,4 +73,4 @@ Thumbs.db # -------- *.dll *.exe - +build/* diff --git a/CMakeLists.txt b/CMakeLists.txt index 98218c9..ff47ad5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(KF-EBT C CXX) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic -O2 -Wno-long-long -fno-omit-frame-pointer -lpthread") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic -O2 -Wno-long-long -fno-omit-frame-pointer -lpthread -msse -msse2 -msse3") FIND_PACKAGE( OpenCV REQUIRED ) FIND_PACKAGE (Threads REQUIRED) diff --git a/kfebtracker.cpp b/kfebtracker.cpp index 34edb4b..7d50eb1 100644 --- a/kfebtracker.cpp +++ b/kfebtracker.cpp @@ -2,11 +2,11 @@ KFebTracker::KFebTracker() { - asms = tASMS(ajuste, 0.90); - kcf = tKCF(ajuste, 1.15); - cbt = tCBT(ajuste, 0.45); - vdp = tVDP(ajuste, 0.60); - ncc = tncc(ajuste, 0.7); + asms = tASMS(ajuste, 1.0f); + kcf = tKCF(ajuste, 1.15f); + cbt = tCBT(ajuste, 0.45f); + vdp = tVDP(ajuste, 0.60f); + ncc = tncc(ajuste, 0.7f); } /* Initiation Parameters diff --git a/main.cpp b/main.cpp index 8844f22..1c95aba 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,7 @@ int main(void){ KFebTracker tracker; - tracker.init("AKN"); + tracker.init("AK"); trax_handle* trax; trax_metadata* config = trax_metadata_create(TRAX_REGION_RECTANGLE, TRAX_IMAGE_PATH, "KFebT", "KFebT", "none"); diff --git a/main_cam.cpp b/main_cam.cpp index 367cdb1..bf6a04a 100644 --- a/main_cam.cpp +++ b/main_cam.cpp @@ -33,9 +33,8 @@ int main(void){ rectOK = false; KFebTracker tracker; - tracker.init("AKN"); + tracker.init("AK"); - cv::Rect region; cv::Mat image; bool run = 1; diff --git a/trackers/ASMS/colotracker.cpp b/trackers/ASMS/colotracker.cpp index 8326b61..eb5e191 100644 --- a/trackers/ASMS/colotracker.cpp +++ b/trackers/ASMS/colotracker.cpp @@ -68,15 +68,20 @@ void ColorTracker::init(cv::Mat & img, int x1, int y1, int x2, int y2) } -void ColorTracker::update(){ +void ColorTracker::update(float ratio){ int x1, x2, y1, y2; x1 = lastPosition.x; y1 = lastPosition.y; x2 = lastPosition.x + lastPosition.width; y2 = lastPosition.y + lastPosition.height; + //boundary checks + y1 = std::max(0, y1); + y2 = std::min(im1.rows-1, y2); + x1 = std::max(0, x1); + x2 = std::min(im1.cols-1, x2); extractForegroundHistogram(x1, y1, x2, y2, q_hist); - q_orig_hist.adapt(&q_hist, UPDATE_RATE); + q_orig_hist.adapt(q_hist, ratio); } cv::Point ColorTracker::histMeanShift(double x1, double y1, double x2, double y2){ diff --git a/trackers/ASMS/colotracker.h b/trackers/ASMS/colotracker.h index c592309..91e1f5f 100644 --- a/trackers/ASMS/colotracker.h +++ b/trackers/ASMS/colotracker.h @@ -82,7 +82,7 @@ class ColorTracker { return track(img, lastPosition.x, lastPosition.y, lastPosition.x + lastPosition.width, lastPosition.y + lastPosition.height, confidence); } - void update(); + void update(float ratio); }; #endif // COLOTRACKER_H diff --git a/trackers/ASMS/histogram.cpp b/trackers/ASMS/histogram.cpp index ddba6fd..1b7fd47 100644 --- a/trackers/ASMS/histogram.cpp +++ b/trackers/ASMS/histogram.cpp @@ -110,12 +110,11 @@ void Histogram::multiplyByWeights(Histogram * hist) normalize(); } -void Histogram::adapt(Histogram * hist, float height){ - for (unsigned int i=0; i < data.size(); ++i) { - data[i] *= 1-height; - data[i] += (hist->data[i]*height); +void Histogram::adapt(Histogram hist, float height){ + for (unsigned int i=0; i < data.size() && i < hist.data.size(); ++i) { + data[i] *= (1-height); + data[i] += (hist.data[i]*height); } - normalize(); } diff --git a/trackers/ASMS/histogram.h b/trackers/ASMS/histogram.h index bc5490e..8fa6f2f 100644 --- a/trackers/ASMS/histogram.h +++ b/trackers/ASMS/histogram.h @@ -36,7 +36,7 @@ class Histogram void transformToWeights(); void transformByWeight(double min); void multiplyByWeights(Histogram * hist); - void adapt(Histogram * hist, float height); + void adapt(Histogram hist, float height); void clear(); void normalize(); diff --git a/trackers/btracker.h b/trackers/btracker.h index e449707..5fc74d3 100644 --- a/trackers/btracker.h +++ b/trackers/btracker.h @@ -11,6 +11,7 @@ #include #define DIST_ADJ 0.30 +#define FEED_RATIO 0.90 class BTracker { @@ -21,6 +22,7 @@ class BTracker float dist_adj; float conf_adj; double ratio; + double feedbackRatio; std::vector state; std::vector stateUncertainty; @@ -32,6 +34,14 @@ class BTracker virtual cv::Rect getRect() = 0; + void updateStateFeedback(std::vector newState) + { + for(int i = 0; i < (int)state.size() && i< (int)newState.size(); i++) + { + state[i] = newState[i]*feedbackRatio + state[i]*(1-feedbackRatio); + } + } + void run() { if(updateModel){ diff --git a/trackers/kcf/adjust.cpp b/trackers/kcf/adjust.cpp index 491253f..10c2de4 100644 --- a/trackers/kcf/adjust.cpp +++ b/trackers/kcf/adjust.cpp @@ -14,11 +14,20 @@ cv::Mat Adjust::init(cv::Mat& image, cv::Rect rect){ float border_w = w; if(h < w){ + + if(h < w*MIN_RATIO){ + h = w*MIN_RATIO; + } + if(h < MINSIZE){ w *= (MINSIZE/h); h = MINSIZE; } } else { + if(w < h*MIN_RATIO){ + w = h*MIN_RATIO; + } + if(w < MINSIZE){ h *= (MINSIZE/w); w = MINSIZE; diff --git a/trackers/kcf/adjust.h b/trackers/kcf/adjust.h index ac66469..96249a6 100644 --- a/trackers/kcf/adjust.h +++ b/trackers/kcf/adjust.h @@ -5,6 +5,7 @@ #define MINSIZE 44 #define KCF_PADDING 1.5 +#define MIN_RATIO 0.9 class Adjust { diff --git a/trackers/kcf/kcf.cpp b/trackers/kcf/kcf.cpp index 8efe254..626af96 100644 --- a/trackers/kcf/kcf.cpp +++ b/trackers/kcf/kcf.cpp @@ -71,7 +71,7 @@ void KCF_Tracker::track(cv::Mat &img) // p_model_alphaf = p_model_alphaf_num / p_model_alphaf_den; } -void KCF_Tracker::updateKernel(cv::Mat img){ +void KCF_Tracker::updateKernel(cv::Mat img, float ratio){ img.convertTo(img, CV_32FC1); ComplexMat xf = fft2(p_fhog.extract(img, 2, p_cell_size, 9), p_cos_window); //Kernel Ridge Regression, calculate alphas (in Fourier domain) @@ -80,8 +80,8 @@ void KCF_Tracker::updateKernel(cv::Mat img){ ComplexMat alphaf = p_yf / (kf + p_lambda); //equation for fast training //subsequent frames, interpolate model - p_model_xf = p_model_xf * (1. - p_interp_factor) + xf * p_interp_factor; - p_model_alphaf = p_model_alphaf * (1. - p_interp_factor) + alphaf * p_interp_factor; + p_model_xf = p_model_xf * (1. - ratio) + xf * ratio; + p_model_alphaf = p_model_alphaf * (1. - ratio) + alphaf * ratio; } cv::Point2f KCF_Tracker::getError(){ diff --git a/trackers/kcf/kcf.h b/trackers/kcf/kcf.h index 9b22dcc..0763738 100644 --- a/trackers/kcf/kcf.h +++ b/trackers/kcf/kcf.h @@ -45,7 +45,7 @@ class KCF_Tracker BBox_c getBBox(); // kernel update - void updateKernel(cv::Mat img); + void updateKernel(cv::Mat img, float ratio); cv::Point2f getError(); double correlation; diff --git a/trackers/tasms.cpp b/trackers/tasms.cpp index 88a1f5e..864598d 100644 --- a/trackers/tasms.cpp +++ b/trackers/tasms.cpp @@ -4,6 +4,8 @@ tASMS::tASMS(float dist_adj, float conf_adj) { this->dist_adj = dist_adj; this->conf_adj = conf_adj; + this->feedbackRatio = FEED_RATIO; + this->updateRatio = 0.03; } void tASMS::init(cv::Mat& image, cv::Rect region){ @@ -13,7 +15,7 @@ void tASMS::init(cv::Mat& image, cv::Rect region){ } void tASMS::correctState(std::vector st){ - this->state = st; + updateStateFeedback(st); asms.lastPosition.height = st[2]*ratio; asms.lastPosition.width = st[2]; asms.lastPosition.x = st[0] - asms.lastPosition.width/2; @@ -21,7 +23,7 @@ void tASMS::correctState(std::vector st){ } void tASMS::track(){ - double confidenceASMS = 0; + confidenceASMS = 0; cv::Rect asmsRect; BBox * bb = asms.track(currentFrame, &confidenceASMS); @@ -47,7 +49,7 @@ void tASMS::track(){ } void tASMS::update(){ - //asms.update(); + asms.update(confidenceASMS * updateRatio); } void tASMS::newFrame(cv::Mat &image, std::vector predictRect){ diff --git a/trackers/tasms.h b/trackers/tasms.h index f243d5c..0828b7a 100644 --- a/trackers/tasms.h +++ b/trackers/tasms.h @@ -19,6 +19,8 @@ class tASMS : public BTracker private: ColorTracker asms; + float updateRatio; + double confidenceASMS; cv::Mat currentFrame; std::vector currentPredictRect; }; diff --git a/trackers/tkcf.cpp b/trackers/tkcf.cpp index d3a809e..79c8fd7 100644 --- a/trackers/tkcf.cpp +++ b/trackers/tkcf.cpp @@ -4,6 +4,8 @@ tKCF::tKCF(float dist_adj, float conf_adj) { this->dist_adj = dist_adj; this->conf_adj = conf_adj; + this->feedbackRatio = FEED_RATIO; + this->updateRatio = 0.04; } void tKCF::init(cv::Mat& image, cv::Rect region){ @@ -19,7 +21,7 @@ void tKCF::init(cv::Mat& image, cv::Rect region){ } void tKCF::correctState(std::vector st){ - this->state = st; + updateStateFeedback(st); this->region.height = round(st[2]*ratio); this->region.width = round(st[2]); this->region.x = round(st[0] - st[2]/2.0); @@ -27,9 +29,6 @@ void tKCF::correctState(std::vector st){ } void tKCF::track(){ - /*cv::Mat imag = currentFrame.clone(); - cv::rectangle(imag, region, cv::Scalar(0, 255, 0));*/ - cv::Point2f motion; cv::cvtColor(currentFrame, grayImage, CV_BGR2GRAY); cv::Mat kcfPatch = adj.getRect(grayImage, region); @@ -46,8 +45,7 @@ void tKCF::track(){ stateUncertainty.clear(); float penalityKCF = pow(dist_adj*fabs(state[0] - currentPredictRect[0])/((double)region.width),2) + - pow(dist_adj*fabs(state[1] - currentPredictRect[1])/((double)region.height), 2);// + - //pow(dist_adj*fabs(state[2] - currentPredictRect[2])/((double)region.width),2); + pow(dist_adj*fabs(state[1] - currentPredictRect[1])/((double)region.height), 2); float uncertainty = 1e-4*exp(-3.5*(conf_adj*kcf.correlation - penalityKCF)); stateUncertainty.push_back(uncertainty); stateUncertainty.push_back(uncertainty); @@ -55,14 +53,11 @@ void tKCF::track(){ region.x += round(motion.x); region.y += round(motion.y); - - /*cv::rectangle(imag, region, cv::Scalar(255, 0, 0)); - cv::imshow("aaaa", imag);*/ } void tKCF::update(){ cv::Mat kcfPatch = adj.getRect(grayImage, region); - kcf.updateKernel(kcfPatch); + kcf.updateKernel(kcfPatch, updateRatio*kcf.correlation); } void tKCF::newFrame(cv::Mat &image, std::vector predictRect){ diff --git a/trackers/tkcf.h b/trackers/tkcf.h index 141e329..52237b6 100644 --- a/trackers/tkcf.h +++ b/trackers/tkcf.h @@ -20,6 +20,7 @@ class tKCF : public BTracker private: KCF_Tracker kcf; Adjust adj; + float updateRatio; cv::Rect region; cv::Mat grayImage; cv::Mat currentFrame; diff --git a/trackers/tncc.cpp b/trackers/tncc.cpp index 84d24bd..9c26922 100644 --- a/trackers/tncc.cpp +++ b/trackers/tncc.cpp @@ -4,6 +4,7 @@ tncc::tncc(float dist_adj, float conf_adj) { this->dist_adj = dist_adj; this->conf_adj = conf_adj; + this->feedbackRatio = FEED_RATIO; } void tncc::init(cv::Mat& image, cv::Rect region){ @@ -13,7 +14,7 @@ void tncc::init(cv::Mat& image, cv::Rect region){ } void tncc::correctState(std::vector st){ - this->state = st; + updateStateFeedback(st); ncc.p_position = cv::Point2f(st[0], st[1]); ncc.p_size.width = st[2]; ncc.p_size.height = st[2]*ratio; From ef6224074b9c3dcc380ea0940d2dccae1e78c6b4 Mon Sep 17 00:00:00 2001 From: Pedro Senna Date: Sun, 3 Jun 2018 19:20:32 -0400 Subject: [PATCH 2/2] test --- trackers/ASMS/colotracker.cpp | 2 +- trackers/tasms.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/trackers/ASMS/colotracker.cpp b/trackers/ASMS/colotracker.cpp index eb5e191..3393d4c 100644 --- a/trackers/ASMS/colotracker.cpp +++ b/trackers/ASMS/colotracker.cpp @@ -154,7 +154,7 @@ cv::Point ColorTracker::histMeanShift(double x1, double y1, double x2, double y2 cv::Point ColorTracker::histMeanShiftIsotropicScale(double x1, double y1, double x2, double y2, double * scale, int * iter, double* similarity) { - int maxIter = 15; + int maxIter = 10; double w2 = (x2-x1)/2; double h2 = (y2-y1)/2; diff --git a/trackers/tasms.cpp b/trackers/tasms.cpp index 864598d..b86a902 100644 --- a/trackers/tasms.cpp +++ b/trackers/tasms.cpp @@ -5,7 +5,7 @@ tASMS::tASMS(float dist_adj, float conf_adj) this->dist_adj = dist_adj; this->conf_adj = conf_adj; this->feedbackRatio = FEED_RATIO; - this->updateRatio = 0.03; + this->updateRatio = 0.02; } void tASMS::init(cv::Mat& image, cv::Rect region){