summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/testApp.cpp')
-rwxr-xr-xsrc/testApp.cpp137
1 files changed, 86 insertions, 51 deletions
diff --git a/src/testApp.cpp b/src/testApp.cpp
index 0392c92..5928ee5 100755
--- a/src/testApp.cpp
+++ b/src/testApp.cpp
@@ -18,15 +18,17 @@ void testApp::setup(){
gh=576;
vidGrabber.setVerbose(true);
- bGrab=false;
- //vidGrabber.initGrabber(gw,gh); //base grab size
+ bGrab=vidGrabber.initGrabber(gw,gh); //base grab size
+
+ learningRate = 0.01f;
colorImg.allocate(gw,gh);
grayImage.allocate(gw,gh);
grayBg.allocate(gw,gh);
grayDiff.allocate(gw,gh);
- mode=COLOUR;
+ mode=DISPLAY_FG;
+ method=METHOD_SIMPLE;
bLearnBakground = true;
@@ -34,6 +36,7 @@ void testApp::setup(){
boundaries.push_back(boundary());
}
selectedBoundary=0;
+ selectedPoint=-1;
guiWin=new guiWindow();
ofxFenster* win=ofxFensterManager::get()->createFenster(0, 0, 200, 200, OF_WINDOW);
@@ -45,44 +48,61 @@ void testApp::setup(){
gui.add(vol.setup("volume",volume,0,1.0,255));
vol.addListener(this,&testApp::volumeChanged);
gui.add(bFlip.setup("flip grab", flip));
+ bFlip.addListener(this,&testApp::resetBG);
gui.add(bInvert.setup("invert grab", invert));
+ bInvert.addListener(this,&testApp::resetBG);
gui.add(thresh.setup("threshold",threshold,0,255,255));
win->setWindowTitle("config");
win->addListener(guiWin);
guiWin->setup();
guiWin->setParent(this);
-
-
}
//--------------------------------------------------------------
void testApp::update(){
if (bGrab) {
vidGrabber.grabFrame();
- bool bNewFrame = vidGrabber.isFrameNew();
- if (bNewFrame){
- colorImg.setFromPixels(vidGrabber.getPixels(), gw,gh);
- if (bFlip) colorImg.mirror(false,true);
- grayImage = colorImg;
- if (bInvert) grayImage.invert();
- if (bLearnBakground == true){
- grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
- bLearnBakground = false;
- }
- grayDiff.absDiff(grayBg, grayImage);
- grayDiff.threshold(threshold);
- contourFinder.findContours(grayDiff, 20, (gw*gh)/3, 10, true); // find holes
- }
-
- //generate events
-
- for (int i = 0; i < contourFinder.nBlobs; i++){
- for (int j=0;j<boundaries.size();j++) {
- if (boundaries[j].checkClick(contourFinder.blobs[i].centroid)) {
- printf("playing %i: %s\n",boundaries[j].filename.c_str(),j);
- }
- }
- }
+ bool bNewFrame = vidGrabber.isFrameNew();
+ if (bNewFrame){
+ colorImg.setFromPixels(vidGrabber.getPixels(), gw,gh);
+ if (bFlip) colorImg.mirror(false,true);
+ grayImage = colorImg;
+ if (bInvert) grayImage.invert();
+ switch(method) {
+ case METHOD_SIMPLE:
+ if (bLearnBakground){
+ grayBg = grayImage;
+ bLearnBakground = false;
+ }
+ break;
+ case METHOD_ACCUM:
+ cv::Mat img = grayImage.getCvImage();
+ //printf("img: %ix%i %i ch x %i bits\n",img.size().width,img.size().height,img.channels(),img.depth());
+ if (bLearnBakground) {
+ img.convertTo(accumulator, CV_32FC1);
+ startTime=ofGetElapsedTimef();
+ bLearnBakground=false;
+ }
+ cv::Mat im3;
+ img.convertTo(im3, CV_32FC1);
+ //printf("accumulator: %ix%i %i ch x %i bits\n",accumulator.size().width,accumulator.size().height,accumulator.channels(),accumulator.depth());
+ //printf("im3: %ix%i %i ch x %i bits\n",im3.size().width,im3.size().height,im3.channels(),im3.depth());
+ accumulateWeighted(im3, accumulator, max(1.0f/((ofGetElapsedTimef()-startTime)*30.0f),learningRate));
+ accumulator.convertTo(outmat,CV_8UC1);
+ grayBg=new IplImage(outmat);
+ }
+ grayDiff.absDiff(grayBg, grayImage);
+ grayDiff.threshold(threshold);
+ contourFinder.findContours(grayDiff, 20, (gw*gh)/3, 10, true); // find holes
+ }
+ //generate events
+ for (int i = 0; i < contourFinder.nBlobs; i++){
+ for (int j=0;j<boundaries.size();j++) {
+ if (boundaries[j].checkClick(contourFinder.blobs[i].centroid)) {
+ //printf("playing %i: %s\n",boundaries[j].filename.c_str(),j);
+ }
+ }
+ }
}
}
@@ -92,21 +112,21 @@ void testApp::draw(){
//ofBackground(0,0,0);
ofSetColor(255,255,255);
if (bGrab) {
- switch(mode){
- case COLOUR:
- colorImg.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
- break;
- case GREY:
- grayImage.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
- break;
- case DIFF:
- grayDiff.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
- break;
- }
- }
-
- for (int i = 0; i < contourFinder.nBlobs; i++){
- contourFinder.blobs[i].draw(0,0); //(ofGetHeight()-gh)/2);
+ switch(mode){
+ case DISPLAY_FG:
+ grayImage.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
+ break;
+ case DISPLAY_BG:
+ grayBg.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
+ break;
+ case DISPLAY_DIFF:
+ grayDiff.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
+ break;
+ }
+
+ for (int i = 0; i < contourFinder.nBlobs; i++){
+ contourFinder.blobs[i].draw(0,0); //(ofGetHeight()-gh)/2);
+ }
}
for (int i=0;i<boundaries.size();i++) {
@@ -135,10 +155,10 @@ void testApp::keyPressed(int key){
//keys to play/stop (test)
//overall sound level control
//fine tune image detection
-
+ bool T=true;
switch (key){
case ' ':
- bLearnBakground = true;
+ resetBG(T);
break;
case '+':
threshold ++;
@@ -150,9 +170,11 @@ void testApp::keyPressed(int key){
break;
case 'i':
bInvert=!bInvert;
+ resetBG(T);
break;
case 'f':
bFlip=!bFlip;
+ resetBG(T);
break;
case 's':
saveSettings("settings.xml");
@@ -188,10 +210,17 @@ void testApp::keyPressed(int key){
case '3':
mode=key-48;
break;
+ case '4':
+ case '5':
+ method=key-48;
+ bLearnBakground=true;
+ break;
}
}
-
+void testApp::resetBG(bool &chg){
+ bLearnBakground = true;
+}
void testApp::volumeChanged(float &v){
for (int i=0;i<boundaries.size();i++) boundaries[i].setVolume(v);
}
@@ -207,23 +236,29 @@ void testApp::mouseMoved(int x, int y ){
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
-
+ if (selectedPoint>-1) boundaries[selectedBoundary].points[selectedPoint]=ofPoint(x,y,0);
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
+ ofPoint p=ofPoint(x,y,0);
switch (button) {
case 0:
- boundaries[selectedBoundary].add(ofPoint(x,y,0));
+ selectedPoint=boundaries[selectedBoundary].findPoint(p);
+ if (selectedPoint<0) {
+ selectedPoint=boundaries[selectedBoundary].points.size();
+ boundaries[selectedBoundary].add(p);
+ }
+ else boundaries[selectedBoundary].points[selectedPoint]=p;
break;
case 1:
for (int i=0;i<boundaries.size();i++) {
- if (boundaries[i].checkClick(ofPoint(x,y,0))) printf("playing %i: %s\n",i,boundaries[i].filename.c_str());
+ if (boundaries[i].checkClick(p)) printf("playing %i: %s\n",i,boundaries[i].filename.c_str());
}
break;
case 2:
for (int i=0;i<boundaries.size();i++) {
- if (boundaries[i].contains(ofPoint(x,y,0))) selectedBoundary=i;
+ if (boundaries[i].contains(p)) selectedBoundary=i;
}
break;
}