Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Processing – Ape Color Tracker

Februar 24, 2012

Der Color Tracker lässt Affenköpfe steuern und durch eine zweite Farbe ihren Mund bewegen

import processing.video.*;

// Variable for capture device
Capture video;
PVector middle;

int index = 0;
int apeCount = 1;
Ape[] apes;

int space = 2;
int[] closestX, closestSecondX, closestY, closestSecondY;

color[] myColors = {
#A95900, #AD6300, #854300, #7C3000, #984900, #B76800, #5C2401, #CD8602, #BF7400, #9A5300, #C17A11, #933500, #D88C09, #491401, #643C00, #9B4400, #DF950C, #9E5100, #C26B00, #AD5400, #DD9B1B, #BC6400, #C77B00, #BB5900, #AA4700, #7D5319, #270901, #EBA917, #A06E21, #B67341, #523213, #9D5520, #E89C00, #BD8B49, #CF8E28, #7D651E, #F3B429, #E39B64, #E99D20, #EAAF92, #DE8F00, #856006, #856240, #CD7600, #80361A, #E2784D, #CF5223, #E9D165, #EFBCB6, #634422, #F28641, #CFBC66, #B2430D, #B68378, #F0DC91, #E66628, #BAA193, #7E0E01, #C07400, #C46B00, #E5A700, #BCB9B8, #EFCCC4, #C0C9D8
};
float[] myColorsRate = {
7.12, 7.06, 5.95, 5.3, 5.24, 5.03, 4.46, 3.91, 3.89, 3.33, 3.02, 2.96, 2.85, 2.68, 2.44, 2.42, 1.92, 1.9, 1.83, 1.64, 1.62, 1.61, 1.57, 1.54, 1.49, 1.49, 1.45, 1.36, 1.13, 0.94, 0.94, 0.91, 0.8, 0.74, 0.71, 0.62, 0.6, 0.55, 0.54, 0.43, 0.39, 0.39, 0.39, 0.25, 0.25, 0.22, 0.19, 0.18, 0.18, 0.16, 0.16, 0.15, 0.15, 0.14, 0.13, 0.13, 0.12, 0.11, 0.07, 0.07, 0.07, 0.06, 0.06, 0.01
};

// A variable for the color we are searching for.
color[] trackColor1;
color[] trackColor2;

void setup() {
size(320*2, 240*2);
video = new Capture(this, width, height, 15);
// Start off tracking for red
smooth();

apes = new Ape[apeCount];
closestX = new int[apeCount];
closestY = new int[apeCount];
closestSecondX = new int[apeCount];
closestSecondY = new int[apeCount];
trackColor1 = new color[apeCount];
trackColor2 = new color[apeCount];

for (int i=0;i<apeCount;i++) {
apes[i] = new Ape(myColors, myColorsRate);
closestX[i] = 0;
closestY[i] = 0;
trackColor1[i] = color(255, 0, 0);
trackColor2[i] = color(0, 255, 255);
}

middle = new PVector(width/2, height);
}

void draw() {
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video, 0, 0);

int rowsCount = 100;
int colsCount = 100;

int rows = width/rowsCount;
int cols = height/colsCount;

for (int i = 0; i < apeCount;i++) {

float worldRecord1 = 1000;
float worldRecord2 = 1000;

int startY, startX, endY, endX;
if ((closestX[i] == 0 && closestY[i] == 0) || frameCount % 50 == 0) {
// first time
startY = 0;
startX = 0;
endY = colsCount;
endX = rowsCount;
}
else {
startY = closestY[i]/cols-space;
startX = closestX[i]/rows-space;
endY = startY + space*2;
endX = startX + space*2;
}
for (int y = startY; y < endY; y ++ ) {
for (int x = startX; x < endX; x ++ ) {
color currentColor = getColor(x*rows, y*cols, rows, cols);
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor1[i]);
float g2 = green(trackColor1[i]);
float b2 = blue(trackColor1[i]);
float d = dist(r1, g1, b1, r2, g2, b2);
if (d < worldRecord1) {
worldRecord1 = d;
closestX[i] = x*rows;
closestY[i] = y*cols;
}
}
}

if ((closestSecondX[i] == 0 && closestSecondY[i] == 0) || frameCount % 50 == 0) {
// first time
startY = 0;
startX = 0;
endY = colsCount;
endX = rowsCount;
}
else {
startY = closestSecondY[i]/cols-space;
startX = closestSecondX[i]/rows-space;
endY = startY + space*2;
endX = startX + space*2;
}
for (int y = startY; y < endY; y ++ ) {
for (int x = startX; x < endX; x ++ ) {
color currentColor = getColor(x*rows, y*cols, rows, cols);
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor2[i]);
float g2 = green(trackColor2[i]);
float b2 = blue(trackColor2[i]);
float d = dist(r1, g1, b1, r2, g2, b2);
if (d  video.height) {
_y -= abs(_y+w-video.height);
}

if (_x < 0) {
_x += abs(0 -_x+w);
}
if (_y < 0) {
_y += abs(0-_y+w);
}
PVector c = new PVector();
for (int x = _x; x < _x+w; x++ ) {
for (int y = _y; y < _y+w; y++ ) {
int loc = x + y * video.width;
color col = video.pixels[loc];
c.x += red(col);
c.y += green(col);
c.z += blue(col);
}
}
c.div(w*h);
return color(c.x, c.y, c.z);
}

void keyPressed() {
if (key == ' ') {
for (int i=0;i 0 && key  0.8) { // nur 20%
showBeard  = true;
beardCount = 10;
beardX = new float[beardCount];
beardY = new float[beardCount];
for(int i = 0;i < beardCount; i++) {
beardX[i] = random(-0.5,0.5);
beardY[i] = random(0.3,0.6);
}
} else {
showBeard  = false;
}
}

void draw(float _x, float _y, float _d,String _a, float mouthC) {
if(_a == "LEFT") {
draw(_x+_d/2,_y+_d/2,_d,mouthC);
} else if (_a == "RIGHT") {
draw(_x-_d/2,_y+_d/2,_d,mouthC);
} else {
draw(_x,_y,_d,mouthC);
}
}

void draw(float _x, float _y, float _d,float mouthC) {

float r2 = _d/2;
float r1 = r2*0.75;

// basic shape
fill(col1);
noStroke();

pushMatrix();
translate(_x,_y);

beginShape();
for(int i = 0;i<hairCount;i++) {
vertex(hairX1[i]*r1,hairY1[i]*r1);
vertex(hairX2[i]*(r2-r2*hairX3[i]),hairY2[i]*(r2-r2*hairY3[i]));
}
endShape(CLOSE);

// face
fill(col2);
ellipse(0,r1*0.2,r1*faceW,r1*faceH);
ellipse(r1*-eyeA,r1*-eyeH,r1*eyeR1,r1*eyeR1);
ellipse(r1*eyeA,r1*-eyeH,r1*eyeR1,r1*eyeR1);

stroke(col1);
strokeWeight(r1*0.05);
//eyes
ellipse(r1*-eyeA,r1*-eyeH,r1*eyeR2,r1*eyeR2);
ellipse(r1*eyeA,r1*-eyeH,r1*eyeR2,r1*eyeR2);
fill(col1);
noStroke();
ellipse(r1*-eyeA,r1*-eyeH,r1*0.03,r1*0.03);
ellipse(r1*eyeA,r1*-eyeH,r1*0.03,r1*0.03);

noFill();
stroke(col1);

if(showEyeBrown) {
strokeWeight(r1*0.04);
curve(0,r1*eyeB1,r1*-eyeA-r1*eyeR2,r1*-eyeH,0,r1*-eyeH,0,r1);
curve(0,r1*eyeB2,0,r1*-eyeH,r1*+eyeA+r1*eyeR2,r1*-eyeH,0,r1);
}

//nose
strokeWeight(r1*0.05);
line(r1*-noseA,eyeH*r1*-noseH,0,0-r1/8);
line(r1*noseA,eyeH*r1*-noseH,0,0-r1/8);

//mouth
noStroke();
fill(col1);
rectMode(CENTER);
//strokeWeight(r1*mouthS);
//curve(0,r1*mouthC,r1*-faceW*0.54,r1*mouthH,r1*faceW*0.54,r1*mouthH,0,r1*mouthC);
ellipse(0,faceW/2+20,10*mouthC,mouthC*5);

stroke(col1);
noFill();
strokeWeight(r1*0.05);
if(showBeard) {
for(int i = 0;i < beardCount; i++) {
point(r1*faceW*beardX[i],r1*faceH*beardY[i]);
}
}
popMatrix();
}

//------------------------------- colors -------------------------------

void initColors() {
col1 = getColor(false);
col2 = getColor(true);
while(!checkColorContrast(col1,col2)) {
col2 = getColor(true);
}
if(!checkDarkColor(col1,col2)) {
color tmpcol = col2;
col2 = col1;
col1 = tmpcol;
}
}

private color getColor(boolean rand) {
if(!rand) {
while(true) {
for(int i=0;i> 8 & 0xFF) + (col1 & 0xFF);
int value2 = (col2 >> 16 & 0xFF ) + (col2 >> 8 & 0xFF) + (col2 & 0xFF);
if(value1 > value2) {
return false;
}
else {
return true;
}
}

}