Skip to content

Instantly share code, notes, and snippets.

@federico-pepe
Last active September 29, 2018 11:34
Show Gist options
  • Save federico-pepe/b46d44944c5386b22a9caf629cec7631 to your computer and use it in GitHub Desktop.
Save federico-pepe/b46d44944c5386b22a9caf629cec7631 to your computer and use it in GitHub Desktop.
Coding Rescue #2
/*
* Coding Rescue #2 - Binarizzare un'immagine
* Federico Pepe, 26.10.2017
* http://blog.federicopepe.com/processing
*/
float threshold = 150;
PImage immagine, imgbin;
boolean bin = true;
void setup() {
size(700, 542);
immagine = loadImage("corgi-photo.jpg");
image(immagine, 0, 0);
frameRate(2);
}
void draw() {
threshold = calculateThreshold();
if (bin) {
imgbin = immagine.copy();
image(binarize(imgbin), 0, 0);
} else {
image(immagine, 0, 0);
}
}
void mouseClicked() {
bin = !bin;
}
PImage binarize(PImage img) {
img.loadPixels();
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++) {
int loc = x + y * img.width;
if (brightness(img.pixels[loc]) > threshold) {
img.pixels[loc] = color(0);
} else {
img.pixels[loc] = color(255);
}
}
}
img.updatePixels();
return img;
}
float calculateThreshold() {
float d = dist(pmouseX, pmouseY, mouseX, mouseY);
threshold = d;
return threshold;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment