Skip to content

Commit

Permalink
added tracker 'Track' with basic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
internaut committed Jun 26, 2014
1 parent f51fd1b commit 0e7d7a8
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 2 deletions.
8 changes: 8 additions & 0 deletions marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ Marker::Marker(Point2fVec &pts) {
init();
}

Marker::Marker(const Marker &other) {
setPoints(other.getPoints());
init();

id = other.getId();
updatePoseMat(other.getRVec(), other.getTVec());
}

void Marker::rotatePoints(int rot) {
rotate(points.begin(), points.begin() + 4 - rot, points.end());
}
Expand Down
12 changes: 12 additions & 0 deletions marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Marker {
*/
Marker(Point2fVec &pts);

/**
* Copy constructor
*/
Marker(const Marker &other);

/**
* Set the marker ID to <newId>.
*/
Expand All @@ -61,6 +66,11 @@ class Marker {
*/
Point2fVec getPoints() const { return points; }

/**
* Set the points in <pVec>.
*/
void setPoints(const Point2fVec &pVec) { points.assign(pVec.begin(), pVec.end()); }

/**
* Add a point <p> to the corner points.
*/
Expand Down Expand Up @@ -101,6 +111,8 @@ class Marker {
*/
void updatePoseMat(const cv::Mat &r, const cv::Mat &t);

// void updateFromOtherMarker(const Marker *otherMrk);

/**
* Return the 4x4 OpenGL 3D pose model-view matrix as pointer to
* the internal float[16].
Expand Down
1 change: 1 addition & 0 deletions ocv_ar.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#include "conf.h"
#include "types.h"
#include "detect.h"
#include "track.h"

#endif
91 changes: 91 additions & 0 deletions track.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* ocv_ar - OpenCV based Augmented Reality library
*
* Tracker class -- implementation file.
*
* Author: Markus Konrad <konrad@htw-berlin.de>, June 2014.
* INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/
*
* See LICENSE for license.
*/

#include "track.h"

using namespace ocv_ar;

void Track::detect(const cv::Mat *frame) {
assert(detector && frame);
detector->setInputFrame(frame, frame->channels() == 1);

detector->processFrame();
}

void Track::lockMarkers() {
while (markersLocked) {};
markersLocked = true;
}

void Track::unlockMarkers() {
markersLocked = false;
}

void Track::update() {
// get the vector of detected markers
vector<Marker *> newMarkers = detector->getMarkers();

// update already existing markers
lockMarkers();
for (MarkerMap::iterator it = markers.begin();
it != markers.end();
)
{
int existingMrkId = it->first;
Marker *existingMrk = &it->second;

bool markerUpdated = false;

// try to find a matching marker in the "new markers" vector
for (vector<Marker *>::const_iterator newMrkIt = newMarkers.begin();
newMrkIt != newMarkers.end();
)
{
const Marker *newMrk = *newMrkIt;
if (existingMrkId == newMrk->getId()) { // we found a matching marker
// printf("ocv_ar::Track - updating marker %d\n", existingMrkId);

// update the existing marker with the information of the "new" marker
existingMrk->updatePoseMat(newMrk->getRVec(), newMrk->getTVec());

// delete this marker from the vector
newMrkIt = newMarkers.erase(newMrkIt);

// set status
markerUpdated = true;
break;
} else {
++newMrkIt; // advance
}
}

if (!markerUpdated) { // we could not find this marker in the "new markes" vector ...
// ... remove it!
printf("ocv_ar::Track - lost marker %d\n", existingMrkId);
markers.erase(it++); // safe map item delete
} else {
++it;
}
}

// add new markers
for (vector<Marker *>::const_iterator newMrkIt = newMarkers.begin();
newMrkIt != newMarkers.end();
++newMrkIt)
{
const Marker *newMrk = *newMrkIt;
MarkerMapPair newMrkPair(newMrk->getId(), Marker(*newMrk));
markers.insert(newMrkPair);
printf("ocv_ar::Track - added new marker %d\n", newMrk->getId());
}

unlockMarkers();
}
60 changes: 60 additions & 0 deletions track.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* ocv_ar - OpenCV based Augmented Reality library
*
* Tracker class -- header file.
*
* Author: Markus Konrad <konrad@htw-berlin.de>, June 2014.
* INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/
*
* See LICENSE for license.
*/

#ifndef OCV_AR_TRACK_H
#define OCV_AR_TRACK_H

#include <opencv2/core/core.hpp>

#include "types.h"
#include "detect.h"

namespace ocv_ar {

/**
* Marker map. Holds one marker mapped to a marker id.
* Note that this means that only unique markers in a
* scene can be tracked for now!
*/
typedef std::map<int, Marker> MarkerMap;
typedef std::pair<int, Marker> MarkerMapPair;

/**
* Marker tracker.
*
* Note that that only unique markers in a scene can be tracked for now!
* This means that there should not be two or more markers with the same id
* in the scene.
*/
class Track {
public:
Track(Detect *detectorPtr) : detector(detectorPtr), markersLocked(false)
{};

void detect(const cv::Mat *frame);

void update();

void lockMarkers();
void unlockMarkers();

const MarkerMap *getMarkers() const { return &markers; }

private:
MarkerMap markers;
bool markersLocked;

Detect *detector; // weak ref to Detect object
};

}

#endif
2 changes: 0 additions & 2 deletions types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <map>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>

namespace ocv_ar {
/**
Expand Down

0 comments on commit 0e7d7a8

Please sign in to comment.