diff --git a/example/src/main.cpp b/example/src/main.cpp index e502a4d..7a15690 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -1,5 +1,5 @@ #include "ofMain.h" -#include "testApp.h" +#include "ofApp.h" #include "ofAppGlutWindow.h" //======================================================================== @@ -11,6 +11,6 @@ int main( ){ // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: - ofRunApp( new testApp()); + ofRunApp( new ofApp()); } diff --git a/example/src/testApp.cpp b/example/src/ofApp.cpp similarity index 85% rename from example/src/testApp.cpp rename to example/src/ofApp.cpp index aa8fd73..41bd8f6 100644 --- a/example/src/testApp.cpp +++ b/example/src/ofApp.cpp @@ -1,4 +1,4 @@ -#include "testApp.h" +#include "ofApp.h" #include "ofxCv.h" #include "ofBitmapFont.h" @@ -18,7 +18,7 @@ void drawMarker(float size, const ofColor & color){ } //-------------------------------------------------------------- -void testApp::setup(){ +void ofApp::setup(){ ofSetVerticalSync(true); useVideo = false; string boardName = "boardConfiguration.yml"; @@ -28,7 +28,7 @@ void testApp::setup(){ player.play(); video = &player; }else{ - grabber.setDeviceID(1); + //grabber.setDeviceID(1); grabber.initGrabber(640,480); video = &grabber; } @@ -50,7 +50,7 @@ void testApp::setup(){ } //-------------------------------------------------------------- -void testApp::update(){ +void ofApp::update(){ video->update(); if(video->isFrameNew()){ aruco.detectBoards(video->getPixelsRef()); @@ -58,7 +58,7 @@ void testApp::update(){ } //-------------------------------------------------------------- -void testApp::draw(){ +void ofApp::draw(){ ofSetColor(255); video->draw(0,0); @@ -96,7 +96,7 @@ void testApp::draw(){ } //-------------------------------------------------------------- -void testApp::keyPressed(int key){ +void ofApp::keyPressed(int key){ if(key=='m') showMarkers = !showMarkers; if(key=='b') showBoard = !showBoard; if(key=='i') showBoardImage = !showBoardImage; @@ -110,41 +110,41 @@ void testApp::keyPressed(int key){ } //-------------------------------------------------------------- -void testApp::keyReleased(int key){ +void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- -void testApp::mouseMoved(int x, int y ){ +void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- -void testApp::mouseDragged(int x, int y, int button){ +void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- -void testApp::mousePressed(int x, int y, int button){ +void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- -void testApp::mouseReleased(int x, int y, int button){ +void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- -void testApp::windowResized(int w, int h){ +void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- -void testApp::gotMessage(ofMessage msg){ +void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- -void testApp::dragEvent(ofDragInfo dragInfo){ +void ofApp::dragEvent(ofDragInfo dragInfo){ } diff --git a/example/src/testApp.h b/example/src/ofApp.h similarity index 95% rename from example/src/testApp.h rename to example/src/ofApp.h index b082d3a..e95f328 100644 --- a/example/src/testApp.h +++ b/example/src/ofApp.h @@ -3,7 +3,7 @@ #include "ofMain.h" #include "ofxAruco.h" -class testApp : public ofBaseApp{ +class ofApp : public ofBaseApp{ public: void setup(); diff --git a/src/ofxAruco.cpp b/src/ofxAruco.cpp index a42b757..8871bac 100644 --- a/src/ofxAruco.cpp +++ b/src/ofxAruco.cpp @@ -50,6 +50,7 @@ void ofxAruco::setupXML(string calibrationXML,float w, float h, string boardConf size.height = h; markerSize = _markerSize; detector.setThresholdMethod(aruco::MarkerDetector::ADPT_THRES); + maxAge = 7; camParams.readFromXMLFile(ofToDataPath(calibrationXML)); camParams.resize(cv::Size(w,h)); @@ -67,6 +68,23 @@ void ofxAruco::setupXML(string calibrationXML,float w, float h, string boardConf if(threaded) startThread(); } +//setup without calibration to work on 2d +void ofxAruco::setup2d(float w, float h, string boardConfigFile, float _markerSize){ + + size.width = w; + size.height = h; + markerSize = _markerSize; + + detector.setThresholdMethod(aruco::MarkerDetector::ADPT_THRES); + + maxAge = 7; + + // bgraf + addBoardConf(boardConfigFile); + + if(threaded) startThread(); +} + void ofxAruco::addBoardConf(string boardConfigFile) { if(boardConfigFile!="") { aruco::BoardConfiguration boardConfig; @@ -200,7 +218,43 @@ void ofxAruco::findBoards(ofPixels & pixels){ } } -void ofxAruco::draw(){ +void ofxAruco::draw() +{ + if ( camParams.isValid() ) + draw3d(); + else draw2d(); +} + +void ofxAruco::draw2d() +{ + ofSetColor(255,0,0); + + for( int i = 0; i < markers.size(); i++ ) + { + cv::Point2f p0,p1; + ofPoint ctr(0,0); + + for ( int j = 0; j < 4; j++ ) + { + p0 = markers[i][j]; + p1 = markers[i][ (j+1)%4 ]; + + ofLine( p0.x, p0.y, p1.x, p1.y ); + + ctr.x += p0.x; + ctr.y += p0.y; + } + + ctr.x /= 4.; + ctr.y /= 4.; + + ofDrawBitmapString( ofToString(markers[i].idMarker), ctr ); + } + + ofSetColor(255); +} + +void ofxAruco::draw3d(){ glMatrixMode( GL_PROJECTION ); glPushMatrix(); @@ -266,7 +320,14 @@ vector ofxAruco::getBoardProbabilities() { return boardProbabilities; } -void ofxAruco::begin(int marker){ +void ofxAruco::begin(int marker) +{ + if ( ! camParams.isValid() ) + { + ofLogError("ofxAruco::begin add some camera parameters on setup to use this method"); + return; + } + glMatrixMode( GL_PROJECTION ); glPushMatrix(); @@ -296,6 +357,13 @@ void ofxAruco::begin(int marker){ } void ofxAruco::end(){ + + if ( ! camParams.isValid() ) + { + ofLogError("ofxAruco::end add some camera parameters on setup to use this method"); + return; + } + glMatrixMode( GL_MODELVIEW ); glPopMatrix(); diff --git a/src/ofxAruco.h b/src/ofxAruco.h index fe472ee..956ba9f 100644 --- a/src/ofxAruco.h +++ b/src/ofxAruco.h @@ -24,14 +24,17 @@ class ofxAruco: public ofThread { void setThreaded(bool threaded); // defaults to true void setup(string calibrationFile,float w, float h, string boardConfig="", float markerSize=.15); void setupXML(string calibrationXML,float w, float h, string boardConfig="", float markerSize=.15); + void setup2d(float w, float h, string boardConfig="", float markerSize=.15); void addBoardConf(string boardConfig=""); void detectMarkers(ofPixels & pixels); void detectBoards(ofPixels & pixels); - + void draw(); - + void draw3d(); + void draw2d(); + vector & getMarkers(); // bgraf // aruco::Board & getBoard(); @@ -106,8 +109,7 @@ class ofxAruco: public ofThread { double projMatrix[16]; float projfMatrix[16]; - ofMatrix4x4 ofprojMatrix; - + ofMatrix4x4 ofprojMatrix; aruco::Marker * findMarker(int id); TrackedMarker * findTrackedMarker(int id);