Februar 27, 2012
![](../../../../files/2012/02/bild2-300x231.jpg)
![](../../../../files/2012/02/bild1-300x231.jpg)
![](../../../../files/2012/02/bild3-300x232.jpg)
import processing.video.*; // importiert video library // Object variables ------------------------------------- Capture video; //initalisiert video PShape s; // erstellt einen Shape color trackColor; // erstellt eine Farbvariable ArrayList PunkteArray; ArrayList Farbe; PVector Koordinate; // Setup ------------------------------------------------ void setup() { PunkteArray = new ArrayList(); // Create an empty ArrayList Farbe = new ArrayList(); // Create an empty ArrayList size(1024,768); // erstellt das Fenster video = new Capture(this,1024,768,"IIDC FireWire Video",15); // zieht die Webcambilder // Start off tracking for red //trackColor = color(255,0,0); // gibt eine Track Farbe an 255,0,0 // load Shape s = loadShape("YaoMing-meme.svg"); // lädt die Grafik welche angezeigt werden soll shapeMode(CENTER); // setzt den Grafikmittelpunkt in die Mitte trackColor = color(255,0,0); smooth(); // kantenglättung } // Draw -------------------------------------------------- void draw() { // Capture and display the video if (video.available()) { // Video wird überprüft video.read(); } video.loadPixels(); // Pixels von Video werden analysiert image(video,0,0); // Video wird angezeigt // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat. float worldRecord = 500; // Variable worldRecord wird generiert und mit dem Wert 500 versehen. // XY coordinate of closest color int closestX = 0; // Variable int closestY = 0; // Variable // Begin loop to walk through every pixel for (int x = 0; x < video.width; x ++ ) { // Schlaufe x Werte for (int y = 0; y < video.height; y ++ ) { // Schlaufe y Werte int loc = x + y*video.width; // Pixelposition werd anze // What is current color color currentColor = video.pixels[loc]; // Farbe des Pixels wird ausgelesen float r1 = red(currentColor); float g1 = green(currentColor); float b1 = blue(currentColor); float r2 = red(trackColor); float g2 = green(trackColor); float b2 = blue(trackColor); // Using euclidean distance to compare colors float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking. // If current color is more similar to tracked color than // closest color, save current location and current difference if (d < worldRecord) { worldRecord = d; closestX = x; closestY = y; } } } if (worldRecord < 30) { // Draw a circle at the tracked pixel fill(trackColor,100); strokeWeight(1.0); stroke(255); Koordinate = new PVector(closestX, closestY); PunkteArray.add(Koordinate); Farbe.add(color(trackColor)); } for(int i=0; i < PunkteArray.size(); i++) { PVector Vector = (PVector) PunkteArray.get(i); float xPos = Vector.x; float yPos = Vector.y; for (int x = 0; x < Farbe.size(); x++) { color farbeAnzeigen = (color)(Integer)Farbe.get(i); // (Integer wird dafür verwendet, damit man die Color aus dem ArrayList auslesen kann. fill(farbeAnzeigen); } rect(Vector.x,Vector.y,30,30); } //println(FarbArray); } /* void FarbbereichAnalysieren(PImage img, int _x, int _y, int breite, int hoehe) { FarbArray = new ArrayList(); println(FarbArray); int r = 0; int g = 0; int b = 0; for (int i = _x; i < _x+breite; i++) { for (int j = _y; j < _y+hoehe; j++) { int FarbWert = int(color(img.get(i,j))); FarbArray.add (FarbWert); } } } */ // Mouse Pressed ------------------------------------------- void mousePressed() { // Save color where the mouse is clicked in trackColor variable int loc = mouseX + mouseY*video.width; trackColor = video.pixels[loc]; //FarbbereichAnalysieren(video,mouseX,mouseY,30,30); }