Code:int imageWidth, imageHeight;
String imageFilename;
PImage picassoImage;
// This method sizes the output panel based on the size of the given image.
void setup() {
imageFilename = "E:/Dev/PhiLhoSoft/Processing/me.png";
loadPicassoImage(imageFilename);
imageWidth = picassoImage.width;
imageHeight = picassoImage.height;
size(imageWidth, imageHeight);
displayPicassoImage();
}
void draw() {
}
void mouseClicked() {
polarize();
println("Done");
displayPicassoImage();
}
void loadPicassoImage(String filename) {
picassoImage = loadImage(filename);
}
void displayPicassoImage() {
image(picassoImage, 0, 0);
}
// Polarizes the red value of all the pixels.
void polarize () {
float averageRed, sumRed;
sumRed = 0;
for (int i = 0; i < imageWidth; i++) {
for (int j = 0; j < imageHeight; j++) {
sumRed += red(picassoImage.get(i, j));
}
}
averageRed = sumRed / (width * height);
println(averageRed);
color cR = color(255, 0, 0), cBk = color(0, 0, 0);
for (int i = 0; i < imageWidth; i++) {
for (int j = 0; j < imageHeight; j++) {
if (red(picassoImage.get(i, j)) > averageRed)
{
picassoImage.set(i, j, cR);
}
else {
picassoImage.set(i, j, cBk);
}
}
}
}
I made the change on a click, so one can better see before/after...
Note: I tried with pixels[], but with same performances (16ms). Perhaps because on a PImage, get/set perform better than on sketch display? (where each change is displayed)