diff --git a/detect.cpp b/detect.cpp index f5bdbbe..e9c78f0 100644 --- a/detect.cpp +++ b/detect.cpp @@ -16,6 +16,7 @@ #include "detect.h" #include "tools.h" +#include "threading.h" using namespace ocv_ar; @@ -229,12 +230,12 @@ void Detect::processFrame(bool enableMutex) { performThreshold(); findContours(); - if (enableMutex) lockMarkers(); + if (enableMutex) Threading::mutexLock(); findMarkerCandidates(); identifyMarkers(); - if (enableMutex) unlockMarkers(); +// if (enableMutex) unlockMarkers(); } void Detect::estimateMarkersPoses() { @@ -267,15 +268,6 @@ cv::Mat *Detect::getOutputFrame() const { #pragma mark private methods -void Detect::lockMarkers() { - while (markersLocked) {}; - markersLocked = true; -} - -void Detect::unlockMarkers() { - markersLocked = false; -} - void Detect::preprocess() { // downscale the image diff --git a/detect.h b/detect.h index a1d4f1b..1e3d4f3 100644 --- a/detect.h +++ b/detect.h @@ -59,9 +59,6 @@ class Detect { */ ~Detect(); - void lockMarkers(); - void unlockMarkers(); - /** * Prepare for input frames of size x with * color channels. Specify a conversion type for grayscale conversion of @@ -201,8 +198,6 @@ class Detect { bool prepared; // detector is prepared ( called)? - bool markersLocked; // mutex variable to lock markers vector access - int inputFrameCvtType; // color conversion type for input frames FrameProcLevel outFrameProcLvl; // frame output processing level diff --git a/threading.cpp b/threading.cpp new file mode 100644 index 0000000..dd86a61 --- /dev/null +++ b/threading.cpp @@ -0,0 +1,28 @@ +/** + * ocv_ar - OpenCV based Augmented Reality library + * + * Helper function class for threading -- implementation file. + * + * Author: Markus Konrad , July 2014. + * INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/ + * + * See LICENSE for license. + */ + +#include "threading.h" + +using namespace ocv_ar; + +pthread_mutex_t Threading::mutex; + +void Threading::init() { + pthread_mutex_init (&mutex, NULL); +} + +void Threading::mutexLock() { + pthread_mutex_lock(&mutex); +} + +void Threading::mutexUnlock() { + pthread_mutex_unlock(&mutex); +} \ No newline at end of file diff --git a/threading.h b/threading.h new file mode 100644 index 0000000..b5e3689 --- /dev/null +++ b/threading.h @@ -0,0 +1,32 @@ +/** + * ocv_ar - OpenCV based Augmented Reality library + * + * Helper function class for threading -- header file. + * + * Author: Markus Konrad , July 2014. + * INKA Research Group, HTW Berlin - http://inka.htw-berlin.de/ + * + * See LICENSE for license. + */ + +#ifndef OCV_AR_THREADING_H +#define OCV_AR_THREADING_H + +#include + +namespace ocv_ar { + +class Threading { +public: + static void init(); + + static void mutexLock(); + static void mutexUnlock(); + +private: + static pthread_mutex_t mutex; +}; + +} + +#endif \ No newline at end of file diff --git a/track.cpp b/track.cpp index 80c5ffa..6d3b53b 100644 --- a/track.cpp +++ b/track.cpp @@ -12,6 +12,7 @@ #include "track.h" #include "tools.h" +#include "threading.h" using namespace ocv_ar; @@ -29,17 +30,14 @@ void Track::detect(const cv::Mat *frame) { // lockMarkers(); // lock markers map // detect and identify the markers - detector->processFrame(true); + detector->processFrame(true); // will call Threading::mutexLock at the correct spot // correct the vertices of the found markers - lockMarkers(); - detector->lockMarkers(); correctMarkerVertexOrder(detector->getMarkers()); // estimate the markers' 3D poses detector->estimateMarkersPoses(); - detector->unlockMarkers(); unlockMarkers(); // unlockMarkers(); // lock markers map @@ -47,12 +45,11 @@ void Track::detect(const cv::Mat *frame) { } void Track::lockMarkers() { - while (markersLocked) {}; - markersLocked = true; + Threading::mutexLock(); } void Track::unlockMarkers() { - markersLocked = false; + Threading::mutexUnlock(); } void Track::update() { diff --git a/track.h b/track.h index ef376db..714bbb4 100644 --- a/track.h +++ b/track.h @@ -40,7 +40,6 @@ typedef std::pair MarkerMapPair; class Track { public: Track(Detect *detectorPtr) : detector(detectorPtr), - markersLocked(false), detectionRunning(false) {}; @@ -58,7 +57,6 @@ class Track { MarkerMap markers; - bool markersLocked; bool detectionRunning;