Newer
Older
// Destructor
ofApp::~ofApp(){
int wwidth = 1920;
int wheight = 1080;
ofSetWindowShape(wwidth, wheight);
cam.setDeviceID(camId);
if(camId==2){
cam.setup(1920, 1080);
xr = 20, yr = 20, wr = 1800, hr = 900;
xr = 20, yr = 20, wr = 300, hr = 200;
}
//contourFinder.setInvert(true); // find black instead of white
// initialise gui
gui.setPosition(50,500);
// Initialise gui and parameters
for(int i = 0; i < num_colours; i++){
ofParameter<float> t;
ofParameter<bool> b;
ofParameter<bool> cc;
ofxCv::ContourFinder cf;
thresholds.push_back(t);
trackHues.push_back(b);
changeColours.push_back(cc);
//contourFinders.push_back(cf);
sharedState->contourFinders.push_back(cf);
gui.add(thresholds[i].set("Threshold " + to_string(i), 255,0,255));
gui.add(trackHues[i].set("Track Hue/Sat colour "+to_string(i), false));
gui.add(changeColours[i].set("Change colour "+to_string(i), false));
sharedState->contourFinders[i].setMinArea(10);
sharedState->contourFinders[i].setMaxArea(40);
sharedState->contourFinders[i].setMinAreaRadius(40);
sharedState->contourFinders[i].setMaxAreaRadius(350);
for(int i = 0; i < 4; i++){
vn.push_back(false);
}
//
xyb=false;
whb=false;
camPix.cropTo(camPix, xr, yr, wr, hr);
// check new frame
if(cam.isFrameNew()) {
// Loop for number of colours and track target colours
for(int i = 0; i < num_colours; i++){
sharedState->contourFinders[i].setTargetColor(targetColours[i], trackHues[i] ? TRACK_COLOR_HS : TRACK_COLOR_RGB);
sharedState->contourFinders[i].setThreshold(thresholds[i]);
sharedState->contourFinders[i].findContours(camPix);
//--------------------------------------------------------------
// Draw camera
// cam.draw(0, 0);
camImage.setFromPixels(camPix);
camImage.draw(0,0);
// Draw gui
ofPushMatrix();
// Draw contours found
for(int i=0; i < num_colours; i ++)
sharedState->contourFinders[i].draw();
ofPushMatrix();
ofTranslate(700,10);
for(int i=0; i < num_colours; i ++){
ofTranslate(0, 75);
ofFill();
ofSetColor(0);
ofDrawRectangle(-3, -3, 64+6, 64+6);
ofSetColor(targetColours[i]);
ofDrawRectangle(0, 0, 64, 64);
}
ofPopMatrix();
// Draw anchor points and highlight when hovered
ofPushMatrix();
// for(int i = 0; i < 4; i++){
// ofSetColor(100, 244, 244, 100);
// ofFill();
// ofDrawCircle(sharedState->proPoints[i].x,
// sharedState->proPoints[i].y, 10); if (vn[i]==true){
// ofNoFill();
// ofDrawCircle(sharedState->proPoints[i].x,
// sharedState->proPoints[i].y, 30);
// }
// }
ofSetColor(100, 244, 244, 100);
ofNoFill();
if(xyb==true){
ofDrawCircle(xr, yr, 30);
}
if(whb==true){
ofDrawCircle(xr+wr, yr+hr, 30);
ofFill();
ofSetColor(ofColor(255, 200, 233, 40));
ofDrawRectangle(xr, yr, wr, hr);
ofPopMatrix();
// img1.setFromPixels(img1Pix);
// img1.draw(700,200);
// Debugging
//cout << targetColours.size() << endl;
void ofApp::mousePressed(int x, int y, int button) {
for(int i = 0; i < num_colours; i++) {
if(changeColours[i]==true) targetColours[i]=cam.getPixels().getColor(x, y);
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
// Anchor hover booleans
// for(int i = 0; i < 4; i++){
// if (ofDist(mouseX, mouseY, sharedState->proPoints[i].x, sharedState->proPoints[i].y) < 20){
// vn[i] = true;
// } else {
// vn[i] = false;
// }
// }
if(ofDist(mouseX, mouseY, xr, yr) < 20){
xyb=true;
}
if (ofDist(mouseX, mouseY, xr+wr, yr+hr) < 20) {
whb=true;
}
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
// Control anchors
// for(int i = 0; i < 4; i++){
// if (ofDist(mouseX, mouseY, sharedState->proPoints[i].x, sharedState->proPoints[i].y) < 20){
// sharedState->proPoints[i].x = mouseX;
// sharedState->proPoints[i].y = mouseY;
// sharedState->setvx(i, mouseX);
// sharedState->setvy(i, mouseY);
// }
// }
if (xyb) {
xr=mouseX;
yr=mouseY;
}
if (whb) {
wr=mouseX-xr;
hr=mouseY-yr;
}
}
//--------------------------------------------------------------