Skip to content

Instantly share code, notes, and snippets.

@ipa
Created November 5, 2011 09:55
Show Gist options
  • Save ipa/1341338 to your computer and use it in GitHub Desktop.
Save ipa/1341338 to your computer and use it in GitHub Desktop.
convert rgb picture to monochrome with
PImage img;
void setup() {
size(150, 200);
noLoop();
img = loadImage("kaefer.png");
}
void draw() {
image(img, 0, 0);
calcThreshold();
for (int x = 0; x < width; x = x + 1) {
for (int y = 0; y < height; y = y + 1) {
color col = get(x, y);
set(x, y, monochrome(col));
}
}
}
color threshold;
void calcThreshold(){
int r, g, b;
r = g = b = 0;
int iPixels = width * height;
for (int x = 0; x < width; x = x + 1) {
for (int y = 0; y < height; y = y + 1) {
color col = get(x, y);
r += red(col);
g += green(col);
b += blue(col);
}
}
threshold = color(r/iPixels, g/iPixels, b/iPixels);
}
color monochrome(color col){
return col < threshold ? color(0) : color(255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment