このサンプルは、動いた場所を特定するためのコードです。
//This code has been arranged by Yasushi Noguchi. Last updated on June 21, 2008.
/**
* Background Subtraction
* by Golan Levin.
*
* 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; //背景のピクセル
int noiseFilter = 100; //ノイズを拾わないためのフィルタ(値を増やすとフィルタが強くかかる)
Capture video; //キャプチャ映像用の変数
void setup() {
size(640, 480);
video = new Capture(this, width, height, 24);
//キャプチャーするビデオ画像の総ピクセル数
numPixels = video.width * video.height;
//現在のキャプチャ画像と比べるために背景画像用の配列を作る
backgroundPixels = new int[numPixels];
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];
//現在のピクセルのR, G, Bを抜き出す
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8 ) & 0xFF;
int currB = currColor & 0xFF;
//背景画像のR, G, Bを抜き出す
int bkgdR = (bkgdColor >> 16) & 0xFF;
int bkgdG = (bkgdColor >> 8 ) & 0xFF;
int bkgdB = bkgdColor & 0xFF;
//現在のピクセルから背景画像のピクセルの色を引いた絶対値
int diffR = abs(currR - bkgdR);
int diffG = abs(currG - bkgdG);
int diffB = abs(currB - bkgdB);
//noiseFilterの値よりも大きかったらpresenceSumに足していく
//そして、現在の色に更新
if(diffR + diffG + diffB > noiseFilter){
presenceSum += diffR + diffG + diffB;
pixels[i] = color(currR, currG, currB);
//次のコードの方が高速に実行できるが、ちょっと難しい
//pixels[i] = 0xFF000000 | (currR < < 16) | (currG << 8 ) | currB;
}
else{
//そうでない場合は黒
pixels[i] = color(0);
}
}
updatePixels(); //ピクセルを更新
//println(presenceSum); //変化したピクセルの総数をプリント
}
}
//マウスを押したときに、現在のフレームの画像をbackgroudPixelsにコピーする
void mousePressed() {
video.loadPixels();
arraycopy(video.pixels, backgroundPixels);
}
