■
WebDesigning 11月号を読んで.
ちょっとやりたい事が見つかったので,取り敢えずProcessingのサンプルコードを読んでみる.
Open->Library->Video(Capture)->Background Subtraction
カメラを使って背景差分を取りましょう.
/** * Background Subtraction * by Golan Levin. * * 単純なフレームを用いた背景差分テクニックを使って,人や物の存在を発見する. * 背景画像を初期化するにはkeyを叩けば良いです. * * Detect the presence of people and objects in the frame using a simple * background-subtraction technique. To initialize the background, press a key. */ //ライブラリーを読み込みます. import processing.video.*; int numPixels; int[] backgroundPixels; Capture video; void setup() { // もしカメラの解像度が低すぎたら640x480に変更します size(640, 480); video = new Capture(this, width, height, 24); numPixels = video.width * video.height; // 配列を作って,背景画像を格納する. backgroundPixels = new int[numPixels]; // Make the pixels[] array available for direct manipulation loadPixels(); } void draw() { if (video.available()) { video.read(); // 新しいビデオのフレームを獲得します video.loadPixels(); // ビデオのフレームからピクセルにアクセスできるようにします. // 現在のフレームと,貯蔵された背景画像との差分をだす int presenceSum = 0; for (int i = 0; i < numPixels; i++) { // 各々のピクセル単位で比較する // ある場所(ピクセル)の現在の色(クラス)を取得します. // 背景画像も同様です. color currColor = video.pixels[i]; color bkgdColor = backgroundPixels[i]; // ここが,現在のあるポイントの色(赤・緑・青)情報 int currR = (currColor >> 16) & 0xFF; int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // ここが,背景のあるポイントの色(赤・緑・青)情報 int bkgdR = (bkgdColor >> 16) & 0xFF; int bkgdG = (bkgdColor >> 8) & 0xFF; int bkgdB = bkgdColor & 0xFF; // 赤・緑・青の値の差分を取ります.abs関数を使って絶対値にしています. int diffR = abs(currR - bkgdR); int diffG = abs(currG - bkgdG); int diffB = abs(currB - bkgdB); // トータルでどの程度違うかを合計しています. presenceSum += diffR + diffG + diffB; // 差分画像をレンダリングします. pixels[i] = color(diffR, diffG, diffB); // 下記のコードは上記のコードと処理は同じですが,テクニカルで高速です. //pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB; } updatePixels(); // Notify that the pixels[] array has changed println(presenceSum); // 動きの合計を標準出力します. } } // キーが押された時に,背景画像をキャプチャーし,背景画像として記録します, void keyPressed() { video.loadPixels(); arraycopy(video.pixels, backgroundPixels); }
うーん.これを少しいじりますかなぁ〜