Skip to content

Commit

Permalink
modified OpenTLD rc
Browse files Browse the repository at this point in the history
  • Loading branch information
IIUJ-MaksymPlencler committed Aug 6, 2013
1 parent 9e3f5b8 commit 620e3d5
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ OpenTLDConfig.cmake
TODO
cmake_install.cmake
*.a

build/
2 changes: 2 additions & 0 deletions src/libopentld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_library(libopentld
tld/ForegroundDetector.cpp
tld/MedianFlowTracker.cpp
tld/NNClassifier.cpp
tld/ShapeClassifier.cpp
tld/TLD.cpp
tld/TLDUtil.cpp
tld/VarianceFilter.cpp
Expand All @@ -33,6 +34,7 @@ add_library(libopentld
tld/DetectionResult.h
tld/DetectorCascade.h
tld/EnsembleClassifier.h
tld/ShapeClassifier.h
tld/ForegroundDetector.h
tld/IntegralImage.h
tld/MedianFlowTracker.h
Expand Down
4 changes: 4 additions & 0 deletions src/libopentld/tld/DetectionResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ DetectionResult::DetectionResult()
detectorBB = NULL;

variances = NULL;
//similarities = NULL;
posteriors = NULL;
featureVectors = NULL;
}
Expand All @@ -56,6 +57,7 @@ void DetectionResult::init(int numWindows, int numTrees)
{
variances = new float[numWindows];
posteriors = new float[numWindows];
//similarities = new float[numWindows];
featureVectors = new int[numWindows * numTrees];
confidentIndices = new vector<int>();

Expand All @@ -79,6 +81,8 @@ void DetectionResult::release()
fgList->clear();
delete[] variances;
variances = NULL;
//delete[] similarities;
//similarities = NULL;
delete[] posteriors;
posteriors = NULL;
delete[] featureVectors;
Expand Down
1 change: 1 addition & 0 deletions src/libopentld/tld/DetectionResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DetectionResult
std::vector<int>* confidentIndices;
int *featureVectors;
float *variances;
//float *similarities;
int numClusters;
cv::Rect *detectorBB; //Contains a valid result only if numClusters = 1

Expand Down
25 changes: 18 additions & 7 deletions src/libopentld/tld/DetectorCascade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
#include "DetectorCascade.h"

#include <algorithm>

#include <iostream>
#include "TLDUtil.h"

using namespace cv;


namespace tld
{

Expand Down Expand Up @@ -59,6 +60,7 @@ DetectorCascade::DetectorCascade()

foregroundDetector = new ForegroundDetector();
varianceFilter = new VarianceFilter();
shapeClassifier = new ShapeClassifier();
ensembleClassifier = new EnsembleClassifier();
nnClassifier = new NNClassifier();
clustering = new Clustering();
Expand All @@ -72,6 +74,7 @@ DetectorCascade::~DetectorCascade()

delete foregroundDetector;
delete varianceFilter;
delete shapeClassifier;
delete ensembleClassifier;
delete nnClassifier;
delete detectionResult;
Expand Down Expand Up @@ -100,12 +103,14 @@ void DetectorCascade::propagateMembers()
detectionResult->init(numWindows, numTrees);

varianceFilter->windowOffsets = windowOffsets;
shapeClassifier->windowOffsets = windowOffsets;
ensembleClassifier->windowOffsets = windowOffsets;
ensembleClassifier->imgWidthStep = imgWidthStep;
ensembleClassifier->numScales = numScales;
ensembleClassifier->scales = scales;
ensembleClassifier->numFeatures = numFeatures;
ensembleClassifier->numTrees = numTrees;
shapeClassifier->windows = windows;
nnClassifier->windows = windows;
clustering->windows = windows;
clustering->numWindows = numWindows;
Expand All @@ -114,6 +119,7 @@ void DetectorCascade::propagateMembers()

foregroundDetector->detectionResult = detectionResult;
varianceFilter->detectionResult = detectionResult;
shapeClassifier->detectionResult = detectionResult;
ensembleClassifier->detectionResult = detectionResult;
nnClassifier->detectionResult = detectionResult;
clustering->detectionResult = detectionResult;
Expand All @@ -129,6 +135,7 @@ void DetectorCascade::release()
initialised = false;

foregroundDetector->release();
shapeClassifier->release();
ensembleClassifier->release();
nnClassifier->release();

Expand Down Expand Up @@ -281,13 +288,13 @@ void DetectorCascade::detect(const Mat &img)
//Prepare components
foregroundDetector->nextIteration(img); //Calculates foreground
varianceFilter->nextIteration(img); //Calculates integral images
shapeClassifier->nextIteration(img);
ensembleClassifier->nextIteration(img);

#pragma omp parallel for

for(int i = 0; i < numWindows; i++)
{

int *window = &windows[TLD_WINDOW_SIZE * i];

if(foregroundDetector->isActive())
Expand Down Expand Up @@ -319,21 +326,25 @@ void DetectorCascade::detect(const Mat &img)
continue;
}

if(!ensembleClassifier->filter(i))
if(!ensembleClassifier->filter(i))
{
continue;
}

if(!shapeClassifier->filter(i))
{
continue;
}

if(!nnClassifier->filter(img, i))
{
continue;
}

detectionResult->confidentIndices->push_back(i);


}

//Cluster
clustering->clusterConfidentIndices();

Expand Down
2 changes: 2 additions & 0 deletions src/libopentld/tld/DetectorCascade.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "DetectionResult.h"
#include "ForegroundDetector.h"
#include "VarianceFilter.h"
#include "ShapeClassifier.h"
#include "EnsembleClassifier.h"
#include "Clustering.h"
#include "NNClassifier.h"
Expand Down Expand Up @@ -73,6 +74,7 @@ class DetectorCascade
//Components of Detector Cascade
ForegroundDetector *foregroundDetector;
VarianceFilter *varianceFilter;
ShapeClassifier *shapeClassifier;
EnsembleClassifier *ensembleClassifier;
Clustering *clustering;
NNClassifier *nnClassifier;
Expand Down
127 changes: 127 additions & 0 deletions src/libopentld/tld/ShapeClassifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* ShapeClassifier.h
*
* Created on: May 20, 2013
* Author: Maksym Plencler
*/
#include "ShapeClassifier.h"
#include "DetectorCascade.h"
#include "TLDUtil.h"
#include <iostream>

using namespace cv;

namespace tld
{

ShapeClassifier::ShapeClassifier(void)
{
this->img = NULL;
enabled = false;
similarityThreshold = 0.6f;
}

ShapeClassifier::~ShapeClassifier(void)
{
release();
}

void ShapeClassifier::release()
{
positivePatches->clear();
}

void ShapeClassifier::nextIteration(const Mat &img)
{
if(!enabled) return;

this->img = img;
}

bool ShapeClassifier::filter(int i)
{
if(!enabled) return true;

if(calcSimilarity(i) < similarityThreshold) {
//cout << "rejected" << endl;
return false;
}
return true;
}

float ShapeClassifier::calcSimilarity(int windowIdx)
{
NormalizedPatch patch;

int *bbox = &windows[TLD_WINDOW_SIZE * windowIdx];
tldExtractNormalizedPatchBB(img, bbox, patch.values);

return similarityWithModel(&patch);
}

float similarityOf(Mat patch1, Mat patch2)
{
Mat result;
absdiff(patch1, patch2, result);

float sum = 0;
unsigned char *imgData = (unsigned char *)result.data;

for(int i = 0; i < 15; i++)
{
for(int j = 0; j < 15; j++)
{
sum += imgData[j * result.step + i];
}
}
float res = 1 - (sum/(15*15))/255;
return res;
}

float ShapeClassifier::similarityWithModel(NormalizedPatch *patch)
{
if(positivePatches->empty())
{
return 0;
}

float similarity = 0;

Mat patchMat = Mat(15, 15, CV_8U, patch);
Canny(patchMat, patchMat, 10, 30, 3);
//int element_shape = CV_SHAPE_RECT;
//IplConvKernel* element = cvCreateStructuringElementEx(2+1, 2+1, 1, 1, element_shape);
dilate(patchMat, patchMat, Mat(), Point(-1,-1));
//imshow( "propozycja", patchMat );

Mat modelElemMat;
//cout << "PP: " << positivePatches->size() << endl;

for(size_t i = 0; i < positivePatches->size(); i++)
{
modelElemMat = Mat(15, 15, CV_8U, positivePatches->at(i).values);
Canny(patchMat, patchMat, 10, 30, 3);
dilate(patchMat, patchMat, Mat(), Point(-1,-1));

similarity = similarityOf(modelElemMat, patchMat);

if(similarity > similarityThreshold)
{
patchMat.release();
modelElemMat.release();
return similarity;
}
}

patchMat.release();
modelElemMat.release();
return similarity;
}


void ShapeClassifier::setPositivePatches(std::vector<NormalizedPatch>* pp)
{
positivePatches = pp;
}

} /* namespace tld */
46 changes: 46 additions & 0 deletions src/libopentld/tld/ShapeClassifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* ShapeClassifier.h
*
* Created on: May 20, 2013
* Author: Maksym Plencler
*/

#ifndef SHAPECLASSIFIER_H_
#define SHAPECLASSIFIER_H_

#include <opencv/cv.h>

#include "DetectionResult.h"
#include "NormalizedPatch.h"

namespace tld
{

class ShapeClassifier
{
std::vector<NormalizedPatch>* positivePatches;
cv::Mat img;


public:
bool enabled;
int *windows;
int *windowOffsets;

DetectionResult *detectionResult;

float similarityThreshold;

ShapeClassifier();
virtual ~ShapeClassifier();

void release();
void nextIteration(const cv::Mat &img);
bool filter(int idx);
float calcSimilarity(int windowIdx);
void setPositivePatches(std::vector<NormalizedPatch>* positivePatches);
float similarityWithModel(NormalizedPatch *patch);
};

} /* namespace tld */
#endif /* SHAPECLASSIFIER_H_ */
Loading

0 comments on commit 620e3d5

Please sign in to comment.