summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/data/settings.xml17
-rw-r--r--figgis_detect.layout16
-rwxr-xr-xsrc/boundary.cpp18
-rwxr-xr-xsrc/boundary.h7
-rwxr-xr-xsrc/testApp.cpp137
-rwxr-xr-xsrc/testApp.h23
6 files changed, 139 insertions, 79 deletions
diff --git a/bin/data/settings.xml b/bin/data/settings.xml
index b18ba0c..3fcd6ea 100755
--- a/bin/data/settings.xml
+++ b/bin/data/settings.xml
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<figgis volume="0.368627" threshold="175" invert="0" flip="1" />
+<figgis volume="0.282353" threshold="42" invert="0" flip="0" />
<boundaries>
<boundary filename="hook.wav">
- <point x="645.000000" y="40.000000"></point>
+ <point x="556.000000" y="28.000000"></point>
<point x="414.000000" y="53.000000"></point>
<point x="374.000000" y="150.000000"></point>
- <point x="458.000000" y="239.000000"></point>
- <point x="637.000000" y="224.000000"></point>
+ <point x="475.000000" y="247.000000"></point>
+ <point x="642.000000" y="233.000000"></point>
<point x="676.000000" y="157.000000"></point>
+ <point x="633.000000" y="60.000000"></point>
</boundary>
<boundary filename="pus.wav">
<point x="651.000000" y="343.000000"></point>
@@ -58,10 +59,10 @@
<point x="209.000000" y="202.000000"></point>
</boundary>
<boundary filename="friends.wav">
- <point x="184.000000" y="257.000000"></point>
- <point x="275.000000" y="369.000000"></point>
- <point x="269.000000" y="240.000000"></point>
- <point x="224.000000" y="222.000000"></point>
+ <point x="189.000000" y="260.000000"></point>
+ <point x="285.000000" y="365.000000"></point>
+ <point x="284.000000" y="317.000000"></point>
+ <point x="227.000000" y="212.000000"></point>
<point x="212.000000" y="246.000000"></point>
</boundary>
</boundaries>
diff --git a/figgis_detect.layout b/figgis_detect.layout
index ec2556a..52953b7 100644
--- a/figgis_detect.layout
+++ b/figgis_detect.layout
@@ -4,25 +4,25 @@
<File name="addons.make" open="0" top="0" tabpos="3">
<Cursor position="24" topLine="0" />
</File>
- <File name="bin/data/settings.xml" open="1" top="0" tabpos="5">
+ <File name="bin/data/settings.xml" open="0" top="0" tabpos="5">
<Cursor position="506" topLine="0" />
</File>
<File name="config.make" open="0" top="0" tabpos="1">
<Cursor position="376" topLine="18" />
</File>
- <File name="src/boundary.cpp" open="1" top="0" tabpos="4">
+ <File name="src/boundary.cpp" open="0" top="0" tabpos="4">
<Cursor position="1826" topLine="71" />
</File>
- <File name="src/boundary.h" open="1" top="0" tabpos="6">
+ <File name="src/boundary.h" open="0" top="0" tabpos="6">
<Cursor position="484" topLine="0" />
</File>
- <File name="src/main.cpp" open="1" top="0" tabpos="1">
+ <File name="src/main.cpp" open="0" top="0" tabpos="1">
<Cursor position="437" topLine="0" />
</File>
- <File name="src/testApp.cpp" open="1" top="1" tabpos="3">
- <Cursor position="3433" topLine="98" />
+ <File name="src/testApp.cpp" open="1" top="1" tabpos="0">
+ <Cursor position="2081" topLine="25" />
</File>
- <File name="src/testApp.h" open="1" top="0" tabpos="2">
- <Cursor position="1904" topLine="0" />
+ <File name="src/testApp.h" open="0" top="0" tabpos="2">
+ <Cursor position="1870" topLine="54" />
</File>
</CodeBlocks_layout_file>
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;
};