Created
February 1, 2012 08:20
-
-
Save tatsuro-ueda/1715931 to your computer and use it in GitHub Desktop.
color-watagashi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.video.*; | |
Capture cam; | |
void setup() { | |
size(320, 240); | |
} | |
void draw() { | |
if (cam.available() == true) { | |
cam.read(); | |
float sum_red = 0; | |
float sum_green = 0; | |
float sum_blue = 0; | |
// 色値をRGB別に積算し平均を求める | |
for (int x = 0; x < cam.width; x++) | |
{ | |
for (int y = 0; y < cam.height; y++) | |
{ | |
color c = cam.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; | |
} | |
} | |
int R = (int)(sum_red / (cam.width * cam.height)); | |
int G = (int)(sum_green / (cam.width * cam.height)); | |
int B = (int)(sum_blue / (cam.width * cam.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 = cam.height * (R-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
heightG = cam.height * (G-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
heightB = cam.height * (B-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
} | |
else | |
{ | |
heightR = 0; heightG = 0; heightB = 0; // 真っ暗なとき | |
} | |
// 矩形を描画する | |
//strokeWeight(20); | |
//noFill(); // 塗りつぶさない | |
if(heightR != 0) | |
{ | |
rect(0, 0, cam.width, heightR); | |
} | |
println( | |
"R: " + R + " " + | |
"G: " + G + " " + | |
"B: " + B + " " + | |
"RGBmin: " + RGBmin + " " + | |
"heightR: " + heightR + " " + | |
"heightG: " + heightG + " " + | |
"heightB: " + heightB + " " | |
); | |
image(cam, 0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment