Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created February 2, 2012 06:12
Show Gist options
  • Save tatsuro-ueda/1721839 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1721839 to your computer and use it in GitHub Desktop.
color-watagashi-prototype3.pde
import SimpleOpenNI.*;
SimpleOpenNI context;
void setup()
{
context = new SimpleOpenNI(this);
// 深度カメラを有効にする
if(context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
// 人物検出を有効にする
context.enableScene();
// RGBカメラを有効にする
if(context.enableRGB() == false)
{
println("Can't open the rgbMap, maybe the camera is not connected or there is no rgbSensor!");
exit();
return;
}
// 画像データと深度データの位置合わせをする
context.alternativeViewPointDepthToImage();
// キャンバスの大きさ
size(context.depthWidth() , context.depthHeight());
}
void draw()
{
// カメラ情報を更新する
context.update();
// sceneImageの人の部分を取り出すマスク用画像を作る
PImage maskImg = makeImgForMask(context.sceneImage());
PImage maskedImg = context.rgbImage(); // RGBカメラの映像がマスク対象
maskedImg.mask(maskImg); // 人物の形で繰り抜いて
image(maskedImg, 0, 0); // 表示する
drawRgbColorRect(maskedImg); // RGB分析して四角形を描画する
}
// 深度映像から人物だけを抜き出すようなマスク用画像を返す
PImage makeImgForMask(PImage img)
{
color cBlack = color(0, 0, 0);
color cWhite = color(255, 255, 255);
for (int x = 0; x < img.width; x++)
{
for (int y = 0; y < img.height; y++)
{
color c = img.get(x, y);
// 人が写っていない白、灰色、黒はRGB値が同じ
if (red(c) == green(c) & green(c) == blue(c))
{
img.set(x, y, cBlack); // 黒でマスクする
}
// 何らかの色が付いている部分は人が写っている
else
{
img.set(x, y, cWhite); // 白で人の部分を残す
}
}
}
return img;
}
// 画像のRGB値を調べて長方形を描画する
void drawRgbColorRect(PImage img)
{
// 色値をRGB別に積算する
float sum_red = 0;
float sum_green = 0;
float sum_blue = 0;
for (int x = 0; x < img.width; x++)
{
for (int y = 0; y < img.height; y++)
{
color c = img.get(x, y);
float tmp_red = red(c);
float tmp_green = green(c);
float tmp_blue = blue(c);
sum_red += tmp_red;
sum_green += tmp_green;
sum_blue += tmp_blue;
}
}
// RGB別に色値の平均を求める
int R = (int)(sum_red / (img.width * img.height));
int G = (int)(sum_green / (img.width * img.height));
int B = (int)(sum_blue / (img.width * img.height));
// 最小色値を求める
int RGBlist[] = {R, G, B};
int RGBmin = RGBlist[0];
for (int i = 0; i < 3; i++)
{
if (RGBlist[i] < RGBmin)
{
RGBmin = RGBlist[i];
}
}
// 最小色値に基づいてRGB色値を正規化する
int heightR, heightG, heightB;
if (RGBmin != 0)
{
heightR = img.height * (R-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin));
heightG = img.height * (G-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin));
heightB = img.height * (B-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin));
}
else
{
heightR = 0; heightG = 0; heightB = 0; // 真っ暗なとき
}
// 半透明な矩形をRGBそれぞれ描画する
colorMode(RGB, 256);
if(heightR != 0) // 赤
{
fill(255, 0, 0, 64);
rect(0, 0, img.width, heightR);
}
if(heightG != 0) // 緑
{
fill(0, 255, 0, 64);
rect(0, heightR, img.width, heightR + heightG);
}
if(heightB != 0) // 青
{
fill(0, 0, 255, 64);
rect(0, heightR + heightG, img.width, heightR + heightG + heightB);
}
// デバッグ用
println(
"R: " + R + " " +
"G: " + G + " " +
"B: " + B + " " +
"RGBmin: " + RGBmin + " " +
"heightR: " + heightR + " " +
"heightG: " + heightG + " " +
"heightB: " + heightB + " "
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment