#include "testApp.h" int imgWidth = 320; int imgHeight = 240; //-------------------------------------------------------------- void testApp::setup(){ /* vidGrabber.setVerbose(true); vidGrabber.initGrabber(320,240); // windows direct show users be careful // some devices (dvcams, for example) don't // allow you to capture at this resolution. // you will need to check, and if necessary, change // some of these values from 320x240 to whatever your camera supports // most webcams, firewire cams and analog capture devices will support this resolution. colorImg.allocate(imgWidth, imgHeight); */ // allocate memory giOriginalImage.allocate(imgWidth, imgHeight); giBgImage.allocate(imgWidth, imgHeight); giCurrentImage.allocate(imgWidth, imgHeight); giDiff.allocate(imgWidth, imgHeight); fiLearn.allocate(imgWidth, imgHeight); // load images imgLoaderA.loadImage("images/bg_0.bmp"); // load image giOriginalImage.setFromPixels( imgLoaderA.getPixels(),imgWidth, imgHeight ); imgLoaderB.loadImage("images/bg_1.bmp"); giCurrentImage.setFromPixels( imgLoaderB.getPixels(),imgWidth, imgHeight ); giBgImage = giOriginalImage; fiLearn.setFromGrayscaleImage(giBgImage); fLearnRate = .0001f; threshold = 80; } //-------------------------------------------------------------- void testApp::update(){ /*vidGrabber.grabFrame(); if (vidGrabber.isFrameNew()){ colorImg.setFromPixels(vidGrabber.getPixels(), 320,240); grayImage.setFromColorImage(colorImg); }*/ ofBackground(180,180,180); fiLearn.addWeighted(giCurrentImage, fLearnRate); giBgImage = fiLearn; // take the abs value of the difference between background and incoming and then threshold: giDiff.absDiff(giBgImage, giCurrentImage); giDiff.threshold(threshold); // find contours which are between the size of 20 pixels and 1/3 the w*h pixels. // also, find holes is set to true so we will get interior contours as well.... contourFinder.findContours(giDiff, 20, (340*240)/3, 10, true); // find holes } //-------------------------------------------------------------- void testApp::draw(){ ofSetColor(0xffffff); giOriginalImage.draw(10,20); giCurrentImage.draw(340,20); giBgImage.draw(670,20); giDiff.draw(10,300); ofSetColor(0x000000); ofFill(); ofRect(340,300,320,240); ofSetColor(0xffffff); ofNoFill(); glPushMatrix(); glTranslatef(340,300,0); contourFinder.draw(); glPopMatrix(); ofSetColor(0x000000); char reportStr[1024]; sprintf(reportStr, "Original Background Image"); ofDrawBitmapString(reportStr, 10, 280); sprintf(reportStr, "Current Image"); ofDrawBitmapString(reportStr, 340, 280); sprintf(reportStr, "Learned Background Image"); ofDrawBitmapString(reportStr, 670, 280); sprintf(reportStr, "Learn Rate: %f\n\n+ and - to adjust the learn rate.",fLearnRate); ofDrawBitmapString(reportStr, 670, 360); sprintf(reportStr, "Elapsed Time: %f",ofGetElapsedTimef()); ofDrawBitmapString(reportStr, 670, 420); } //-------------------------------------------------------------- void testApp::keyPressed (int key){ switch (key){ case '+': fLearnRate += .0001f; break; case '-': fLearnRate -= .0001f; fLearnRate = MAX(0.0f, fLearnRate); break; } } //-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void testApp::mouseReleased(){ }