summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2013-01-29 17:23:14 +0000
committerComment <tim@gray.(none)>2013-01-29 17:23:14 +0000
commited539c88af8e18ae3b505e324319b956944e0d91 (patch)
tree3c35b84658c3ff057923ca87daca53ba14665afe /src
parentba37049c44ff64b3c6482a5e2f199f178351a69f (diff)
working with accumulation
Diffstat (limited to 'src')
-rwxr-xr-xsrc/boundary.cpp18
-rwxr-xr-xsrc/boundary.h7
-rwxr-xr-xsrc/testApp.cpp137
-rwxr-xr-xsrc/testApp.h23
4 files changed, 122 insertions, 63 deletions
diff --git a/src/boundary.cpp b/src/boundary.cpp
index 2425c4a..3e5b18c 100755
--- a/src/boundary.cpp
+++ b/src/boundary.cpp
@@ -37,13 +37,15 @@ void boundary::undo(){
}
void boundary::getCentroid(){
- float x=0;
float y=0;
+ float maxx=-1;
+ float minx=100000;
for (int i=0;i<points.size();i++) {
- x+=points[i].x;
y+=points[i].y;
+ minx=min(minx,points[i].x);
+ maxx=max(maxx,points[i].x);
}
- centroid=ofPoint(x/points.size(),y/points.size(),0);
+ centroid=ofPoint(minx+((maxx-minx)*0.1),y/points.size(),0);
}
bool boundary::contains(ofPoint p)
@@ -77,6 +79,16 @@ bool boundary::contains(ofPoint p)
return false;
else
return true;
+}
+int boundary::findPoint(ofPoint pos){
+ int threshold=6;
+ int selected=-1;
+ for (int i=0;i<points.size();i++) {
+ if (abs(points[i].x-pos.x)<=threshold&&abs(points[i].x-pos.x)<=threshold) {
+ selected=i;
+ }
+ }
+ return selected;
}
void boundary::openFile(string file) {
filename=file;
diff --git a/src/boundary.h b/src/boundary.h
index bf0b780..e3eb011 100755
--- a/src/boundary.h
+++ b/src/boundary.h
@@ -16,13 +16,14 @@ class boundary
void add(ofPoint p);
void undo();
void getCentroid();
- vector<ofPoint> points;
- ofPoint centroid;
+ vector<ofPoint> points;
+ int findPoint(ofPoint pos);
bool checkClick(ofPoint pos);
bool checkFile(ofPoint pos,string file);
void openFile(string file);
void setVolume(float vol);
-
+
+ ofPoint centroid;
string filename;
ofSoundPlayer sound;
};
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;
}
diff --git a/src/testApp.h b/src/testApp.h
index bea746c..c8db7f2 100755
--- a/src/testApp.h
+++ b/src/testApp.h
@@ -8,9 +8,12 @@
#include "ofxFensterManager.h"
#include "ofxGui.h"
-#define COLOUR 1
-#define GREY 2
-#define DIFF 3
+#define DISPLAY_FG 1
+#define DISPLAY_BG 2
+#define DISPLAY_DIFF 3
+
+#define METHOD_SIMPLE 4
+#define METHOD_ACCUM 5
//12-10-12
//each area will trigger 'once and once only' ?
@@ -20,6 +23,10 @@
//tweak opencv stuff
//test performance
+//size factor
+//continuous background
+//abs diff?
+
class guiWindow;
@@ -49,16 +56,18 @@ class testApp : public ofxFensterListener{
ofVideoGrabber vidGrabber;
int gw,gh;
+
+ float learningRate,startTime;
- ofxCvColorImage colorImg;
-
+ cv::Mat accumulator,outmat;
+ ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
- int mode;
+ int mode,method;
bool bLearnBakground;
bool flip,invert;
@@ -80,11 +89,13 @@ class testApp : public ofxFensterListener{
// ie:
// ofAddListener(addon.newIntEvent, this, &Class::method)
void volumeChanged(float &v);
+ void resetBG(bool &chg);
bool bGrab;
vector<boundary> boundaries;
int selectedBoundary;
+ int selectedPoint;
vector<ofPoint> clicks;
};