#include "ofApp.h"
void ofApp::setup(){
#ifdef _USE_LIVE_VIDEO
vidGrabber.setVerbose(true);
vidGrabber.setup(320,240);
#else
vidPlayer.load("fingers.mov");
vidPlayer.play();
vidPlayer.setLoopState(OF_LOOP_NORMAL);
#endif
colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);
bLearnBakground = true;
threshold = 80;
}
void ofApp::update(){
ofBackground(100,100,100);
bool bNewFrame = false;
#ifdef _USE_LIVE_VIDEO
vidGrabber.update();
bNewFrame = vidGrabber.isFrameNew();
#else
vidPlayer.update();
bNewFrame = vidPlayer.isFrameNew();
#endif
if (bNewFrame){
#ifdef _USE_LIVE_VIDEO
colorImg.setFromPixels(vidGrabber.getPixels());
#else
colorImg.setFromPixels(vidPlayer.getPixels());
#endif
grayImage = colorImg;
if (bLearnBakground == true){
grayBg = grayImage;
bLearnBakground = false;
}
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);
}
}
void ofApp::draw(){
ofSetHexColor(0xffffff);
colorImg.draw(20,20);
grayImage.draw(360,20);
grayBg.draw(20,280);
grayDiff.draw(360,280);
ofFill();
ofSetHexColor(0x333333);
ofDrawRectangle(360,540,320,240);
ofSetHexColor(0xffffff);
for (int i = 0; i < contourFinder.nBlobs; i++){
contourFinder.blobs[i].draw(360,540);
ofSetColor(255);
if(contourFinder.blobs[i].hole){
ofDrawBitmapString("hole",
contourFinder.blobs[i].boundingRect.getCenter().x + 360,
contourFinder.blobs[i].boundingRect.getCenter().y + 540);
}
}
ofSetHexColor(0xffffff);
stringstream reportStr;
reportStr << "bg subtraction and blob detection" << endl
<< "press ' ' to capture bg" << endl
<< "threshold " << threshold << " (press: +/-)" << endl
<< "num blobs found " << contourFinder.nBlobs << ", fps: " << ofGetFrameRate();
ofDrawBitmapString(reportStr.str(), 20, 600);
}
void ofApp::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;
}
}
void ofApp::keyReleased(int key){
}
void ofApp::mouseMoved(int x, int y ){
}
void ofApp::mouseDragged(int x, int y, int button){
}
void ofApp::mousePressed(int x, int y, int button){
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments