Skip to content

Instantly share code, notes, and snippets.

@gatana
Created April 29, 2016 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatana/52632abab82e6d5a995e1115ee75adff to your computer and use it in GitHub Desktop.
Save gatana/52632abab82e6d5a995e1115ee75adff to your computer and use it in GitHub Desktop.
#include "ofApp.h"
int vidX = 320;
int vidY = 240;
int pointerX = 0;
int pointerY = 0;
int pointerRad = 20;
int i = 0;
//used within _i
int _i = 0;
ofVideoPlayer mov1;
ofImage guts;
ofImage oo;
//--------------------------------------------------------------
void ofApp::setup(){
// mesh.setMode(OF_PRIMITIVE_POINTS);
// mesh.enableColors();
//
// float intensityThreshold = 100.0;
// int w = oo.getWidth();
// int h = oo.getHeight();
// for (int x=0; x<w; ++x) {
// for (int y=0; y<h; ++y) {
// ofColor c = oo.getColor(x, y);
// float intensity = c.getBrightness();
// if (intensity >= intensityThreshold) {
// ofVec3f pos(x, y, 0.0);
// mesh.addVertex(pos);
// mesh.addColor(c);
// }
// }
//
vidGrabber.setVerbose(true);
vidGrabber.setup(vidX,vidY); // setup live video grabbing
colorImg.allocate(vidX,vidY); // color image display
grayImage.allocate(vidX,vidY); // grayscale display
grayBg.allocate(vidX,vidY); // contour image
grayDiff.allocate(vidX,vidY); // b/w differencing image
bLearnBakground = true;
threshold = 80;
myFont.load("stars.ttf",45);
ofSetVerticalSync(true);
bSendSerialMessage = false;
ofSetLogLevel(OF_LOG_VERBOSE);
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
// arduino users check in arduino app....
int baud = 9600;
// serial.setup(0, baud); //open the first device
//serial.setup("COM4", baud); // windows example
serial.setup("/dev/tty.usbmodem1411", baud); // mac osx example
//serial.setup("/dev/ttyUSB0", baud); //linux example
nTimesRead = 0;
nBytesRead = 0;
readTime = 0;
memset(bytesReadString, 0, 4);
backgroundmusic.load("TheStars.mp3");
// music1.play();
// music1.setPaused(true);
oo.load("oo.jpg");
guts.load("guts.png");
}
//--------------------------------------------------------------
void ofApp::update(){
// ofBackground(19,89,190, 80);
bool bNewFrame = false;
vidGrabber.update();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){ // if we have a new frame
colorImg.setFromPixels(vidGrabber.getPixels());
grayImage = colorImg;
if (bLearnBakground == true){ // if we hit space, this grabs a "control" image
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.
// find max 10 blobs with no holes in them
contourFinder.findContours(grayDiff, 20, (vidX*vidY)/3, 10, false); // false == dont find holes in our blobs
}
if (bSendSerialMessage){
// (1) write the letter "a" to serial:
serial.writeByte('a');
// (2) read
// now we try to read 3 bytes
// since we might not get them all the time 3 - but sometimes 0, 6, or something else,
// we will try to read three bytes, as much as we can
// otherwise, we may have a "lag" if we don't read fast enough
// or just read three every time. now, we will be sure to
// read as much as we can in groups of three...
nTimesRead = 0;
nBytesRead = 0;
int nRead = 0; // a temp variable to keep count per read
unsigned char bytesReturned[3];
memset(bytesReadString, 0, 4);
memset(bytesReturned, 0, 3);
while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
nTimesRead++;
nBytesRead = nRead;
};
memcpy(bytesReadString, bytesReturned, 3);
bSendSerialMessage = false;
readTime = ofGetElapsedTimef();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
int alpha;
oo.draw(0, 0);
// grayImage.draw(vidX + 40, 20);
grayDiff.draw(vidX + 40, vidY*2 + 40);
contourFinder.draw(vidX*2 + 60, vidY*2 + 40);
// loop pages, back and forth
if (_i < 0) {
_i = 10;
}
if (_i > 10){
_i = 0;
}
switch(_i) {
case 0:
myFont.drawString("AE HOUSMAN - \n \n POEM FROM A SHROPSHIRE LAD", 90, 90);
//before drawing string test serial pore
//how to have the code solely effect the font/ not flash intensely
// bSendSerialMessage = true;
// if (nBytesRead != 1) {
// ofSetColor(255, 244, 2, alpha);
// alpha++;
// // ofdelay(8000);
// } else {
// alpha =0;
// }
//if space bar is pressed then draw color redraw draw frame
break;
case 1:
myFont.drawString ("THE STARS HAVE NOT DEALT ME \n \n THE WORST THEY COULD DO", 90, 90);
break;
case 2:
myFont.drawString("MY PLEASURES ARE PLENTY \n \n MY TROUBLES ARE TWO", 90, 90);
break;
case 3:
myFont.drawString("BUT OH MY TWO TROUBLES \n \n THEY REAVE ME OF REST", 90, 90);
break;
case 4:
myFont.drawString("THE BRAINS IN MY HEAD \n \n AND THE HEART IN MY BREAST ", 90, 90);
break;
case 5:
myFont.drawString("OH GRANT ME THE EASE \n \n THAT WAS GRANTED SO FREE", 90, 90);
break;
case 6:
myFont.drawString("THE BIRTHRIGHT OF MULTITUDES \n \n GIVE IT TO ME", 90, 90);
break;
case 7:
myFont.drawString("THAT RELISH THEIR VICTUALS \n \n AND REST ON THE BED", 90, 90);
break;
case 8:
myFont.drawString("WITH FLINT IN THE BOSOM", 90, 90);
break;
case 9:
myFont.drawString("AND GUTS IN THE HEAD.", 90, 90);
if (contourFinder.nBlobs > 0) { // if any detected TODO: go w largest
pointerX = ofMap(contourFinder.blobs[0].boundingRect.x, vidX, 0, 0, ofGetWidth(), true);
pointerY = ofMap(contourFinder.blobs[0].boundingRect.y, 0, vidY, 0, ofGetHeight(), true);
//ofDrawCircle(pointerX, pointerY, pointerRad);
guts.draw(pointerX, pointerY, pointerRad);
}
break;
case 10:
myFont.drawString("FONT BY ANA SOFIA REMIS", 90, 90);
break;
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == OF_KEY_RIGHT){
_i++;
}
if (key == OF_KEY_LEFT){
_i--;
}
if (key == OF_KEY_UP) {
// backgroundmusic.load("TheStars.mp3");
// backgroundmusic.setLoop(true);
backgroundmusic.play();
}
if (key == OF_KEY_DOWN) {
backgroundmusic.setPaused(true);
}
if (key == 'g' ){
ofSetBackgroundAuto(false);
}
if (key == 'l') {
ofSetColor (90, 178, 190);
}
if (key == 'k') {
//get rid of videos by pressing this button
ofSetColor (20,2,20,100);
grayDiff.draw(vidX + 40, vidY*2 + 40);
contourFinder.draw(vidX*2 + 60, vidY*2 + 40);
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
//
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
//how to make it so when the mouse is dragged the text moves with mouse x mouse Y
//myFont.drawString(someString, mouseX, mouseY);
ofHideCursor();
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
//ofSetColor (250, 70, 100);
bSendSerialMessage = true;
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
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"
#define _USE_LIVE_VIDEO
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 mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
// ofSoundPlayer;
ofSoundPlayer backgroundmusic;
ofImage oo;
ofImage guts;
ofTrueTypeFont myFont;
ofSoundPlayer music1;
bool bSendSerialMessage; // a flag for sending serial
char bytesRead[3]; // data from serial, we will be trying to read 3
char bytesReadString[4]; // a string needs a null terminator, so we need 3 + 1 bytes
int nBytesRead; // how much did we read?
int nTimesRead; // how many times did we read?
float readTime; // when did we last read?
ofSerial serial;
#ifdef _USE_LIVE_VIDEO
ofVideoGrabber vidGrabber;
#else
ofVideoPlayer vidPlayer;
#endif
ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
ofMesh mesh;
int threshold;
bool bLearnBakground;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment