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 › fixing this code...
Page Index Toggle Pages: 1
fixing this code... (Read 607 times)
fixing this code...
Oct 28th, 2007, 5:08am
 
hi there
I have a code in which an image is read in terms of black or white. i then have a grid of crosses in which i would like the cross to be drawn if the initial pixel is white otherwise leave that space blank.
i have a code where i think this should be happening, but for some reason only a few crosses (when i believe there should be more) are being drawn.

can anyone help me please.
thanks

the code i have is;PImage snow;
void setup (){
background (255);
size (1200,1200);
snow =loadImage("snow.JPG");
image(snow, 0,0);
filter(GRAY);
filter(POSTERIZE, 2);
stroke (247,50,50);
fill (255);
}  
void draw ()
{  
   snow.loadPixels ();  
   for (int x = 15; x< snow.width; x+=30)  
   {  
  for (int y=15; y< snow.height; y+=30)  
  {  
 int i = (y * snow.width + x);  
 if (snow.pixels [i] == color(255) ) {  
 drawCross (x,y);
 }  
 else if (snow.pixels [i] == color(0) ) {  
 }  
  }  
   }  
}
void drawCross (int x, int y){
 line (x-5,y,x+5,y);
 line (x,y-5,x,y+5);


}

Re: fixing this code...
Reply #1 - Oct 28th, 2007, 5:44am
 
ok.
i now realise why it isnt drawing as many crosses as i expected.
it is because it is reading the initial image rather than the image after i have gray filtered it and made it two colours.

does anyone know how i would get it to read the image after i have done this.
thanks
Re: fixing this code...
Reply #2 - Oct 28th, 2007, 6:54am
 
Hi, I hope this helps.
PS: There are more than one way of doing this.
The example below is just one of them
-----------------------------------------------------

PImage picture;
int value;

void setup() {
 picture = loadImage("snow.jpg");  //loads the image
 size (picture.width,picture.height); //sets the canvas size to the image size
 background(255);

 image(picture, 0,0); //loads the picture
 filter(GRAY); //greyscale
 filter(POSTERIZE, 2); //posterize  
 
 stroke(247,50,50); //sets stroke

 //loop trhough all the pixels on the screen
 for (int x=0; x < width; x++) {
   for (int y=0; y < height; y++) {
     //Since you only want black (rgb = 0,0,0) or white (rgb, 255, 255, 255), we only need to get one value of either the reds the greens or the blues.
    //This next line extracts the red value from the byte format that the function get returns
     value = get(x, y) >> 16 & 0xFF;
     if (r == 255) {
       //if it's white, then draw the cross
       drawCross (x, y);
     }
   }
 }
}

void draw() {
}

void drawCross (int x, int y){
 line (x-5,y,x+5,y);
 line (x,y-5,x,y+5);
}

Re: fixing this code...
Reply #3 - Oct 28th, 2007, 7:02am
 
if i paste this in it says there is no accessible field name 'r'
do you know what this means?

it looks promising though... thanks
Re: fixing this code...
Reply #4 - Oct 28th, 2007, 8:55am
 
Code:

...
value = get(x, y) >> 16 & 0xFF;
if (r == 255) {
...


should be

Code:

...
int r = red( get(x, y) ); // read the amount of red from that pixel
if ( r == 255 ) {
...
Re: fixing this code...
Reply #5 - Oct 28th, 2007, 9:15am
 
i am going crazy!

now it is telling me (whilst highlighting the red expression) that the type or right sub-expression is not assignable to the variable of type 'int'

i dont have float written anywhere, not am i asking for anything other than integers!!!!
Re: fixing this code...
Reply #6 - Oct 28th, 2007, 9:42am
 
right, that's just what it says:
you were asking for int (real numbers) but red gives float. you need to write ( .. i should have written):
float r = red( ...
Re: fixing this code...
Reply #7 - Oct 28th, 2007, 9:49am
 
wow
it actually worked
thankyou so much for your help!!!
Re: fixing this code...
Reply #8 - Oct 28th, 2007, 3:26pm
 
Sorry about that.
It should be

if (value == 255) {

and not

if (r == 255) {

You're welcome! Smiley
Page Index Toggle Pages: 1