Skip to content

Commit

Permalink
Final results from master's thesis
Browse files Browse the repository at this point in the history
  • Loading branch information
psenna committed Jan 23, 2017
1 parent 87799a1 commit ea6b33d
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 60 deletions.
24 changes: 9 additions & 15 deletions kfebt.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#include "kfebt.h"

#include "stdlib.h"

KFEBT::KFEBT(){

}

void KFEBT::setProcessCov(float cov){
cv::setIdentity(KF.processNoiseCov, cv::Scalar::all(cov));
}

KFEBT::KFEBT(int nStates, int nMeasurements, int nInputs, double dt, cv::Rect initialState)
{
KF.init(nStates, nMeasurements, nInputs, CV_64F); // init Kalman Filter
cv::setIdentity(KF.processNoiseCov, cv::Scalar::all(0.8*1e-4)); // set process noise
cv::setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-5)); // set measurement noise
cv::setIdentity(KF.errorCovPost, cv::Scalar::all(1)); // error covariance
/* DYNAMIC MODEL */
// [1 0 d dt 0 0 dt2 0 0 0 0 0 0 0 0 0 0 0]
// [0 1 0 0 dt 0 0 dt2 0 0 0 0 0 0 0 0 0 0]
// [0 0 1 0 0 dt 0 0 dt2 0 0 0 0 0 0 0 0 0]
// [0 0 0 1 0 0 dt 0 0 0 0 0 0 0 0 0 0 0]
// [0 0 0 0 1 0 0 dt 0 0 0 0 0 0 0 0 0 0]
// [0 0 0 0 0 1 0 0 dt 0 0 0 0 0 0 0 0 0]
// [0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0]
// [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]
// [0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0]

// position

// model
KF.transitionMatrix.at<double>(0,3) = dt;
KF.transitionMatrix.at<double>(1,4) = dt;
KF.transitionMatrix.at<double>(2,5) = dt;
Expand Down Expand Up @@ -59,15 +53,15 @@ KFEBT::KFEBT(int nStates, int nMeasurements, int nInputs, double dt, cv::Rect in


void KFEBT::predict(){
estimated = KF.predict();
estimated = KF.predict().clone();
}

void KFEBT::correct(std::vector<float> measures, std::vector<float> Uncertainty){
for(int i = 0; i < measures.size(); i++){
KFMeasures.at<double>(i) = measures[i];
KF.measurementNoiseCov.at<double>(i,i) = Uncertainty[i];
}
corrected = KF.correct(KFMeasures);
corrected = KF.correct(KFMeasures).clone();
}

std::vector<float> KFEBT::getFusion(){
Expand Down
1 change: 1 addition & 0 deletions kfebt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class KFEBT
std::vector<float> getFusion();
cv::Rect getResult();
std::vector<float> getPrediction();
void setProcessCov(float cov);

private:
cv::KalmanFilter KF;
Expand Down
30 changes: 17 additions & 13 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
#include <opencv2/tracking/kalman_filters.hpp>
#include "trax.h"

int main(){
int main(int argc, char *argv[]){

float ajuste = 0.35;

// Alocate trackers
tASMS asms;
tKCF kcf;
tCBT cbt;
tVDP vdp;
tASMS asms(ajuste, 1.00);
tKCF kcf(ajuste, 1.10);
tCBT cbt(ajuste, 0.45);
tVDP vdp(ajuste, 0.60);
KFEBT fusion;

std::vector<BTracker*> trackers;
trackers.push_back(&cbt);
//trackers.push_back(&vdp);
//trackers.push_back(&kcf);
//trackers.push_back(&asms);
//trackers.push_back(&cbt);
trackers.push_back(&vdp);
trackers.push_back(&kcf);
trackers.push_back(&asms);

trax_handle* trax;
trax_configuration config;
Expand All @@ -41,10 +44,6 @@ int main(){
std::vector<float> uncertainty, trackersResults;
bool run = 1;

KFEBT fusion;



while (run){

trax_properties* prop = trax_properties_create();
Expand All @@ -68,6 +67,11 @@ int main(){

// Alocate KFEBT
fusion = KFEBT(9, 3*trackers.size(), 0, 0.05, region);
if(argc >= 7){
float adj = atof(argv[5]);
fusion.setProcessCov(adj);
}


trax_server_reply(trax, rect, NULL);

Expand Down
6 changes: 3 additions & 3 deletions trackers/CBT/cbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void CBT::init(cv::Mat &image, cv::Rect rect, bool findConsensus){
double CBT::track(cv::Mat &image, float &scale){
cv::Mat mask = cv::Mat::zeros(lastImage.size(), CV_8UC1);
cv::Rect r = assertRoi(lastPosition, image.size());
cv::Mat roi = mask(r);
if(r.width > 0 && r.height > 0){
cv::Mat roi = mask(r);
roi.setTo(255);
}

Expand Down Expand Up @@ -72,12 +72,12 @@ double CBT::track(cv::Mat &image, float &scale){

cv::Rect r = assertRoi(lastPosition, image.size());
if(r.width > 0 && r.height > 0){
roi = image(r);
cv::Mat roi = image(r);
double colorConfidence = avaliation.compare(roi);
confidence = foundRate * colorConfidence;
}
} else {
confidence = foundRate*0.8;
confidence = foundRate;
}


Expand Down
4 changes: 3 additions & 1 deletion trackers/btracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <vector>
#include <cmath>

#define DIST_ADJ 0.25
#define DIST_ADJ 0.30

class BTracker : public QThread
{
Expand All @@ -19,6 +19,8 @@ class BTracker : public QThread
public:
bool ok;
bool updateModel;
float dist_adj;
float conf_adj;
double ratio;
std::vector<float> state;
std::vector<float> stateUncertainty;
Expand Down
13 changes: 7 additions & 6 deletions trackers/tasms.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "tasms.h"

tASMS::tASMS()
tASMS::tASMS(float dist_adj, float conf_adj)
{

this->dist_adj = dist_adj;
this->conf_adj = conf_adj;
}

void tASMS::run(){
Expand Down Expand Up @@ -45,10 +46,10 @@ void tASMS::track(){
state.push_back(asms.lastPosition.width);

this->stateUncertainty.clear();
float penalityASMS = pow(DIST_ADJ*fabs(state[0] - currentPredictRect[0])/((double)asms.lastPosition.width),2) +
pow(DIST_ADJ*fabs(state[1] - currentPredictRect[1])/((double)asms.lastPosition.height), 2);// +
//pow(DIST_ADJ*fabs(state[2] - currentPredictRect[2])/(double)asms.lastPosition.width,2);
float uncertainty = 1e-4*exp(-3.5*(1.0*confidenceASMS - penalityASMS));
float penalityASMS = pow(dist_adj*fabs(state[0] - currentPredictRect[0])/((double)asms.lastPosition.width),2) +
pow(dist_adj*fabs(state[1] - currentPredictRect[1])/((double)asms.lastPosition.height), 2);// +
//pow(dist_adj*fabs(state[2] - currentPredictRect[2])/(double)asms.lastPosition.width,2);
float uncertainty = 1e-4*exp(-3.5*(conf_adj*confidenceASMS - penalityASMS));
stateUncertainty.push_back(uncertainty);
stateUncertainty.push_back(uncertainty);
stateUncertainty.push_back(uncertainty);
Expand Down
2 changes: 1 addition & 1 deletion trackers/tasms.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class tASMS : public BTracker
{
public:
tASMS();
tASMS(float dist_adj = DIST_ADJ, float conf_adj = 1.0);

void run();

Expand Down
13 changes: 7 additions & 6 deletions trackers/tcbt.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "tcbt.h"

tCBT::tCBT()
tCBT::tCBT(float dist_adj, float conf_adj)
{

this->dist_adj = dist_adj;
this->conf_adj = conf_adj;
}

void tCBT::run(){
Expand Down Expand Up @@ -37,12 +38,12 @@ void tCBT::track(){
float newWidth = state[2]*scale;

this->stateUncertainty.clear();
float penalityCBT = pow(DIST_ADJ*fabs(state[0] - currentPredictRect[0])/((double)cbt.lastPosition.width),2) +
pow(DIST_ADJ*fabs(state[1] - currentPredictRect[1])/((double)cbt.lastPosition.height), 2);// +
//pow(DIST_ADJ*fabs(state[2] - currentPredictRect[2])/((double)cbt.lastPosition.width),2);
float penalityCBT = pow(dist_adj*fabs(state[0] - currentPredictRect[0])/((double)cbt.lastPosition.width),2) +
pow(dist_adj*fabs(state[1] - currentPredictRect[1])/((double)cbt.lastPosition.height), 2);// +
//pow(dist_adj*fabs(state[2] - currentPredictRect[2])/((double)cbt.lastPosition.width),2);
float uncertainty;
if(confidence != 0){
uncertainty = 1e-4*exp(-3.5*(0.9*confidence - penalityCBT));
uncertainty = 1e-4*exp(-3.5*(conf_adj*confidence - penalityCBT));
state.clear();
state.push_back(round((float)cbt.lastPosition.x + (float)cbt.lastPosition.width/2.0));
state.push_back(round((float)cbt.lastPosition.y + (float)cbt.lastPosition.height/2.0));
Expand Down
2 changes: 1 addition & 1 deletion trackers/tcbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class tCBT : public BTracker
{
public:
tCBT();
tCBT(float dist_adj = DIST_ADJ, float conf_adj = 0.9);

void run();

Expand Down
13 changes: 7 additions & 6 deletions trackers/tkcf.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "tkcf.h"

tKCF::tKCF()
tKCF::tKCF(float dist_adj, float conf_adj)
{

this->dist_adj = dist_adj;
this->conf_adj = conf_adj;
}

void tKCF::init(cv::Mat& image, cv::Rect region){
Expand Down Expand Up @@ -44,10 +45,10 @@ void tKCF::track(){
state.push_back(width);

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);
float uncertainty = 1e-4*exp(-3.5*(1.1*kcf.correlation - penalityKCF));
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);
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);
Expand Down
2 changes: 1 addition & 1 deletion trackers/tkcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class tKCF : public BTracker
{
public:
tKCF();
tKCF(float dist_adj = DIST_ADJ, float conf_adj = 1.1);

void run();

Expand Down
13 changes: 7 additions & 6 deletions trackers/tvdp.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "tvdp.h"

tVDP::tVDP()
tVDP::tVDP(float dist_adj, float conf_adj)
{

this->dist_adj = dist_adj;
this->conf_adj = conf_adj;
}

void tVDP::run(){
Expand Down Expand Up @@ -37,12 +38,12 @@ void tVDP::track(){
float newWidth = state[2]*scale;

this->stateUncertainty.clear();
float penalityCBT = pow(DIST_ADJ*fabs(state[0] - currentPredictRect[0])/((double)cbt.lastPosition.width),2) +
pow(DIST_ADJ*fabs(state[1] - currentPredictRect[1])/((double)cbt.lastPosition.height), 2);// +
//pow(DIST_ADJ*fabs(state[2] - currentPredictRect[2])/((double)cbt.lastPosition.width),2);
float penalityCBT = pow(dist_adj*fabs(state[0] - currentPredictRect[0])/((double)cbt.lastPosition.width),2) +
pow(dist_adj*fabs(state[1] - currentPredictRect[1])/((double)cbt.lastPosition.height), 2);// +
//pow(DIST_ADJ*fabs(state[2] - currentPredictRect[2])/((double)cbt.lastPosition.width),2);
float uncertainty;
if(confidence != 0){
uncertainty = 1e-4*exp(-3.5*(0.9*confidence - penalityCBT));
uncertainty = 1e-4*exp(-3.5*(conf_adj*confidence - penalityCBT));
state.clear();
state.push_back(round((float)cbt.lastPosition.x + (float)cbt.lastPosition.width/2.0));
state.push_back(round((float)cbt.lastPosition.y + (float)cbt.lastPosition.height/2.0));
Expand Down
2 changes: 1 addition & 1 deletion trackers/tvdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class tVDP : public BTracker
{
public:
tVDP();
tVDP(float dist_adj = DIST_ADJ, float conf_adj = 0.72);

void run();

Expand Down

0 comments on commit ea6b33d

Please sign in to comment.