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 & HelpSyntax Questions › pixels[] and finding light/dark areas of an image
Page Index Toggle Pages: 1
pixels[] and finding light/dark areas of an image (Read 570 times)
pixels[] and finding light/dark areas of an image
Apr 24th, 2009, 9:38am
 
Hi all,

I have a sketch that loads a high contrast image with lines that draw over it created with a simple particle system.

What I'm struggling with is attempting to have the lines/particles originate in random dark areas of the image.

My code isn't breaking but the lines are starting in the upper left or other areas.

I have to guess I'm not approaching this the most effectively. What I'm trying to do is find the dark areas of the "face" image, store them and then refer to only those points when I'm generating the particles.

Here's the code of what I've been working, the particles are rendered in draw and there's an update in the particle class (originally from some perlin noise code by lenny IIRC).

TIA

Quote:
    PImage face;
    PImage darkAreas;
    p p[]; 
    
    void setup(){
      size(509,800,P2D);
      smooth();
    
      face = loadImage("face.jpg");
      image(face, 0, 0);  // img, x, y, width, height
      darkAreas = createImage(face.width, face.height, RGB);
      
      float threshold = 127;
      face.loadPixels();
      darkAreas.loadPixels();
      
      for (int x=0; x < face.width; x++) {
        for (int y=0; y < face.height; y++){
        int loc = x + y*face.width;
        if (brightness(face.pixels[loc]) < threshold) { //???
          darkAreas.pixels[loc] = color(0);
          }
        }
      }
    
      p=new p[100]; //number of particles
      for(int i=0; i<p.length; i++)p[i]=new p(i/1.0, int(random(darkAreas.pixels[i])), int(random(darkAreas.pixels[i])));  
    } 
    
    void draw(){  
      //frameRate(30);
      //image(face, 0, 0);  // img, x, y, width, height
   
      stroke(255, 253, 235);
      for(int i=0; i<p.length; i++) p[i].update();//render particles
    }
    
    void mousePressed() {
      image(face, 0, 0);  // img, x, y, width, height
    
    }
    
    class p {
      float id,x,y,xp,yp,s,d;
    
      public p(float _id, float _x, float _y) {
        id=_id; 
        x=xp=_x; 
        y=yp=_y; 
        s=random(1,10); // SPEED (orig. 2,7)///////////////////////
    
      } 
    
      void update(){
    
        id+=0.01; 
        d=(noise(id,x/mouseY,y/mouseY)+0.5)*mouseY;  //original 0.5
        x+=cos(radians(d))*s;
        y+=sin(radians(d))*s;
    
        if(x<-10) x=xp=width+10;//offscreen rewind
        if(x>width+100)x=xp=-10;
        if(y<-10) y=yp=height+10;
        if(y>height+10)y=yp=-10;
    
        stroke(10);    
        line(xp,yp,x,y);
        xp=x; 
        yp=y; 
      }
    }  

Re: pixels[] and finding light/dark areas of an image
Reply #1 - Apr 24th, 2009, 5:37pm
 
Quote:
What I'm struggling with is attempting to have the lines/particles originate in random dark areas of the image.

i guess. if you think it doesn't work properly at the moment maybe its because of this
int(random(darkAreas.pixels[i]))

when you create the particles.
i don't think that makes any sense, because you dont get any x,y coordinates there.
you can save the darkPoint in an ArrayList
shuffle them and then give them for the particles later. sorry for my english Smiley i mean it like this:
Code:

PImage face;
//   PImage darkAreas;
p p[];
ArrayList darkPoints;

void setup(){
 size(509,800,P2D);
 smooth();

 face = loadImage("face.jpg");
 image(face, 0, 0,width,height);  // img, x, y, width, height
 //    darkAreas = createImage(face.width, face.height, RGB);
 ArrayList darkPoints= new ArrayList();
 float threshold = 10;
 loadPixels();
 // darkAreas.loadPixels();

 for (int x=0; x < width; x++) {
   for (int y=0; y < height; y++){
int loc = x + y*width;
if (brightness(pixels[loc]) < threshold) { //???
 // darkAreas.pixels[loc] = color(0);
 darkPoints.add(new Point(x,y));
}
   }
 }
 Collections.shuffle(darkPoints);
 p=new p[100]; //number of particles
 for(int i=0; i<p.length; i++)p[i]=new p(i/1.0, ((Point)darkPoints.get(i)).x, ((Point)darkPoints.get(i)).y);  
}

void draw(){  
 //frameRate(30);
 //image(face, 0, 0);  // img, x, y, width, height

 stroke(255, 253, 235);
 for(int i=0; i<p.length; i++) p[i].update();//render particles
}

void mousePressed() {
 image(face, 0, 0);  // img, x, y, width, height

}

class p {
 float id,x,y,xp,yp,s,d;

 public p(float _id, float _x, float _y) {
   id=_id;
   x=xp=_x;
   y=yp=_y;
   s=random(1,10); // SPEED (orig. 2,7)///////////////////////

 }

 void update(){

   id+=0.01;
   d=(noise(id,x/mouseY,y/mouseY)+0.5)*mouseY;  //original 0.5
   x+=cos(radians(d))*s;
   y+=sin(radians(d))*s;

   if(x<-10) x=xp=width+10;//offscreen rewind
   if(x>width+100)x=xp=-10;
   if(y<-10) y=yp=height+10;
   if(y>height+10)y=yp=-10;

   stroke(10);    
   line(xp,yp,x,y);
   xp=x;
   yp=y;
 }
}  




i don't know that image you use. maybe you can post it.
you dont need loadpixels for images. i need it here because i used an image that ha not the size as the frame and had to scale it. i rhink you can figure it  out.
ra
Re: pixels[] and finding light/dark areas of an image
Reply #2 - Apr 26th, 2009, 5:38pm
 
Thank you very much, that worked perfectly! I wasn't familiar with Array Lists but that did exactly what I wanted.

I'll upload the sketch with image when I get a chance but the picture is a self portrait with very heavy contrast. The lines now emerge from the shadowy areas and swarm around.

Thanks again!

Page Index Toggle Pages: 1