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 › problem with pulling color
Page Index Toggle Pages: 1
problem with pulling color? (Read 325 times)
problem with pulling color?
Jan 28th, 2009, 2:20pm
 
Hi Guys, not sure whats going wrong- but i'd like to load an img, pull the color values from it at specific locations. I can get it to work at one location (width/2, height/2) but once i try to pull from multiple locations - no luck. I've tried 2 methods, get(red(x,y)) and something like img.pixel[i] -- code below - any ideas?
Quote:
//different method for pulling color out... 

PImage img;

//______________________________________________________________________
void setup(){
  img = loadImage("Photo92.jpg");
  size(img.width,img.height);
  noLoop();

}//setup
//______________________________________________________________________
void draw(){

  image(img,0,0);
  
//works fine. 

  println(  " red is "   + red(get(width/2, height/2))
          + " green is " + green(get(width/2, height/2))
          + " blue is "  + blue(get(width/2, height/2)));


//doesn't work at all.

  for(int i=0; i < width; i+=50){
    for(int j=0; j < height; i+=50){
  
        println(  " red is "   + red(get(i,j))
                + " green is " + green(get(i,j))
                + " blue is "  + blue(get(i,j)));

    }//for
  }//for

  
}//draw

//______________________________________________________________________

Re: problem with pulling color?
Reply #1 - Jan 28th, 2009, 2:22pm
 
here is the goal of the project, but with this method i get a NullPointerException - no idead where to go from that.

Quote:
/*color sampler
-the goal of this is to read an image, and pull a number of colors out of it. 
-could specifiy an even distrubution of colors, or weighted toward RGB or some o
ther method. 

-need to pull out and display the colors, then store the color values in an exte
rnal file to be used later

*/


PImage img;

//color r[];
//color g[];
//color b[];

int pallet[];

int here, psize, csize, k;

//----------------------------------------------------------------
void setup(){
    //colorMode(RGB, 255);
  
    img = loadImage("test2.jpg");
    size (img.width, img.height);
  
    csize = img.width*img.height+200; //how big does the array have to be?
    psize = 3*csize; //make sure the array is big enought for rgb?


/*  
    color[] r = new color[csize];
    color[] g = new color[csize];
    color[] b = new color[csize];
*/

    int[] pallet = new int[psize];
    
    noLoop();
}

//----------------------------------------------------------------

void draw(){
  img.loadPixels();

  for (int i=0; i < img.width*img.height; i+=20)
    {
      //img.pixels[i] = color(0,0,0);  
      here = img.pixels[i];
                println("red   " + red(here));
                println("green    " + int(green(here)));
                println("blue        " + int(blue(here)));

//this doesn't work.... NullPointerException
      
      //pallet[i]   = int(red(here));
      //pallet[i+1] = int(green(here));
      //pallet[i+2] = int(blue(here));
                
    }//for


  img.updatePixels();
//----------------------------------------------------------------


  image(img, 0, 0);
//----------------------------------------------------------------
//draw


Re: problem with pulling color?
Reply #2 - Jan 28th, 2009, 2:43pm
 
Hi
as you declare a new Array in your setup function, the setup pallet is not the same as the class one.

Remove the "int[]" so the line looks like :

pallet = new int[psize];

and voila !
Re: problem with pulling color?
Reply #3 - Jan 28th, 2009, 6:32pm
 
perfect, thanks for your help...

would bit-shifting be a way faster method of doing the same thing?

thanks
Re: problem with pulling color?
Reply #4 - Jan 29th, 2009, 11:42am
 
for me, storing red, green, blue values seperatly is not a good idea.
If you have a white image you'll store same values in your array... 255,255,255 and if you use those colors later, you'll have en RGB image, not a white one.

Re: problem with pulling color?
Reply #5 - Jan 30th, 2009, 5:04am
 
what would be a better way of storing the pixel color? the whole RGBA - would i run in to a problem later if i would use a different format image?

i'm working through this idea to learn processing- so any tips or direction is very welcome.

jon

Page Index Toggle Pages: 1