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 › Reading color from image and creating a map
Page Index Toggle Pages: 1
Reading color from image and creating a map (Read 807 times)
Reading color from image and creating a map
Nov 14th, 2009, 8:04am
 
Hi all,
i'm new to processing and have little knowleadge of java programming... I need to know how to read colours from a given image... for every pixel i have to make a point rising in z axis when i found a green or yellow pixel... and display according to the bitmap to create a 3d space...
how can i make this??? thanks..
marco
Re: Reading color from image and creating a map
Reply #1 - Nov 14th, 2009, 8:15am
 
Look at pixels[] and friends (loadPixels, etc.).
Re: Reading color from image and creating a map
Reply #2 - Nov 14th, 2009, 2:06pm
 
i always recommend to use get(x,y) as i find it more intuitive in most cases.
@PhiLho, is there any difference in performance?
Re: Reading color from image and creating a map
Reply #3 - Nov 14th, 2009, 10:12pm
 
pixels[] seemed appropriate for a linear scan of the list of pixels... Smiley

Quoting the manual: "Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[]." and the JavaDoc: "This function is included primarily for beginners. It is quite slow because it has to check to see if the x, y that was provided is inside the bounds, and then has to check to see what image type it is. If you want things to be more efficient, access the pixels[] array directly."

Indeed, it adds 4 tests and a switch to each get. That's not much, and might not do a noticeable difference for a one time usage, but might do a difference for an algorithm reading each pixel several times (some filter) on each frame.
Re: Reading color from image and creating a map
Reply #4 - Nov 15th, 2009, 1:27am
 
true true there is no doubt that in this case pixels even makes much more sense as he wants to scan every pixel. i just didnt read carefully enough what he wants to do Smiley

but i didnt know that it was actually slower. was just curious. but now as i know at least for my programs i will use pixels.
Re: Reading color from image and creating a map
Reply #5 - Nov 15th, 2009, 7:28am
 
Hi all,
This is what i'm trying to do, but i can't achieve the result i need... any help?? thanks in advance..




Code:

int widthImg = 600;
int heightImg = 635;

size(1200,600, P3D);
//background(240);

//float fov = mouseX/float(width) * PI/2;
// float cameraX = width;
//float cameraZ = (height/2.0) / tan(fov / 2.0);

//perspective();
//perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
//ortho(-width/2, width/2, -height/2, height/2, -10, 10);

float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(PI * fov / 360.0);
perspective(fov, float(width)/float(height),
cameraZ/2.0, cameraZ*2.0);




// X, Z, Y --> eye, center , etc...
//camera(0, 600, 0, 600, 0, -600, 0, 1, 0);

//rect(0,0, 100, 100);


//ortho(-width/2, width/2, -height/2, height/2, -10, 10);

//strokeWeight(1);

// griglia set-up
//line(x1,y1,z1, x2,y2,z2);
// inverted X as X / Y as -Z / Z as Y

line(0, 0+600, 0, 0, 600, -9000); // y axis
line(100, 0+600, 0, 0, 600, -9000); // y axis
line(200, 0+600, 0, 0, 600, -9000); // y axis
line(300, 0+600, 0, 0, 600, -9000); // y axis
line(400, 0+600, 0, 0, 600, -9000); // y axis
line(500, 0+600, 0, 0, 600, -9000); // y axis
line(600, 0+600, 0, 0, 600, -9000); // y axis
line(700, 0+600, 0, 0, 600, -9000); // y axis
line(800, 0+600, 0, 0, 600, -9000); // y axis
// line (X, Z, Y);
line(0, 0+600, -200, 800, 0+600, -200); // x axis
line(0, 0+600, -400, 800, 0+600, -400); // x axis
line(0, 0+600, -600, 800, 0+600, -600); // x axis
line(0, 0+600, -800, 800, 0+600, -800); // x axis


PImage img = loadImage("mestreorto.jpg");

color fillColor = color(0,255,0);
color black = color(0,0,0);

colorMode(HSB, 360, 100, 100);

img.loadPixels();
for (int i = 0; i < (widthImg*heightImg); i++){
color c = img.pixels[i];

if (hue(c) >= 35 && hue(c) <= 150 && saturation(c) > 25 && brightness(c) > 15 && brightness(c) < 95){
img.pixels[i] = fillColor;
int riga = 0;
if(i > 600){
riga = i/600;
}else{
riga = 0;
}
int colonna = 0;
if(i > 100){
colonna = i-((i/600)*600);
}else{
colonna = i;
}

//real coordinates
//translate(colonna, riga, hue(c));

float x = colonna;
float y = riga;
float z = hue(c)/2;

// point (x,z,y);
//stroke(fillColor);
//point (x, (z*-1.0)+600, (-1.0*y));

println("colonna -> " + colonna + "// hue->" + hue(c) + "// riga->" + riga );

}else{
img.pixels[i] = black;
}

}

/*
PImage a = loadImage("mestreorto.jpg");
beginShape();
texture(a);
// X, Z, Y
vertex(0, 600, 0, 0);
vertex(600, 600, 0, 0);
vertex(0, 600, 635, 635);
vertex(600, 600, 635, 635);
endShape();
*/

//img.updatePixels();
// image(img, 0, 0);



Page Index Toggle Pages: 1