Created
November 5, 2011 09:55
-
-
Save ipa/1341338 to your computer and use it in GitHub Desktop.
convert rgb picture to monochrome with
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
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