Skip to content

Instantly share code, notes, and snippets.

@AlexAbraham1
Created July 29, 2014 20:13
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 AlexAbraham1/d8381bb7a025f64a1a1f to your computer and use it in GitHub Desktop.
Save AlexAbraham1/d8381bb7a025f64a1a1f to your computer and use it in GitHub Desktop.
#include "Circle.h"
Circle::Circle() {
x = 0;
y = 0;
z = 0;
radius = 1;
color = ofColor(0, 0, 0);
timesDrawn = 0;
}
void Circle::drawCircle() {
ofPushStyle();
ofSetColor(color);
ofCircle(x, y, z, radius);
timesDrawn++;
ofPopStyle();
}
#pragma once
#include "ofMain.h"
class Circle {
public:
Circle();
void drawCircle();
int x;
int y;
int z;
int radius;
ofColor color;
int timesDrawn;
};
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(255, 255, 255);
ofSetLogLevel (OF_LOG_VERBOSE);
screenWidth = 640;
screenHeight = 480 ;
grabber = shared_ptr<ofVideoGrabber>(new ofVideoGrabber());
grabber->setPixelFormat(OF_PIXELS_RGB);
DEVICE_ID = grabber->listDevices().size() - 1;
grabber->setDeviceID(DEVICE_ID);
grabber->initGrabber(screenWidth, screenHeight);
image.allocate(screenWidth, screenHeight, OF_IMAGE_COLOR);
firstRun = true;
minRadius = 10;
maxRadius = 30;
maxTimesDrawn = 1000;
minRed = 215;
minGreen = 95;
minBlue = 155;
maxRed = 255;
maxGreen = 135;
maxBlue = 195;
drawMode = false;
resetCoordinates();
}
//--------------------------------------------------------------
void ofApp::update(){
grabber->update();
if (grabber->isFrameNew()) {
image.setFromPixels(grabber->getPixelsRef());
image.mirror(false, true);
for (int x = 0; x < screenWidth; x++) {
for (int y = 0; y < screenHeight; y++) {
ofColor color = image.getColor(x,y);
float red = float(color.r);
float green = float(color.g);
float blue = float(color.b);
if (ofInRange(red, minRed, maxRed) &&
ofInRange(green, minGreen, maxGreen) &&
ofInRange(blue, minBlue, maxBlue)) {
image.setColor(x, y, ofColor::white);
if (x < xLow) {
xLow = x;
}
if (x > xHigh) {
xHigh = x;
}
if (y < yLow) {
yLow = y;
}
if (y > yHigh) {
yHigh = y;
}
} else {
image.setColor(x, y, ofColor::black);
}
}
}
if (drawMode) {
if (xLow != screenWidth && xHigh != 0 && yLow != screenHeight && yHigh != 0) {
int x = (xLow + xHigh)/2;
int y = (yLow + yHigh)/2;
int radius = 5;
int z = 50;
ofColor color = ofColor(255,105,180);
Circle * circle = new Circle();
circle->x = x;
circle->y = y;
circle->z = z;
circle->radius = radius;
circle->color = color;
circles.push_back(circle);
}
}
image.update();
resetCoordinates();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetHexColor(0xFF69B4);
image.draw(0,0);
for (std::vector<Circle>::size_type i = 0; i != circles.size(); i++) {
Circle * circle = circles[i];
circle->drawCircle();
}
}
//--------------------------------------------------------------
void ofApp::keyPressed (int key){
switch (key){
case ' ':
drawMode = !drawMode;
}
}
//--------------------------------------------------------------
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::cleanupCircles() {
ofLog() << "cleanupCircles() started";
stringstream ss;
ss << circles.size();
ofLog() << "NUMBER OF CIRCLES: " + ss.str();
std::random_shuffle(circles.begin(), circles.end());
ofLog() << "cleanupCircles() ended";
ss.str("");
ss << circles.size();
ofLog() << "NUMBER OF CIRCLES: " + ss.str();
}
bool ofApp::isBlankSpace(int x, int y) {
for (std::vector<Circle>::size_type i = 0; i != circles.size(); i++) {
Circle * circle = circles[i];
float x2 = static_cast<int>(pow(static_cast<double>(circle->x - x), 2)+.005);
float y2 = static_cast<int>(pow(static_cast<double>(circle->y - y), 2)+.005);
float radius2 = static_cast<int>(pow(static_cast<double>(circle->radius), 2)+.005);
if ((x2 + y2) < (radius2)/2) {
return false;
}
}
return true;
}
void ofApp::fixZIndex() {
for (std::vector<Circle>::size_type i = 0; i != circles.size(); i++) {
Circle * circle = circles[i];
float smallRadius = (minRadius + ((minRadius + maxRadius)/2))/2;
if (circle->radius <= smallRadius) {
circle->z = 50;
}
}
}
void ofApp::resetCoordinates() {
xLow = screenWidth;
xHigh = 0;
yLow = screenHeight;
yHigh = 0;
}
#pragma once
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
#include "Circle.h"
class ofApp : public ofBaseApp{
public:
public:
void setup();
void update();
void draw();
void keyPressed (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 cleanupCircles();
bool isBlankSpace(int x, int y);
void switchCamera();
void switchInvert();
void fixZIndex();
void touchCircle(int x, int y);
void detectHandCircles(int xLow, int yLow, int xHigh, int yHigh);
void resetCoordinates();
shared_ptr<ofVideoGrabber> grabber;
ofImage image;
bool firstRun;
bool invertColors;
bool drawMode;
std::vector<Circle*> circles;
int minRadius;
int maxRadius;
int maxTimesDrawn;
float minRed, minGreen, minBlue, maxRed, maxGreen, maxBlue;
int xLow, xHigh, yLow, yHigh;
int DEVICE_ID;
int screenWidth;
int screenHeight;
int haarWidth;
int haarHeight;
int orientation;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment