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 › store rgb in a array.
Pages: 1 2 
store rgb in a array. (Read 1682 times)
store rgb in a array.
Mar 26th, 2009, 11:40am
 
Hi,

I want to store all the rgb values in a array by there x and y axis.
Only I don't know how to set the value for the length of the array, now I use this and it doesnt give a error, I this a good way or are there better ways?

Code:

//declare, create
float[][] r = new float[10000][];
float[][] g = new float[10000][];
float[][] b = new float[10000][];
Re: store rgb in a array.
Reply #1 - Mar 26th, 2009, 12:01pm
 
I don't understand your need. What RGB values? Where do they come?
Are you aware, if that's the sketch's colors, that you have them already if you use loadPixels()?
Re: store rgb in a array.
Reply #2 - Mar 27th, 2009, 3:48am
 
loadPixels won't work I guess cause I want to calculate average values and do something else. It's not for copying pixels, I want to use images as somekind of ponscard (don't now if it's the correct word).
...

so far I got this but it says 'cannot convert from float to int' on this line:
     r[i][j] = get(i, j);    

how do I fix that?

Code:
PImage img;

float[][] r = new float[5000][];
float[][] g = new float[5000][];
float[][] b = new float[5000][];

void setup(){
size(50, 100);
img = loadImage("plaatje.png");
noLoop();
}

void draw() {
image(img, -20, -20);
for (float i = 0.0; i<width; i++){
for (float j = 0.0; j < height; j++){
r[i][j] = get(i, j);
//println(i + " " + j);
}
}
}
Re: store rgb in a array.
Reply #3 - Mar 27th, 2009, 4:33am
 
i have to admit that i didnt unterstand what you are trying to do...
but when you need exact pixels, why do you use float and not int ?

Re: store rgb in a array.
Reply #4 - Mar 27th, 2009, 4:45am
 
Cedric is right, floats are overkill and inappropriate here.
ponscard: I think that's punched cards in English (cartes perforées en français).

If you need to access image colors, loadPixels is the fastest way and you can (quickly) get R, G, B components with a some bit operations.
Re: store rgb in a array.
Reply #5 - Mar 27th, 2009, 6:17am
 
ok, I will later look at loadPixels.

for now I have a question about:
     imageColors[y + x] = img.get(x, y);

I want [y + x] to be like 120 + 60 = 12060 (or 120060).
Now it will be just 180 which I understand but how can I add the x behind the y. I tried [y + "" + x] for example but that doesnt work out.


also
 println(imageColors);

prints this for example, what is the 0 and why doesnt it show the value?
if I  do   println(imageColors[33]); then it will perfectly show the value.

[11011] 0
[11012] 0
[11013] 0
[11014] 0

Code:

PImage img;

color[] imageColors;

void setup(){
size(50, 100);
img = loadImage("plaatje.png");
noLoop();
imageColors = new color[img.width*img.height];
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++){
imageColors[y + x] = img.get(x, y);
}
}
println(imageColors);
}
Re: store rgb in a array.
Reply #6 - Mar 27th, 2009, 6:36am
 
konvert it to String add it up and konvert back to int

int x = 120;
int y = 60;

int XandY = int(Integer.toString(x)+Integer.toString(y));

println(XandY);
Re: store rgb in a array.
Reply #7 - Mar 27th, 2009, 6:49am
 
imageColors[y * 1000 + x] = img.get(x, y);

Now, I thought you made bi-dimentional arrays to avoid such computation... (and waste of memory).

As for the println, you don't tell us what is imageColor... I suppose it is a big one-dimensional array.
By default, it is initialized to 0. When you print it, it shows thousands of lines, most of them with 0. If you scroll to [33] you should see the relevant value.
Re: store rgb in a array.
Reply #8 - Mar 27th, 2009, 8:13am
 
* 1000 is so simple but very smart Smiley
althought it doesnt work cause I get the error
ArrayIndexOutOfBounds

Can someone make a example of how to use bi-dimentional array in the code below.

Code:

void setup(){
size(50, 100);
img = loadImage("plaatje.png");
noLoop();
imageColors = new color[img.width*img.height];
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++){
imageColors[y+x] = img.get(x, y);
}
}
println(imageColors);
}
Re: store rgb in a array.
Reply #9 - Mar 27th, 2009, 8:46am
 
imageColors[y * img.width + x] = img.get(x, y);

You have to add the width for each past line.
I don't want to look insisting, but your loop is exactly recreating the pixels[] array...
Re: store rgb in a array.
Reply #10 - Mar 27th, 2009, 8:59am
 
Smiley
Re: store rgb in a array.
Reply #11 - Mar 27th, 2009, 9:36am
 
I don't want to look insisting,

If I run it without the for loop then all values are printed 0
If I run with the for loop then the output is correct:

[11013] -3309016
[11014] -3572181
[11015] -3637975
[11016] -3638234
[11017] -3637968
[11018] -3638479
[11019] -3639510
[11020] -3705300

only the array names are still not how I want them.
It end with 11020, which is the total ammount of pixels.
The image I use now is 107x103 and I want to be able to see the positions back. What is smartest to do, a bi-dimentional array like this:

[arraynumber] { {xpos, ypos, colorvalue} }

The last pixel at right bottem wil be for example:
imageColors[11020]   107, 103, -3705300

try with and without for loop if you like
Code:
PImage img;

color[] imageColors;

void setup(){
size(50, 100);
img = loadImage("plaatje.png");
noLoop();
imageColors = new color[img.width*img.height];
/*for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++){
imageColors[y * img.width + x] = img.get(x, y);
}
}*/
println(imageColors);
}
Re: store rgb in a array.
Reply #12 - Mar 27th, 2009, 9:45am
 
No, PhiLho, you are absolutely correct. The _right_ way to manipulate the pixels in an image (even if you are not going to display it anywhere) is to draw that image onto a PGraphics or PImage object (or load it there, whatever), - they share alot of methods. Then manipulate the pixels using loadPixels(). pixels[] is a flat array, so if you want to access it as a two-dimensional array, you do something like this:
Code:

loadPixels();
int a,r,g,b,p;
for ( int y = 0; y < height; y++ ) {
 for (int x = 0; x < width; x++ ) {
   p = pixels[x + y*width];
   a = (p >> 24) & 0xFF;
   r = (p >> 16) & 0xFF;
   g = (p >>  8) & 0xFF;
   b = (p      ) & 0xFF;
   // Do whatever with a (for alpha) and rgb here
   // then get it back like this:
   pixels[x+y*width] = (a << 24) | (r << 16) | (g << 8) | (b);
 }
}
updatePixels();


Maybe it can be done even faster, but I don't really know how.
Re: store rgb in a array.
Reply #13 - Mar 27th, 2009, 11:29am
 
Ossory thx for input but it's a bit to complex for me atm, I'm kinda new to processing.

now I have this:
Code:

color[] imageColors;


can I make it color[][] and store x and y position aswell or does it have to be a normal array?
Re: store rgb in a array.
Reply #14 - Mar 27th, 2009, 12:17pm
 
OK, let say it is for learning purpose. Which is a very valid reason, after all.
Here is what you want:
Code:
PImage img;

color[][] imageColors;

void setup(){
size(50, 100);
img = loadImage("D:/_PhiLhoSoft/Processing/earth.png");
noLoop();
imageColors = new color[img.width][img.height];
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++) {
imageColors[x][y] = img.get(x, y);
}
}
println(imageColors);
DumpColors();
}

void DumpColors()
{
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++) {
println("x=" + x + " y=" + y + " : " + hex(imageColors[x][y]));
}
}
}

As you can see, Processing can't println a multi-dimensional array, you have to do it yourself.
Pages: 1 2