You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.1 KiB
85 lines
2.1 KiB
import processing.serial.*;
|
|
import processing.video.*;
|
|
|
|
int cellSize = 20;
|
|
int cols, rows, cellN;
|
|
int[] colorValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
Capture video;
|
|
|
|
void setup() {
|
|
size(640, 480);
|
|
frameRate(10);
|
|
colorMode(HSB, 100, 100, 100);
|
|
cols = width / cellSize;
|
|
rows = height / cellSize;
|
|
cellN = (cols)*(rows);
|
|
//colorMode(RGB, 255, 255, 255, 100);
|
|
|
|
video = new Capture(this, width, height);
|
|
video.start();
|
|
|
|
background(0);
|
|
}
|
|
|
|
void draw() {
|
|
if (video.available()) {
|
|
video.read();
|
|
video.loadPixels();
|
|
|
|
for(int i = 0; i<colorValues.length; i++) {
|
|
colorValues[i] = 0;
|
|
}
|
|
|
|
for (int i = 0; i < cols; i++) {
|
|
for (int j = 0; j < rows; j++) {
|
|
|
|
int x = i*cellSize;
|
|
int y = j*cellSize;
|
|
int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
|
|
|
|
// Get hue and brightness values
|
|
float hueVal = hue(video.pixels[loc]);
|
|
float briVal = brightness(video.pixels[loc]);
|
|
float satVal = saturation(video.pixels[loc]);
|
|
|
|
// Set pixel color
|
|
color c = color(hueVal, satVal, briVal);
|
|
|
|
// Check brightness values,
|
|
// if greater then 80% increace white counter,
|
|
// if lower then 20% increace black counter,
|
|
// otherwise increase color counters
|
|
if(briVal > 20 && briVal < 80) {
|
|
colorValues[int(hueVal/8.3)]++;
|
|
} else if(briVal <= 20) {
|
|
colorValues[12]++;
|
|
} else if(briVal >= 20) {
|
|
colorValues[13]++;
|
|
}
|
|
|
|
pushMatrix();
|
|
translate(x+cellSize/2, y+cellSize/2);
|
|
rectMode(CENTER);
|
|
fill(c);
|
|
noStroke();
|
|
rect(0, 0, cellSize+6, cellSize+6);
|
|
popMatrix();
|
|
}
|
|
}
|
|
|
|
// Print colors percentage values
|
|
for(int i = 0; i<colorValues.length-2; i++) {
|
|
//print(colorValues[i]/(float(cellN)/100) + "\t");
|
|
|
|
fill(color((i*8.3)+5, 100, 100));
|
|
rect(0, (i*5)+5, colorValues[i], 5);
|
|
}
|
|
|
|
fill(0);
|
|
rect(0, 60, colorValues[12], 5);
|
|
fill(100);
|
|
rect(0, 65, colorValues[13], 5);
|
|
println("");
|
|
}
|
|
}
|