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.
IndexProgramming Questions & HelpPrograms › [solved][don't know where the problem is at all]
Page Index Toggle Pages: 1
[solved][don't know where the problem is at all] (Read 858 times)
[solved][don't know where the problem is at all]
May 26th, 2009, 8:28pm
 
I tried to write a programm that generates black/white noise (a bit like a TV-set).
The pixels shall flicker from black to white at different speeds, based on the grey values of a b/w image. Seems to work so far, but:
white areas tend to become white instead of true random noise
thank you for helping me :)

screenshots: http://q-garden.de/screens/tmp_51_19693.png
code:http://pastie.org/490935 (with nice colours) or: Code:
void setup() {//runs once

 //vars for setup
 int wid = 400; //width
 int hig = 400; //higth
 PImage akt_bild;


 //setup
 size(wid, hig);// Set the window size in pixels
 frameRate(30);//per second

 //one-time presedures
 akt_bild = loadImage("image.jpg"); //load image to add
 image(akt_bild, 0, 0); //screen image
 loadPixels(); //get pixels from screen to pixel[] array

 for (int pixel=0; pixel < wid*hig; ++pixel) { //do this with each pixel
   if(round(random(1)) == 1) { //with random: black or white
     pixels[pixel] = color (255, 255, 255);
   }
   else{
     pixels[pixel] = color (0, 0, 0);
   }
   lastframe[pixel] = pixels[pixel];
 }


}






//vars
int wid = 400; //width
int hig = 400; //higth
int[] lastframe = new int[wid*hig];
int off = 0;
int count = 0;
int maxi = 30;

void draw() { //frame by frame
 if(count < (maxi-1)) {            //stoper part1
   count = count + 1;
 }
 else {
   off = 1;
 }


 PImage akt_bild;
 akt_bild = loadImage("data/image.jpg"); //load image to add
 image(akt_bild, 0, 0); //screen image
 loadPixels(); //get pixels from screen to pixel[] array

 for (int stufen=1; stufen < 31; ++stufen) {//splitting the image into 30 grey values that are overdrawn by different chances
   for (int pixel=0; pixel < wid*hig; ++pixel) { //do this with each pixel
     if ((pixels[pixel] & 0xFF) > (256-9*stufen) && (pixels[pixel] & 0xFF) <= (256-9*(stufen-1))){ //bitshift gives color value (brightnes since it's b/w) and if it's in the dark are (beneath 8 of 256) do this
       if(random(256) > (256-stufen)) { //for a chance of 256/256 overwrite the color
         if(round(random(1)) == 1) { //with random: black or white
           pixels[pixel] = color (0, 0, 0);
         }
         else{
           pixels[pixel] = color (255, 255, 255);
         }
         lastframe[pixel] = pixels[pixel];
       }
       else{
         pixels[pixel] = lastframe[pixel];      
       }
     }
   }
 }
 updatePixels(); //write pixels[] to screen
 println("loops " + (pixels[80000] & 0xFF)); //yes it loops, but does not change pixels any more
 //saveFrame("data/merge_1#####.tif");//output a snapshot


 if(off == 1){//stoper part2
 //  noLoop();
 }
}


P.S.:btw, load/updatePixels() seems to work only if I reload my image.jpg too, why?
Re: [don't know where the problem is at all]
Reply #1 - May 27th, 2009, 4:36am
 
Interesting concept, but there is lot to say on the code... Smiley

- size() should be the first instruction of setup() and should have constant values (not variables). Once it is called, you can use the width and height variables to get the dimensions. That would avoid to have redundant and duplicate variables (local and global?).
- As I often say, you should avoid doing a loadImage during draw, even more as the image is always the same. Just keep it in a PImage.
- You load the image image.jpg then data/image.jpg?

I will try and fix the code, perhaps the solution will come with these changes... Smiley
Re: [don't know where the problem is at all]
Reply #2 - May 27th, 2009, 6:11am
 
Here is the "cleaned up" version. I show techniques you might have not seen yet, like using the pixels of an offscreen image and copying the image to avoid repeatedly load it.
The result is roughly the same as the code you shown, so I haven't fixed your problem, I fear, although on the image I used I don't see the effect you describe, I just have a phantom blurred view of the visage.
I might try a slightly different algorithm (not sure if I fully understand the current one...).
Code:
PImage akt_bild;
int[] lastFrame;
int pixelNb;

void setup()
{
size(400, 400);
pixelNb = width * height;
frameRate(30);
// Reference image
akt_bild = loadImage("image.jpg");

// to keep the old color of a pixel if the color change is not triggered
lastFrame = new int[pixelNb];
for (int pixel = 0; pixel < pixelNb; pixel++)
{
if (random(1) > 0.5)
{
lastFrame[pixel] = #FFFFFF;
}
else
{
lastFrame[pixel] = #000000;
}
}
}

void draw()
{
PImage displayedImage = akt_bild.get(); // Copy the image
for (int stufen = 1; stufen < 31; stufen++) //splitting the image into 30 grey values that are overdrawn by different chances
{
for (int pixel = 0; pixel < pixelNb; pixel++)
{
int brightness = displayedImage.pixels[pixel] & 0xFF;
if (brightness > 256 - 9 * stufen && brightness <= 256 - 9 * (stufen - 1))
{
if (random(256) > 256 - stufen)
{
int c;
if (random(1) > 0.5)
{
c = #000000;
}
else
{
c = #FFFFFF;
}
displayedImage.pixels[pixel] = lastFrame[pixel] = c;
}
else
{
displayedImage.pixels[pixel] = lastFrame[pixel];
}
}
}
}
displayedImage.updatePixels();
image(displayedImage, 0, 0);
}
Re: [don't know where the problem is at all]
Reply #3 - May 27th, 2009, 6:19am
 
thank you very very much for cleaning up my mess, very intresting to see Smiley
Sadly the bright Areas still tend to become white: http://q-garden.de/screens/tmp_52_42607.png I thought it should be really random noise when I make a screenshot.

Greetings from here,
León

edit: and your timing is great to, just got back to work on it Smiley

edit2: oh and about the algorithm, I devide the pixels in 30 sections, from bright to dark. The dark pixels tend to change their color more frequently than the bright areas, so the noise should be "moving"/flickering faster in the dark areas and nearly stand still in the bright areas. (I will reverse that later think, but I can just reverse the input image, that will be the easiest to do).
Somehow it looks like the bright areas tend to change (or overwrite) their value to white, even if all color set actions are inside the random if which should make an equal distribution of black and white everywhere.
Re: [don't know where the problem is at all]
Reply #4 - May 27th, 2009, 6:22am
 
Here is a new version corresponding more to what you describe. It is much faster (no need to loop 30 times on each pixel!) so I reduced the frame rate...
Code:
PImage akt_bild;
int[] lastFrame;
int pixelNb;
int bandNb = 30;

void setup()
{
size(400, 400);
pixelNb = width * height;
frameRate(10);
// Reference image
akt_bild = loadImage("image.jpg");

// to keep the old color of a pixel if the color change is not triggered
lastFrame = new int[pixelNb];
for (int pixel = 0; pixel < pixelNb; pixel++)
{
if (random(1) > 0.5)
{
lastFrame[pixel] = #FFFFFF;
}
else
{
lastFrame[pixel] = #000000;
}
}
}

void draw()
{
PImage displayedImage = akt_bild.get(); // Copy the image
for (int pixel = 0; pixel < pixelNb; pixel++)
{
int brightness = akt_bild.pixels[pixel] & 0xFF;
int band = bandNb * brightness / 256;
if (random(bandNb) > band)
{
int c;
if (random(1) > 0.5)
{
c = #000000;
}
else
{
c = #FFFFFF;
}
displayedImage.pixels[pixel] = lastFrame[pixel] = c;
}
else
{
displayedImage.pixels[pixel] = lastFrame[pixel];
}
}
displayedImage.updatePixels();
image(displayedImage, 0, 0);
}
Re: [don't know where the problem is at all]
Reply #5 - May 27th, 2009, 6:27am
 
thtas quite good, but I can see, the background pixels change their value all at once and I dont have anything between moving and standing still.
(I edited my last post for a 2nd time, maybe that helps)

edit: btw: if it runs in realtime 30fps it's nice, but I can live with exporting it to a movie(frames) for now

edit2: have to correct one line "        if (random(256) > 256 - 9 * (stufen-1))" is correct
but doesnt change my problem either...
Re: [don't know where the problem is at all]
Reply #6 - May 27th, 2009, 8:54am
 
cougarten wrote on May 27th, 2009, 6:27am:
I can see, the background pixels change their value all at once and I dont have anything between moving and standing still.

I don't agree. Increase the contrast on your image, you will see dark areas changing fast, white areas seeming stable, but actually you can see them changing from time to time.
Re: [don't know where the problem is at all]
Reply #7 - May 27th, 2009, 9:11am
 
oh you are right, than I thank you very much Smiley
Page Index Toggle Pages: 1