-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tracker 'Track' with basic logic
- Loading branch information
Showing
6 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ | |
#include "conf.h" | ||
#include "types.h" | ||
#include "detect.h" | ||
#include "track.h" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters