Skip to content

Commit

Permalink
Merge pull request #2 from psenna/Test
Browse files Browse the repository at this point in the history
Tests for VOT2018: partial feedback, adaptative model update and bugfix
  • Loading branch information
psenna authored Jun 8, 2018
2 parents 1495dae + ef62240 commit 3a47168
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ Thumbs.db
# --------
*.dll
*.exe

build/*
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions kfebtracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions main_cam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 8 additions & 3 deletions trackers/ASMS/colotracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -149,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;
Expand Down
2 changes: 1 addition & 1 deletion trackers/ASMS/colotracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions trackers/ASMS/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion trackers/ASMS/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions trackers/btracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <pthread.h>

#define DIST_ADJ 0.30
#define FEED_RATIO 0.90

class BTracker
{
Expand All @@ -21,6 +22,7 @@ class BTracker
float dist_adj;
float conf_adj;
double ratio;
double feedbackRatio;
std::vector<float> state;
std::vector<float> stateUncertainty;

Expand All @@ -32,6 +34,14 @@ class BTracker

virtual cv::Rect getRect() = 0;

void updateStateFeedback(std::vector<float> 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){
Expand Down
9 changes: 9 additions & 0 deletions trackers/kcf/adjust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions trackers/kcf/adjust.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define MINSIZE 44
#define KCF_PADDING 1.5
#define MIN_RATIO 0.9

class Adjust
{
Expand Down
6 changes: 3 additions & 3 deletions trackers/kcf/kcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(){
Expand Down
2 changes: 1 addition & 1 deletion trackers/kcf/kcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 5 additions & 3 deletions trackers/tasms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.02;
}

void tASMS::init(cv::Mat& image, cv::Rect region){
Expand All @@ -13,15 +15,15 @@ void tASMS::init(cv::Mat& image, cv::Rect region){
}

void tASMS::correctState(std::vector<float> 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;
asms.lastPosition.y = st[1] - asms.lastPosition.height/2;
}

void tASMS::track(){
double confidenceASMS = 0;
confidenceASMS = 0;
cv::Rect asmsRect;
BBox * bb = asms.track(currentFrame, &confidenceASMS);

Expand All @@ -47,7 +49,7 @@ void tASMS::track(){
}

void tASMS::update(){
//asms.update();
asms.update(confidenceASMS * updateRatio);
}

void tASMS::newFrame(cv::Mat &image, std::vector<float> predictRect){
Expand Down
2 changes: 2 additions & 0 deletions trackers/tasms.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class tASMS : public BTracker

private:
ColorTracker asms;
float updateRatio;
double confidenceASMS;
cv::Mat currentFrame;
std::vector<float> currentPredictRect;
};
Expand Down
15 changes: 5 additions & 10 deletions trackers/tkcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -19,17 +21,14 @@ void tKCF::init(cv::Mat& image, cv::Rect region){
}

void tKCF::correctState(std::vector<float> 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);
this->region.y = round(st[1] - (st[2]*ratio/2.0));
}

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);
Expand All @@ -46,23 +45,19 @@ 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);
stateUncertainty.push_back(uncertainty*5.0);

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<float> predictRect){
Expand Down
1 change: 1 addition & 0 deletions trackers/tkcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion trackers/tncc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -13,7 +14,7 @@ void tncc::init(cv::Mat& image, cv::Rect region){
}

void tncc::correctState(std::vector<float> 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;
Expand Down

0 comments on commit 3a47168

Please sign in to comment.