Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleBeispiel
collapsetrue
import processing.video.*;
Capture video;

PImage backgroundImage;
float threshold = 20;

void setup() {
  size(640, 480);
  video = new Capture(this, width, height, 30);
  video.start();
  backgroundImage = createImage(video.width, video.height, RGB);
}

void draw() {
  if (video.available()) {
    video.read();
  }
  
  loadPixels();
  video.loadPixels(); 
  backgroundImage.loadPixels();
  
  for (int x=0; x < video.width; x++) {
    for (int y=0; y < video.height; y++) {
      int loc = x + y * video.width; 
      color fgColor = video.pixels[loc]; 
      
      color bgColor = backgroundImage.pixels[loc];
      
      float r1 = red(fgColor);
      float g1 = green(fgColor);
      float b1 = blue(fgColor);
      float r2 = red(bgColor);
      float g2 = green(bgColor);
      float b2 = blue(bgColor);
      float diff = dist(r1, g1, b1, r2, g2, b2);
      
      if (diff > threshold) {
        pixels[loc] = fgColor;
      } else {
        pixels[loc] = color(0); 
      }
    }
  }

  updatePixels();
}

void mousePressed() {  
  backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  backgroundImage.updatePixels();
}

Hellster Punkt

...

Image Added

Für die direkte Steuerung eines Interfaces kann es hilfreich sein, zu wissen, wo sich der hellste Punkt in einem Bild befindet. Das Beispiel “P04_6_Video_Kamerabild_Hellster_Punkt” zeigt wie es geht. Dazu wird ein PVector erstellt und eine Variable, welche den jeweils hellsten Wert für das aktuelle Frame beinhaltet. Durch das Vergleichen der Helligkeitswerte im ganzen Frame kann sehr schnell der Hellste Punkt bestimmt werden.

Image Removed

Farbtracking

...

Code Block
titleBeispiel
collapsetrue
import processing.video.*;
Capture video;

PVector brightestPoint = new PVector(0,0);

void setup() {
  size(640, 480);
  video = new Capture(this, width, height, 30);
  video.start();
}

void draw() {
  if (video.available()) {
    video.read();
  }

  float brightness = 0;

  for (int x=0; x < width; x++) {
    for (int y=0; y < height; y++) {
      int loc = x + y * width;
      color c = video.pixels[loc];

      if (brightness(c) > brightness) {
        brightness = brightness(c);
        brightestPoint.x = x;
        brightestPoint.y = y;
      }
    }
  }
  
  image(video, 0, 0);
  ellipse(brightestPoint.x, brightestPoint.y, 20, 20);
}


Farbtracking

Image Added

Code Block
titleBeispiel
collapsetrue
import processing.video.*;
Capture video;

color trackColor; 

void setup() {
  size(640, 480);
  
  video = new Capture(this, width, height, 15);
  video.start();

  trackColor = color(255, 0, 0);
  smooth();
}

void draw() {
  if (video.available()) {
    video.read();
  }

  video.loadPixels();
  image(video, 0, 0);

  float worldRecord = 500; 

  int closestX = 0;
  int closestY = 0;
  
  PVector closestPoint = new PVector();
  
  for (int x=0; x < video.width; x++) {
    for (int y=0; y < video.height; y++) {
      int loc = x + y * video.width;
      color currentColor = video.pixels[loc];
      
      PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor));
      PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));
      float diff = currColorVec.dist(trackColorVec);
      
      if (diff < worldRecord) {
        worldRecord = diff;
        closestPoint.x = x;
        closestPoint.y = y;
      }
    }
  }

  if (worldRecord < 10) { 
    fill(trackColor);
    strokeWeight(4.0);
    stroke(0);
    ellipse(closestPoint.x, closestPoint.y, 50, 50);
  }
}

void mousePressed() {
  int loc = mouseX + mouseY * video.width;
  trackColor = video.pixels[loc];
}


Blob Detection

Weitere Informationen

...