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

Daily Creative Coding

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

カメラの基礎1

Webカメラの映像を表示する

f:id:aa_debdeb:20151105202554j:plain
/**
* Video
* 
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;

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

void draw(){
  image(camera, 0, 0);
}

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

カメラ映像を白黒にする

f:id:aa_debdeb:20151105202711j:plain
/**
* Monochrome Video
*
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;

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

void draw(){
  camera.loadPixels();
  loadPixels();
    for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      color c = camera.pixels[y * width + x];
      float grey = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
      pixels[y * width + x] = color(grey, grey, grey);
    }
  }
  updatePixels();
}

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

カメラ映像を2値化する

f:id:aa_debdeb:20151105202729j:plain
/**
* Binarized Video
*
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;

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

void draw(){
  camera.loadPixels();
  loadPixels();
  for(int x = 0; x < width; x += 1){
    for(int y = 0; y < height; y += 1){
      color c = camera.pixels[y * width + x];
      float grey = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
      if(grey > (float(mouseY) / height) * 255){
        pixels[y * width + x] = color(255);
      } else {
        pixels[y * width + x] = color(0);      
      }
    }
  }
  updatePixels();
}

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

カメラ映像の色を反転する

f:id:aa_debdeb:20151105202914j:plain
/**
* Reversed Color Image
*
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;

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

void draw(){
  camera.loadPixels();
  loadPixels();
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      color c = camera.pixels[y * width + x];
      pixels[y * width + x] = color(255 - red(c), 255 - green(c), 255 - blue(c));
    }
  }
  updatePixels();
}

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

空間微分する(輪郭を抽出する)

f:id:aa_debdeb:20151105203031j:plain
/**
* Space differentiated Video
*
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;

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

void draw(){
  camera.loadPixels();
  loadPixels();
  color[][] greys = new color[width][height];
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      color c = camera.pixels[y * width + x];
      float grey = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
      greys[x][y] = color(grey, grey, grey);
    }
  }
  for(int x = 0; x < width ; x += 1){
    for(int y = 0; y < height; y += 1){
      if(x == 0 || x == width - 1 || y == 0 || y == height - 1){
        pixels[y * width + x] = color(0);
      } else {
        float vg = - red(greys[x - 1][y - 1]) + 
                   - 2 * red(greys[x - 1][y]) +
                   - red(greys[x - 1][y + 1]) +
                   red(greys[x + 1][y - 1]) +
                   2 * red(greys[x + 1][y]) +
                   red(greys[x + 1][y + 1]);      
        float hg = - red(greys[x - 1][y - 1]) + 
                   - 2 * red(greys[x][y - 1]) +
                   - red(greys[x + 1][y - 1]) +
                   red(greys[x - 1][y + 1]) +
                   2 * red(greys[x][y + 1]) +
                   red(greys[x + 1][y + 1]);
         float grey = sqrt(sq(vg) + sq(hg));
         pixels[y * width + x] = color(grey, grey, grey);      
       }

    }
  }
  updatePixels();
}

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

時間微分する(動きを抽出する)

f:id:aa_debdeb:20151105203124j:plain
/**
* Time differentiated Video
*
* @author aa_debdeb
* @date 2015/11/07
*/

import processing.video.*;

Capture camera;
color[][] previousPixels;

void setup(){
  size(640, 480);
  camera = new Capture(this, width, height);
  camera.start();
  
  previousPixels = new color[width][height];
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      previousPixels[x][y] = 127;
    }
  }  
}

void draw(){
  camera.loadPixels();
  loadPixels();
  color[][] currentPixels = new color[width][height];
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      currentPixels[x][y] = camera.pixels[y * width + x];
    }
  }
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      float r = abs(red(previousPixels[x][y]) - red(currentPixels[x][y]));
      float g = abs(green(previousPixels[x][y]) - green(currentPixels[x][y]));
      float b = abs(blue(previousPixels[x][y]) - blue(currentPixels[x][y]));
      pixels[y * width + x] = color(r, g, b);
    }
  }
  updatePixels();
  previousPixels = currentPixels;
}

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