We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Polarize (Read 576 times)
Polarize
Oct 14th, 2009, 2:47pm
 
Hello. I just began to use processing and I am quite lost in trying to polarize the RGB values of a picture. Here is my code so far and I would be very grateful if anyone can give some help. Thanks!

// Shaun Chun

int imageWidth, imageHeight, width, height, colorAvg;
float averageRed, sumRed;
String imageFilename;
PImage image;

// This method sizes the output panel based on the size of the given image.

void setup() {
 imageFilename = "picasso-weeping-woman-1937.jpg";
 loadPicassoImage(imageFilename);
 imageWidth = image.width;
 imageHeight = image.height;
 width = imageWidth;
 height = imageHeight;
 size(width, height);
 background(255);
 displayPicassoImage();
 polarize();
}

void draw() {
 polarize();
}

void loadPicassoImage(String filename) {
 image = loadImage(filename);
}

void displayPicassoImage() {
 image(image, 0, 0);
}



// Polarizes the red value of all the pixels.

void polarize () {
 
 sumRed = 0;

 for (int i = 0; i < imageWidth; i++) {
   for (int j = 0; j < imageHeight; j++) {
     sumRed += red(image.get(i, j));
   }
 }
 
averageRed = sumRed / (width * height);
 color c;
 
 for (int i = 0; i < imageWidth; i++) {
   for (int j = 0; j < imageHeight; j++) {
     if (red(image.get(i, j)) > averageRed)
     {
       image.set(0, 0, color(red(255)));
     }
     else {
       image.set(0, 0, color(red(0)));
     }
   }
 }

}
Re: Polarize
Reply #1 - Oct 15th, 2009, 2:41am
 
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)
Page Index Toggle Pages: 1