Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created February 2, 2012 03:50
Show Gist options
  • Save tatsuro-ueda/1721327 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1721327 to your computer and use it in GitHub Desktop.
color-watagashi-prototype2.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();
if(context.enableRGB() == false)
{
println("Can't open the rgbMap, maybe the camera is not connected or there is no rgbSensor!");
exit();
return;
}
// align depth data to image data
context.alternativeViewPointDepthToImage();
size(context.depthWidth() , context.depthHeight());
}
void draw()
{
// update the cam
context.update();
//context.rgbImage().mask(context.sceneImage());
image(context.rgbImage(),0,0);
//image(context.depthImage(),0,0);
//image(context.sceneImage(),0,0);
drawRgbColorRect(context.rgbImage());
}
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; // 真っ暗なとき
}
// 矩形を描画する
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