Last active
September 23, 2017 14:34
-
-
Save juapp/8a5954ee32591bde874d647c89a93f57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Blob { | |
float minx; | |
float miny; | |
float maxx; | |
float maxy; | |
int id = 0; | |
boolean taken = false; | |
Blob(float x, float y) { | |
minx = x; | |
miny = y; | |
maxx = x; | |
maxy = y; | |
} | |
void show() { | |
stroke(0); | |
fill(255, 100); | |
strokeWeight(2); | |
rectMode(CORNERS); | |
rect(minx, miny, maxx, maxy); | |
// textAlign(CENTER); | |
// textSize(64); | |
//fill(0); | |
//text(id, minx + (maxx-minx)*0.5, maxy - 10); | |
} | |
void add(float x, float y) { | |
minx = min(minx, x); | |
miny = min(miny, y); | |
maxx = max(maxx, x); | |
maxy = max(maxy, y); | |
} | |
void become(Blob other) { | |
minx = other.minx; | |
maxx = other.maxx; | |
miny = other.miny; | |
maxy = other.maxy; | |
} | |
float size() { | |
return (maxx-minx)*(maxy-miny); | |
} | |
PVector getCenter() { | |
float x = (maxx - minx)* 0.5 + minx; | |
float y = (maxy - miny)* 0.5 + miny; | |
return new PVector(x,y); | |
} | |
boolean isNear(float x, float y) { | |
float cx = max(min(x, maxx), minx); | |
float cy = max(min(y, maxy), miny); | |
float d = distSq(cx, cy, x, y); | |
if (d < distThreshold*distThreshold) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.video.*; | |
Capture video; | |
import arb.soundcipher.*; | |
SoundCipher sc; | |
boolean[] keysNotePlay; | |
int[] keysNoteMap; | |
//Piano acima | |
int blobCounter = 0; | |
float bx; | |
float by; | |
boolean overBox = false; | |
boolean locked = false; | |
int maxLife = 200; | |
color trackColor; | |
float threshold = 40; | |
float distThreshold = 50; | |
ArrayList<Blob> blobs = new ArrayList<Blob>(); | |
void setup() { | |
size(640, 360); | |
String[] cameras = Capture.list(); | |
printArray(cameras); | |
video = new Capture(this, cameras[3]); | |
video.start(); | |
trackColor = color(183, 12, 83); | |
sc = new SoundCipher(this); | |
keysNotePlay = new boolean[127]; | |
keysNoteMap = new int[127]; | |
keysNoteMap['a'] = 59; | |
keysNoteMap['s'] = 60; | |
keysNoteMap['d'] = 62; | |
keysNoteMap['f'] = 64; | |
keysNoteMap['g'] = 65; | |
keysNoteMap['h'] = 67; | |
keysNoteMap['j'] = 69; | |
keysNoteMap['w'] = 61; | |
keysNoteMap['e'] = 63; | |
keysNoteMap['t'] = 66; | |
keysNoteMap['y'] = 68; | |
keysNoteMap['u'] = 70; | |
} | |
void keyReleased(){ | |
keysNotePlay[key] = false; | |
} | |
void captureEvent(Capture video) { | |
video.read(); | |
} | |
void keyPressed() { | |
if (key == 'a') { | |
distThreshold+=5; | |
} else if (key == 'z') { | |
distThreshold-=5; | |
} | |
if (key == 's') { | |
threshold+=5; | |
} else if (key == 'x') { | |
threshold-=5; | |
} | |
} | |
void draw() { | |
rect (10, 10, 30, 100); | |
video.loadPixels(); | |
image(video, 0, 0); | |
ArrayList<Blob> currentBlobs = new ArrayList<Blob>(); | |
// Begin loop to walk through every pixel | |
for (int x = 0; x < video.width; x++ ) { | |
for (int y = 0; y < video.height; y++ ) { | |
int loc = x + y * video.width; | |
// What is current color | |
color currentColor = video.pixels[loc]; | |
float r1 = red(currentColor); | |
float g1 = green(currentColor); | |
float b1 = blue(currentColor); | |
float r2 = red(trackColor); | |
float g2 = green(trackColor); | |
float b2 = blue(trackColor); | |
frameRate(30); | |
float d = distSq(r1, g1, b1, r2, g2, b2); | |
if (d < threshold*threshold) { | |
boolean found = false; | |
for (Blob b : currentBlobs) { | |
if (b.isNear(x, y)) { | |
b.add(x, y); | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
Blob b = new Blob(x, y); | |
currentBlobs.add(b); | |
} | |
} | |
} | |
} | |
for (int i = currentBlobs.size()-1; i >= 0; i--) { | |
if (currentBlobs.get(i).size() < 500) { | |
currentBlobs.remove(i); | |
} | |
} | |
// There are no blobs! | |
if (blobs.isEmpty() && currentBlobs.size() > 0) { | |
println("Adding blobs!"); | |
for (Blob b : currentBlobs) { | |
b.id = blobCounter; | |
blobs.add(b); | |
blobCounter++; | |
} | |
} else if (blobs.size() <= currentBlobs.size()) { | |
// Match whatever blobs you can match | |
for (Blob b : blobs) { | |
float recordD = 1000; | |
Blob matched = null; | |
for (Blob cb : currentBlobs) { | |
PVector centerB = b.getCenter(); | |
PVector centerCB = cb.getCenter(); | |
float d = PVector.dist(centerB, centerCB); | |
if (d < recordD && !cb.taken) { | |
recordD = d; | |
matched = cb; | |
rect (10, 10, 30, 255); | |
} | |
} | |
matched.taken = true; | |
b.become(matched); | |
} | |
// Whatever is leftover make new blobs | |
for (Blob b : currentBlobs) { | |
if (!b.taken) { | |
b.id = blobCounter; | |
blobs.add(b); | |
blobCounter++; | |
} | |
} | |
} else if (blobs.size() > currentBlobs.size()) { | |
for (Blob b : blobs) { | |
b.taken = false; | |
} | |
// Match whatever blobs you can match | |
for (Blob cb : currentBlobs) { | |
float recordD = 1000; | |
Blob matched = null; | |
for (Blob b : blobs) { | |
PVector centerB = b.getCenter(); | |
PVector centerCB = cb.getCenter(); | |
float d = PVector.dist(centerB, centerCB); | |
if (d < recordD && !b.taken) { | |
recordD = d; | |
matched = b; | |
} | |
} | |
if (matched != null) { | |
matched.taken = true; | |
matched.become(cb); | |
} | |
} | |
for (int i = blobs.size() - 1; i >= 0; i--) { | |
Blob b = blobs.get(i); | |
if (!b.taken) { | |
blobs.remove(i); | |
} | |
} | |
} | |
for (Blob b : blobs) { | |
b.show(); | |
} | |
if (mouseX >10 && mouseX < 40 && | |
mouseY >10 && mouseY < 110 ) | |
{ | |
SoundCipher sc = new SoundCipher(this); | |
frameRate(5); | |
sc.playNote(59, 100, 2.0); | |
} | |
fill(255); | |
if( keyPressed && keysNotePlay['a'] == true){ | |
fill(204); | |
} | |
rect (10, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['s'] == true){ | |
fill(204); | |
} | |
rect (40, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['d'] == true){ | |
fill(204); | |
} | |
rect (70, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['f'] == true){ | |
fill(204); | |
} | |
rect (100, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['g'] == true){ | |
fill(204); | |
} | |
rect (130, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['h'] == true){ | |
fill(204); | |
} | |
rect (160, 10, 30, 100); | |
fill(255); | |
if( keyPressed && keysNotePlay['j'] == true){ | |
fill(204); | |
} | |
rect (190, 10, 30, 100); | |
//black tut | |
fill(0); | |
if( keyPressed && keysNotePlay['w'] == true){ | |
fill(204); | |
} | |
rect (32,10,15,60); | |
fill(0); | |
if( keyPressed && keysNotePlay['e'] == true){ | |
fill(204); | |
} | |
rect (62,10,15,60); | |
fill(0); | |
if( keyPressed && keysNotePlay['t'] == true){ | |
fill(204); | |
} | |
rect (122,10,15,60); | |
fill(0); | |
if( keyPressed && keysNotePlay['y'] == true){ | |
fill(204); | |
} | |
rect (152,10,15,60); | |
fill(0); | |
if( keyPressed && keysNotePlay['u'] == true){ | |
fill(204); | |
} | |
rect (182,10,15,60); | |
if( keyPressed && keysNotePlay[key] == false){ | |
sc.playNote(keysNoteMap[key], 100, 1); | |
keysNotePlay[key] = true; | |
} | |
textAlign(RIGHT); | |
//fill(0); | |
//text(currentBlobs.size(), width-10, 40); | |
// text(blobs.size(), width-10, 80); | |
textSize(24); | |
// text("color threshold: " + threshold, width-10, 50); | |
// text("distance threshold: " + distThreshold, width-10, 25); | |
text("Eixo Y:" + pmouseY, width-10, 25); | |
text("Eixo X:" + pmouseX, width-10, 50); | |
println(mouseX + " : " + pmouseY); | |
} | |
float distSq(float x1, float y1, float x2, float y2) { | |
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1); | |
return d; | |
} | |
float distSq(float x1, float y1, float z1, float x2, float y2, float z2) { | |
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1); | |
return d; | |
} | |
void mousePressed() { | |
// Save color where the mouse is clicked in trackColor variable | |
int loc = mouseX + mouseY*video.width; | |
trackColor = video.pixels[loc]; | |
println(red(trackColor), green(trackColor), blue(trackColor)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment