読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

カメラから取得した映像をラインで表現する

f:id:aa_debdeb:20160306192901j:plain
/**
* Line Camera
* 
* @author aa_debdeb
* @date 2016/03/10
*/

import processing.video.*;

int pointNum = 1500;
float nearDist = 50;
PVector[] points;
boolean[][] distMap;
Capture camera;

void setup(){
  size(640, 480);
  points = new PVector[pointNum];
  for(int i = 0; i < pointNum; i++){
    points[i] = new PVector(random(width), random(height));
  }
  distMap = new boolean[pointNum][pointNum];
  for(int i = 0; i < pointNum; i++){
    for(int j = i; j < pointNum; j++){
      float d = PVector.dist(points[i], points[j]);
      if(d <= nearDist){
        distMap[i][j] = distMap[j][i] = true;
      } else {
        distMap[i][j] = distMap[j][i] = false;
      }
    }
  }
  camera = new Capture(this, width, height);
  camera.start();
}

void draw(){
  println(frameCount);
  background(255);
  stroke(0);
  strokeWeight(0.3);
  noFill();
  camera.loadPixels();
  boolean[] isBlack = new boolean[pointNum];
  for(int i = 0; i < pointNum; i++){
    PVector p = points[i];
    color c = camera.pixels[int(p.y) * camera.width + int(p.x)];
    float gray = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
    if(gray < map(mouseX, 0, width, 0, 255)){
      isBlack[i] = true;
    } else {
      isBlack[i] = false;
    }
  }
  for(int i = 0; i < pointNum - 2; i++){
    if(!isBlack[i]){continue;}
    for(int j = i + 1; j < pointNum - 1; j++){
      if(!isBlack[j] || !distMap[i][j]){continue;}
      line(points[i].x, points[i].y, points[j].x, points[j].y);
    }
  }
}

void captureEvent(Capture camera){
  camera.read();
}