#include "ofApp.h"
void ofApp::setup(){
capW = 320;
capH = 240;
#ifdef _USE_LIVE_VIDEO
vidGrabber.setup(capW, capH);
capW = vidGrabber.getWidth();
capH = vidGrabber.getHeight();
#else
vidPlayer.load("fingers.m4v");
vidPlayer.setLoopState(OF_LOOP_NORMAL);
vidPlayer.play();
#endif
colorImg.allocate(capW,capH);
grayImage.allocate(capW,capH);
grayBg.allocate(capW,capH);
grayDiff.allocate(capW,capH);
bLearnBakground = true;
threshold = 80;
ofSetFrameRate(20);
}
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
if( vidGrabber.getPixels().getData() != NULL ){
#else
if( vidPlayer.getPixels().getData() != NULL && vidPlayer.getWidth() > 0 ){
#endif
#ifdef _USE_LIVE_VIDEO
colorImg.setFromPixels(vidGrabber.getPixels().getData(), capW, capH);
#else
colorImg.setFromPixels(vidPlayer.getPixels().getData(), capW, capH);
#endif
grayImage = colorImg;
if (bLearnBakground == true){
grayBg = grayImage;
bLearnBakground = false;
}
grayDiff.absDiff(grayBg, grayImage);
grayDiff.threshold(threshold);
contourFinder.findContours(grayDiff, 20, (capW*capH)/3, 10, true);
}
}
}
void ofApp::draw(){
ofSetColor(255);
ofDrawBitmapString(ofToString(ofGetFrameRate()), 20, 20);
ofPushMatrix();
ofScale(0.5, 0.5, 1);
ofSetHexColor(0xffffff);
grayImage.draw(0,0);
grayBg.draw(capW+4, 0);
grayDiff.draw(0, capH + 4);
for (int i = 0; i < contourFinder.nBlobs; i++){
contourFinder.blobs[i].draw(0, capH + 4);
}
ofPopMatrix();
ofSetHexColor(0xffffff);
stringstream reportStr;
reportStr << "bg subtraction and blob detection\nptap to capture bg\n";
reportStr << "threshold "<< threshold << "\nnum blobs found " << contourFinder.nBlobs << " fps: " << ofGetFrameRate();
ofDrawBitmapString(reportStr.str(), 4, 380);
}
void ofApp::exit(){
}
void ofApp::touchDown(ofTouchEventArgs & touch){
bLearnBakground = true;
}
void ofApp::touchMoved(ofTouchEventArgs & touch){
}
void ofApp::touchUp(ofTouchEventArgs & touch){
}
void ofApp::touchDoubleTap(ofTouchEventArgs & touch){
}
void ofApp::touchCancelled(ofTouchEventArgs & touch){
}
void ofApp::lostFocus(){
}
void ofApp::gotFocus(){
}
void ofApp::gotMemoryWarning(){
}
void ofApp::deviceOrientationChanged(int newOrientation){
}
Comments