Fellow developers,
I’ve begun learning the basics of Open Frameworks and at the moment I am having a terrible issue. I’m working on an application that reacts to the position of an object at a certain point (color tracking), and the problem I’m experiencing is that when I’m using windows of 320x240 resolution, it’s working perfectly, but when I’m using the resolution needed for my applicati on (640x480) the framerate drops miserably, to like a few frames per second. I literally have no idea where the problem might be.
Here is my testApp.cpp (please ignore the fact that there’s a lot of manual labor instead of proper use of loops and arrays, I just needed to come up with a prototype asap):
> #include "testApp.h"
>
>
>
>
> //
> /*
> void testApp::callibration()
> {
>
> contours.findContours(filtered, 75, w*h/2, 1, false);
>
>
> //Callibration
>
> if(contours.nBlobs)
> if (contours.blobs[0].centroid.x > 480 && contours.blobs[0].centroid.x < 640
> && contours.blobs[0].centroid.y > 0 && contours.blobs[0].centroid.y < 240)
> { {ofFmodSoundStopAll(); ofBackground(0,0,0); callibrated = 1;} }
>
>
>
>
> }
> */
> //--------------------------------------------------------------
> void testApp::setup(){
> ofBackground(125,125,125);
>
> w = 640;
> h = 480;
> track = 0;
> callibrated = 0;
>
> movie.initGrabber(w, h, true);
> movie.setVerbose(true);
>
> //reserve memory for cv images
> rgb.allocate(w, h);
> hsb.allocate(w, h);
> hue.allocate(w, h);
> //sat.allocate(w, h);
> //bri.allocate(w, h);
> filtered.allocate(w, h);
>
> // top sounds 1
> sound1.loadSound("sounds/piano_C.mp3", true);
> sound1.setVolume(0.5f);
> sound1.setSpeed(9.0f);
> sound2.loadSound("sounds/piano_D.mp3", true);
> sound2.setVolume(0.5f);
> sound2.setSpeed(9.0f);
> sound3.loadSound("sounds/piano_E.mp3", true);
> sound3.setVolume(0.5f);
> sound3.setSpeed(9.0f);
> sound4.loadSound("sounds/piano_F.mp3", true);
> sound4.setVolume(0.5f);
> sound4.setSpeed(9.0f);
> sound5.loadSound("sounds/piano_G.mp3", true);
> sound5.setVolume(0.5f);
> sound5.setSpeed(9.0f);
> sound6.loadSound("sounds/piano_A.mp3", true);
> sound6.setVolume(0.5f);
> sound6.setSpeed(9.0f);
> sound7.loadSound("sounds/piano_B.mp3", true);
> sound7.setVolume(0.5f);
> sound7.setSpeed(9.0f);
> sound8.loadSound("sounds/piano_C.mp3", true);
> sound8.setVolume(0.5f);
> sound8.setSpeed(9.0f);
>
> // top sounds 2
> sound9.loadSound("sounds/piano_C.mp3", true);
> sound9.setVolume(0.5f);
> sound9.setSpeed(8.0f);
> sound10.loadSound("sounds/piano_A.mp3", true);
> sound10.setVolume(0.5f);
> sound10.setSpeed(8.0f);
> sound11.loadSound("sounds/piano_B.mp3", true);
> sound11.setVolume(0.5f);
> sound11.setSpeed(8.0f);
> sound12.loadSound("sounds/piano_E.mp3", true);
> sound12.setVolume(0.5f);
> sound12.setSpeed(8.0f);
> sound13.loadSound("sounds/piano_G.mp3", true);
> sound13.setVolume(0.5f);
> sound13.setSpeed(8.0f);
> sound14.loadSound("sounds/piano_F.mp3", true);
> sound14.setVolume(0.5f);
> sound14.setSpeed(8.0f);
> sound15.loadSound("sounds/piano_D.mp3", true);
> sound15.setVolume(0.5f);
> sound15.setSpeed(8.0f);
> sound16.loadSound("sounds/piano_C.mp3", true);
> sound16.setVolume(0.5f);
> sound16.setSpeed(8.0f);
>
>
> }
>
> //--------------------------------------------------------------
> void testApp::update(){
> //ofBackground(255,255,255);
> //ofSetVerticalSync(true);
>
> movie.update();
>
> if (movie.isFrameNew()) {
>
> //copy webcam pixels to rgb image
> rgb.setFromPixels(movie.getPixels(), w, h);
>
> //mirror horizontal
> rgb.mirror(false, true);
>
> //duplicate rgb
> hsb = rgb;
>
> //convert to hsb
> hsb.convertRgbToHsv();
>
> //store the three channels as grayscale images
> hsb.convertToGrayscalePlanarImages(hue, sat, bri);
>
> //filter image based on the hue value were looking for
> for (int i=0; i<w*h; i++) {
> filtered.getPixels()[i] = ofInRange(hue.getPixels()[i],findHue-5,findHue+5) ? 255 : 0;
> }
> filtered.flagImageChanged();
>
> //run the contour finder on the filtered image to find blobs with a certain hue
> //contours.findContours(filtered, 150, w*h/2, 1, false);
>
> contours.findContours(filtered, 75, w*h/2, 1, false);
>
>
> //Callibration
>
> //if(contours.nBlobs)
> //if (contours.blobs[0].centroid.x > 480 && contours.blobs[0].centroid.x < 640
> // && contours.blobs[0].centroid.y > 0 && contours.blobs[0].centroid.y < 240)
> //{ {/*ofFmodSoundStopAll();*/ ofBackground(255,0,0); callibrated = 0;} }
> //
> //if(contours.nBlobs)
> //if (contours.blobs[0].centroid.x > 0 && contours.blobs[0].centroid.x < 160
> // && contours.blobs[0].centroid.y > 0 && contours.blobs[0].centroid.y < 240)
> //{ {/*ofFmodSoundStopAll();*/ ofBackground(0,0,255); callibrated = 0;} }
> //
> //if(contours.nBlobs)
> //if (contours.blobs[0].centroid.x > 480 && contours.blobs[0].centroid.x < 640
> // && contours.blobs[0].centroid.y > 240 && contours.blobs[0].centroid.y < 480)
> //{ {/*ofFmodSoundStopAll();*/ ofBackground(0,255,0); callibrated = 0;} }
> //
> //if(contours.nBlobs)
> //if (contours.blobs[0].centroid.x > 0 && contours.blobs[0].centroid.x < 160
> // && contours.blobs[0].centroid.y > 240 && contours.blobs[0].centroid.y < 480)
> //{ {/*ofFmodSoundStopAll();*/ ofBackground(255,255,0); callibrated = 0;} }
>
>
>
>
>
> //FUCKING MADNESS
>
> //1st top line
> //if (callibrated > 1)
> if(contours.nBlobs)
> if (contours.blobs[0].centroid.x < 80 && contours.blobs[0].centroid.y < 60) {if (sound1.getIsPlaying()==false && track != 1) {ofFmodSoundStopAll(); sound1.play(); track=1;}}
> else if (contours.blobs[0].centroid.x < 160 && contours.blobs[0].centroid.x > 80 && contours.blobs[0].centroid.y < 60) {if (sound2.getIsPlaying()==false && track != 2) {ofFmodSoundStopAll(); sound2.play(); track=2;}}
> else if (contours.blobs[0].centroid.x < 240 && contours.blobs[0].centroid.x > 160 && contours.blobs[0].centroid.y < 60) {if (sound3.getIsPlaying()==false && track != 3) {ofFmodSoundStopAll(); sound3.play(); track=3;}}
> else if (contours.blobs[0].centroid.x < 320 && contours.blobs[0].centroid.x > 240 && contours.blobs[0].centroid.y < 60) {if (sound4.getIsPlaying()==false && track != 4) {ofFmodSoundStopAll(); sound4.play(); track=4;}}
> else if (contours.blobs[0].centroid.x < 400 && contours.blobs[0].centroid.x > 320 && contours.blobs[0].centroid.y < 60) {if (sound5.getIsPlaying()==false && track != 5) {ofFmodSoundStopAll(); sound5.play(); track=5;}}
> else if (contours.blobs[0].centroid.x < 480 && contours.blobs[0].centroid.x > 400 && contours.blobs[0].centroid.y < 60) {if (sound6.getIsPlaying()==false && track != 6) {ofFmodSoundStopAll(); sound6.play(); track=6;}}
> else if (contours.blobs[0].centroid.x < 560 && contours.blobs[0].centroid.x > 480 && contours.blobs[0].centroid.y < 60) {if (sound7.getIsPlaying()==false && track != 7) {ofFmodSoundStopAll(); sound7.play(); track=7;}}
> else if (contours.blobs[0].centroid.x < 640 && contours.blobs[0].centroid.x > 560 && contours.blobs[0].centroid.y < 60) {if (sound8.getIsPlaying()==false && track != 8) {ofFmodSoundStopAll(); sound8.play(); track=8;}}
>
> //2nd top line
> else if (contours.blobs[0].centroid.x < 80 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound9.getIsPlaying()==false && track != 9) {ofFmodSoundStopAll(); sound9.play(); track=9;}}
> else if (contours.blobs[0].centroid.x < 160 && contours.blobs[0].centroid.x > 80 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound10.getIsPlaying()==false && track != 10) {ofFmodSoundStopAll(); sound10.play(); track=10;}}
> else if (contours.blobs[0].centroid.x < 240 && contours.blobs[0].centroid.x > 160 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound11.getIsPlaying()==false && track != 11) {ofFmodSoundStopAll(); sound11.play(); track=11;}}
> else if (contours.blobs[0].centroid.x < 320 && contours.blobs[0].centroid.x > 240 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound12.getIsPlaying()==false && track != 12) {ofFmodSoundStopAll(); sound12.play(); track=12;}}
> else if (contours.blobs[0].centroid.x < 400 && contours.blobs[0].centroid.x > 320 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound13.getIsPlaying()==false && track != 13) {ofFmodSoundStopAll(); sound13.play(); track=13;}}
> else if (contours.blobs[0].centroid.x < 480 && contours.blobs[0].centroid.x > 400 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound14.getIsPlaying()==false && track != 14) {ofFmodSoundStopAll(); sound14.play(); track=14;}}
> else if (contours.blobs[0].centroid.x < 560 && contours.blobs[0].centroid.x > 480 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound15.getIsPlaying()==false && track != 15) {ofFmodSoundStopAll(); sound15.play(); track=15;}}
> else if (contours.blobs[0].centroid.x < 640 && contours.blobs[0].centroid.x > 560 && contours.blobs[0].centroid.y > 60 && contours.blobs[0].centroid.y < 120) {if (sound16.getIsPlaying()==false && track != 16) {ofFmodSoundStopAll(); sound16.play(); track=16;}}
>
> }
>
>
>
>
>
> }
>
> //--------------------------------------------------------------
> void testApp::draw(){
> ofSetColor(255,255,255);
>
> // draw all cv images
> rgb.draw(0,0);
> contours.draw(640,0);
> // hue.draw(0,240);
> // sat.draw(320,240);
> // bri.draw(640,240);
> filtered.draw(0,480);
> // hsb.draw(640,480);
>
> ofSetColor(255, 0, 0);
> ofFill();
>
> //draw red circles for found blobs
> for (int i=0; i<contours.nBlobs; i++) {
> ofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 20);
> }
> }
>
> //--------------------------------------------------------------
> void testApp::keyPressed(int key){
>
> }
>
> //--------------------------------------------------------------
> void testApp::keyReleased(int key){
>
> }
>
> //--------------------------------------------------------------
> 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){
> //calculate local mouse x,y in image
>
> int mx = x % w;
> int my = y % h;
>
> //get hue value on mouse position
> //if (callibrated < 2)
> findHue = hue.getPixels()[my*w+mx];
> }
>
> //--------------------------------------------------------------
> void testApp::mouseReleased(int x, int y, int button){
>
> }
>
> //--------------------------------------------------------------
> void testApp::windowResized(int w, int h){
>
> }
>
> //--------------------------------------------------------------
> void testApp::gotMessage(ofMessage msg){
>
> }
>
> //--------------------------------------------------------------
> void testApp::dragEvent(ofDragInfo dragInfo){
>
> }
>
>