Processingネタを1年以上振りに書きます。
もうほとんど触ってもいませんでしたProcessing。
そんなProcessingちゃんを久しぶりにちょこっと書いてみました。
Processingの拡張ライブラリでVideoライブラリがあります。
Macの場合、モニタの上部分についてるあのカメラを使ってProcessingにキャプチャ画像を取り込むことができるのです。
まずはカメラを使う準備から。
下記のソースコードでカメラから取り込んだキャプチャをProcessingに渡します。
import processing.video.*;
Capture cam;
void setup(){
size(420, 320);
cam = new Capture(this, width, height, 15);
}
void captureEvent(Capture cam){
cam.read();
}
void draw(){
image(cam, 0, 0);
}
そしたらピクセル解析の準備をします。
loadPixels()をセットすることで、pixels[]にピクセルデータを渡します。
void draw(){
image(cam, 0, 0);
loadPixels(); //ディスプレイウィンドウへピクセルデータをロードします。
}
ここからはキャプチャを加工するための関数を定義して読み出します。
rect版モザイク

import processing.video.*;
Capture cam;
void setup(){
size(420, 320);
cam = new Capture(this, width, height, 15);
}
void captureEvent(Capture cam){
cam.read();
}
void draw(){
image(cam, 0, 0);
loadPixels();
mosaic(10, 10);
}
void mosaic(int w, int h){
for( int y = 0; y < height; y += h ){
for( int x = 0; x < width; x += w ){
color c = pixels[ y * width + x ];
fill(c);
rect(x, y, w, h);
}
}
}
ellipse版モザイク

import processing.video.*;
Capture cam;
void setup(){
size(420, 320);
cam = new Capture(this, width, height, 15);
}
void captureEvent(Capture cam){
cam.read();
}
void draw(){
image(cam, 0, 0);
loadPixels();
fillMosaic(cam, 10, 10);
}
void fillMosaic(PImage img, int w, int h){
for( int y = 0; y < height; y += h ){
for( int x = 0; x < width; x += w ){
color c = pixels[ y * width + x ];
fill(c);
ellipse(x, y, brightness(c)/7.0, brightness(c)/7.0);
}
}
}
斬新な何か

import processing.video.*;
Capture cam;
void setup(){
size(420, 320);
cam = new Capture(this, width, height, 15);
}
void captureEvent(Capture cam){
cam.read();
}
void draw(){
image(cam, 0, 0);
loadPixels();
fractalSolidMosaic(cam, 10, 10);
}
void fractalSolidMosaic(PImage img, int w, int h){
for( int y = 0; y < height; y += h ){
for( int x = 0; x < width; x += w ){
color c = pixels[ y * width + x ];
fill(c);
pushMatrix();
translate(x, y);
rotate(brightness(c));
rect(0, 0, brightness(c)/2.0, 2);
popMatrix();
}
}
}
実際に動かしてもらった方が面白いと思います。
次回はAR系に挑戦してみようと思います。
[参考記事】yoppa.org - Processingで画像データを扱う