Skip to content

Instantly share code, notes, and snippets.

@volfegan
Last active June 25, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volfegan/ea0b336a86beea1b76885460302c7e00 to your computer and use it in GitHub Desktop.
Save volfegan/ea0b336a86beea1b76885460302c7e00 to your computer and use it in GitHub Desktop.
Custom threshold filter for Processing
//author Volfegan: https://twitter.com/VolfeganGeist
//resources:
//threshold filter https://processing.org/tutorials/pixels/
import java.util.UUID;
PImage img;
PImage sortedPixels; //image processed
float thresholdLevel = 0.5; //The level of brightness to threshold = [0.0 (black) ~ 1.0 (white)]
boolean save = false; //save the image with a random id
int width = 0;
int height = 0;
String filename;
//there is no file validation, so any non-img selected will crash the program
void fileSelected(File selection) {
if (selection == null) {
println("No image file selected.");
exit();
} else {
String filepath = selection.getAbsolutePath();
filename = selection.getName();
int pos = filename.lastIndexOf(".");
if (pos != -1) filename = filename.substring(0, pos);
println("File selected " + filepath);
// load file here
img = loadImage(filepath);
}
}
void interrupt() {
while (img==null) delay(200);
}
void settings() {
selectInput("Select an image file to process:", "fileSelected");
interrupt(); //interrupt process until img is selected
width = img.width;
height = img.height;
if (width > 1920) {
int resizer = width / 1200;
width = 1200;
height = height / resizer;
img.resize(width, height);
}
if (height > 1080) {
int resizer = height / 900;
height = 900;
width = width / resizer;
img.resize(width, height);
}
//the canvas window size will be according to the img size
size(width, height);
}
void setup() {
// Create an opaque image of the same size as the original
sortedPixels = createImage(img.width, img.height, RGB);
sortedPixels = img.get();
//Threshold
sortedPixels = thresholdFilter(sortedPixels, thresholdLevel);
background(0);
image(sortedPixels, 0, 0);
if (save) {
//save the image with a random id
save("image_"+UUID.randomUUID().toString().replace("-", "")+".png");
}
}
//return a threshold brightness PImage by a level parameter. The parameter = [0.0 (black) ~ 1.0 (white)]
public PImage thresholdFilter(PImage image, float parameter) {
if (parameter > 1 || parameter < 0) return null;
float threshold = 255*parameter;
// We are going to look at both image's pixels
image.loadPixels();
// Create an opaque image of the same size as the original
PImage processedImg = createImage(image.width, image.height, RGB);
processedImg.loadPixels();
for (int x = 0; x < image.width-1; x++) {
for (int y = 0; y < image.height-1; y++ ) {
int loc = x + y*image.width;
// Test the brightness against the threshold
if (brightness(image.pixels[loc]) > threshold) {
processedImg.pixels[loc] = color(255); // White
} else {
processedImg.pixels[loc] = color(0); // Black
}
}
}
// State that there are changes to processedImg.pixels[]
processedImg.updatePixels();
return processedImg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment