Greetings
I need help to optimize this script to make it running fast with osc sending. as soon as i turn maxmsp on, and start receiving data, cpu load goes wild. i’d appreciate it if you guys could help me. Kind regards
#include “ofMain.h”
#include “ofApp.h”
#include
using namespace std;
//========================================================================
int main( ){
ofSetupOpenGL(320,240, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
#include “ofApp.h”
ofxOscMessage a;
ofxOscMessage b;
//--------------------------------------------------------------
void ofApp::setup(){
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);
bLearnBakground = true;
threshold = 80;
sender.setup(HOST, PORT);
}
//--------------------------------------------------------------
void ofApp::update(){
bool bNewFrame = false;
vidGrabber.update();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage = colorImg;
if (bLearnBakground == true){
grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
bLearnBakground = false;
}
// take the abs value of the difference between background and incoming and then threshold:
grayDiff.absDiff(grayBg, grayImage);
grayDiff.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(grayDiff, 20, (340*240)/3, 10, true); // find holes
}
}
//--------------------------------------------------------------
void ofApp::draw(){
// ofxOscMessage a;
// ofxOscMessage b;
//
// for (int i = 0; i < contourFinder.nBlobs; i++){
// if(contourFinder.blobs[i].hole){
//
// ofxOscMessage a;
// a.setAddress("/blob/xypos");
// a.addIntArg(i);
// a.addFloatArg(contourFinder.blobs[i].boundingRect.getCenter().x);
// a.addFloatArg(contourFinder.blobs[i].boundingRect.getCenter().y);
// sender.sendMessage(a);
//
// ofxOscMessage b;
// b.setAddress("/blob/WH");
// b.addIntArg(i);
// b.addFloatArg(contourFinder.blobs[i].boundingRect.getWidth());
// b.addFloatArg(contourFinder.blobs[i].boundingRect.getHeight());
// sender.sendMessage(b);
//
// }
// if(contourFinder.blobs[i].hole){
//
// a.setAddress("/blob/xypos");
// a.addIntArg(i);
// a.addFloatArg(contourFinder.blobs[i].boundingRect.getCenter().x);
// a.addFloatArg(contourFinder.blobs[i].boundingRect.getCenter().y);
//
// b.setAddress("/blob/WH");
// b.addIntArg(i);
// b.addFloatArg(contourFinder.blobs[i].boundingRect.getWidth());
// b.addFloatArg(contourFinder.blobs[i].boundingRect.getHeight());
//
// }
//
//
// }
//
// sender.sendMessage(a);
// sender.sendMessage(b);
}
//--------------------------------------------------------------
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::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include “ofMain.h”
#include “ofxOpenCv.h”
#include “ofxOsc.h”
#define HOST “localhost”
#define PORT 12345
//#define _USE_LIVE_VIDEO // uncomment this to use a live camera
// otherwise, we’ll use a movie file
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofVideoGrabber vidGrabber;
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
int threshold;
bool bLearnBakground;
ofxOscSender sender;
};