diff --git a/marker.cpp b/marker.cpp index 1e750cd..82fc604 100644 --- a/marker.cpp +++ b/marker.cpp @@ -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()); } diff --git a/marker.h b/marker.h index abc9352..cd803ca 100644 --- a/marker.h +++ b/marker.h @@ -46,6 +46,11 @@ class Marker { */ Marker(Point2fVec &pts); + /** + * Copy constructor + */ + Marker(const Marker &other); + /** * Set the marker ID to . */ @@ -61,6 +66,11 @@ class Marker { */ Point2fVec getPoints() const { return points; } + /** + * Set the points in . + */ + void setPoints(const Point2fVec &pVec) { points.assign(pVec.begin(), pVec.end()); } + /** * Add a point

to the corner points. */ @@ -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]. diff --git a/ocv_ar.h b/ocv_ar.h index 07686f9..0dc0892 100644 --- a/ocv_ar.h +++ b/ocv_ar.h @@ -19,5 +19,6 @@ #include "conf.h" #include "types.h" #include "detect.h" +#include "track.h" #endif \ No newline at end of file diff --git a/track.cpp b/track.cpp new file mode 100644 index 0000000..d03572f --- /dev/null +++ b/track.cpp @@ -0,0 +1,91 @@ +/** + * ocv_ar - OpenCV based Augmented Reality library + * + * Tracker class -- implementation file. + * + * Author: Markus Konrad , 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 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::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::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(); +} \ No newline at end of file diff --git a/track.h b/track.h new file mode 100644 index 0000000..4b2d669 --- /dev/null +++ b/track.h @@ -0,0 +1,60 @@ +/** + * ocv_ar - OpenCV based Augmented Reality library + * + * Tracker class -- header file. + * + * Author: Markus Konrad , 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 + +#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 MarkerMap; +typedef std::pair 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 \ No newline at end of file diff --git a/types.h b/types.h index 93fd2cc..5002fd7 100644 --- a/types.h +++ b/types.h @@ -16,8 +16,6 @@ #include #include -#include -#include namespace ocv_ar { /**