summaryrefslogtreecommitdiff
path: root/gaunt01
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-03-21 00:14:05 +0000
committerTim Redfern <tim@eclectronics.org>2012-03-21 00:14:05 +0000
commitd1324e15200f944fc6d9ea7e0bf60ded109f6d29 (patch)
tree713c885bb7e2536dd1978c655ac5860b241c41d5 /gaunt01
initial commit
Diffstat (limited to 'gaunt01')
-rw-r--r--gaunt01/src/main.cpp16
-rw-r--r--gaunt01/src/testApp.cpp176
-rw-r--r--gaunt01/src/testApp.h48
3 files changed, 240 insertions, 0 deletions
diff --git a/gaunt01/src/main.cpp b/gaunt01/src/main.cpp
new file mode 100644
index 0000000..6a32c6a
--- /dev/null
+++ b/gaunt01/src/main.cpp
@@ -0,0 +1,16 @@
+#include "ofMain.h"
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+//========================================================================
+int main( ){
+
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context
+
+ // this kicks off the running of my app
+ // can be OF_WINDOW or OF_FULLSCREEN
+ // pass in width and height too:
+ ofRunApp( new testApp());
+
+}
diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp
new file mode 100644
index 0000000..fdff756
--- /dev/null
+++ b/gaunt01/src/testApp.cpp
@@ -0,0 +1,176 @@
+#include "testApp.h"
+
+//--------------------------------------------------------------
+void testApp::setup(){
+
+ #ifdef _USE_LIVE_VIDEO
+ vidGrabber.setVerbose(true);
+ vidGrabber.initGrabber(320,240);
+ #else
+ vidPlayer.loadMovie("camoutput.mov");
+ vidPlayer.play();
+ #endif
+
+ colorImg.allocate(320,240);
+ grayImage.allocate(320,240);
+ grayBg.allocate(320,240);
+ grayDiff.allocate(320,240);
+
+ bLearnBakground = true;
+ threshold = 80;
+}
+
+//--------------------------------------------------------------
+void testApp::update(){
+ ofBackground(100,100,100);
+
+ bool bNewFrame = false;
+
+ #ifdef _USE_LIVE_VIDEO
+ vidGrabber.grabFrame();
+ bNewFrame = vidGrabber.isFrameNew();
+ #else
+ vidPlayer.idleMovie();
+ bNewFrame = vidPlayer.isFrameNew();
+ #endif
+
+ if (bNewFrame){
+
+ #ifdef _USE_LIVE_VIDEO
+ colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
+ #else
+ colorImg.setFromPixels(vidPlayer.getPixels(), 320,240);
+ #endif
+
+ grayImage = colorImg;
+ if (bLearnBakground == true){
+ grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
+ bLearnBakground = false;
+ }
+
+ // take the abs value of the difference between background and incoming and then threshold:
+ grayDiff.absDiff(grayBg, grayImage);
+ grayDiff.threshold(threshold);
+
+ // find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
+ // also, find holes is set to true so we will get interior contours as well....
+ contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true); // find holes
+ }
+
+}
+
+//--------------------------------------------------------------
+void testApp::draw(){
+
+ // draw the incoming, the grayscale, the bg and the thresholded difference
+ ofSetHexColor(0xffffff);
+ colorImg.draw(20,20);
+ grayImage.draw(360,20);
+ grayBg.draw(20,280);
+ grayDiff.draw(360,280);
+
+ // then draw the contours:
+
+ ofFill();
+ ofSetHexColor(0x333333);
+ ofRect(360,540,320,240);
+ ofSetHexColor(0xffffff);
+
+ // we could draw the whole contour finder
+ //contourFinder.draw(360,540);
+
+ // or, instead we can draw each blob individually,
+ // this is how to get access to them:
+ for (int i = 0; i < contourFinder.nBlobs; i++){
+ contourFinder.blobs[i].draw(360,540);
+ }
+
+ // finally, a report:
+
+ ofSetHexColor(0xffffff);
+ char reportStr[1024];
+ sprintf(reportStr, "bg subtraction and blob detection\npress ' ' to capture bg\nthreshold %i (press: +/-)\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
+ ofDrawBitmapString(reportStr, 20, 600);
+
+ float gap=ofGetHeight()-ofGetWidth();
+
+ ofTranslate(0,ofGetHeight(),0);
+ ofRotate(cam_angle,1,0,0);
+ ofTranslate(0,-ofGetHeight(),0);
+
+ for (float i=0;i<ofGetWidth();i+=ofGetWidth()/10) {
+ glBegin(GL_LINES);
+ glVertex3f(i,gap,0);
+ glVertex3f(i,ofGetHeight(),0);
+ glEnd();
+ glBegin(GL_LINES);
+ glVertex3f(0,i+gap,0);
+ glVertex3f(ofGetWidth(),i+gap,0);
+ glEnd();
+ }
+
+}
+
+//--------------------------------------------------------------
+void testApp::keyPressed(int key){
+
+ switch (key){
+ case ' ':
+ bLearnBakground = true;
+ break;
+ case '+':
+ threshold ++;
+ if (threshold > 255) threshold = 255;
+ break;
+ case '-':
+ threshold --;
+ if (threshold < 0) threshold = 0;
+ break;
+ case 'a':
+ cam_angle+=1;
+ break;
+ case 'z':
+ cam_angle-=1;
+ break;
+ }
+}
+
+//--------------------------------------------------------------
+void testApp::keyReleased(int key){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseMoved(int x, int y ){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseDragged(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mousePressed(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseReleased(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::windowResized(int w, int h){
+
+}
+
+//--------------------------------------------------------------
+void testApp::gotMessage(ofMessage msg){
+
+}
+
+//--------------------------------------------------------------
+void testApp::dragEvent(ofDragInfo dragInfo){
+
+}
diff --git a/gaunt01/src/testApp.h b/gaunt01/src/testApp.h
new file mode 100644
index 0000000..eafbf00
--- /dev/null
+++ b/gaunt01/src/testApp.h
@@ -0,0 +1,48 @@
+#pragma once
+
+#include "ofMain.h"
+
+#include "ofxOpenCv.h"
+
+//#define _USE_LIVE_VIDEO // uncomment this to use a live camera
+ // otherwise, we'll use a movie file
+
+class testApp : public ofBaseApp{
+
+ public:
+ void setup();
+ void update();
+ void draw();
+
+ void keyPressed(int key);
+ void keyReleased(int key);
+ void mouseMoved(int x, int y );
+ void mouseDragged(int x, int y, int button);
+ void mousePressed(int x, int y, int button);
+ void mouseReleased(int x, int y, int button);
+ void windowResized(int w, int h);
+ void dragEvent(ofDragInfo dragInfo);
+ void gotMessage(ofMessage msg);
+
+ #ifdef _USE_LIVE_VIDEO
+ ofVideoGrabber vidGrabber;
+ #else
+ ofVideoPlayer vidPlayer;
+ #endif
+
+ ofxCvColorImage colorImg;
+
+ ofxCvGrayscaleImage grayImage;
+ ofxCvGrayscaleImage grayBg;
+ ofxCvGrayscaleImage grayDiff;
+
+ ofxCvContourFinder contourFinder;
+
+ int threshold;
+ bool bLearnBakground;
+
+ float cam_angle;
+
+
+};
+