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 & HelpPrograms › 1000 image array
Page Index Toggle Pages: 1
1000 image array (Read 2449 times)
1000 image array
May 22nd, 2010, 1:58pm
 
I want to create a large (1000+) array of 640x480 images.

It appears memory becomes an issue if I try and pre-load the entire array in setup.

Does anyone have any advice on how to do this?  

Or can anyone explain why this cause a problem? With a smaller array (80 images) i calculated processing used around 4.5MB of memory per image.  How come it uses so much?  Or is this not a lot?

Not all the images will be displayed at the same time -I tried setting an image reference to 'null' once it has been shown to free up memory but this didn't seem to work.

Thanks,
TB
Re: 1000 image array
Reply #1 - May 22nd, 2010, 2:54pm
 
Hi terry,

Uncompressed, all images should be the same size: 640 x 480 x 3 = 900 KB

When loading images, if you overwrite the reference, you should be fine. If you store all image references, you will probably run into problems.

Code:
  for (int i = 0; i < numberofimages; i++) {
   
   PImage img = loadImage("image_"+i+".png");
   img.loadPixels();

   // analyzing pixels & counting colors
   for (int pix = 0; pix < img.pixels.length; pix++) {

int r = int(red(img.pixels[pix]));
int g = int(green(img.pixels[pix]));
int b = int(blue(img.pixels[pix]));

rgbcount[r][g][b]++;
   }

 }
Re: 1000 image array
Reply #2 - May 22nd, 2010, 3:11pm
 
Let's do a little computation. An image in Processing is in RGB or RGBA (with alpha channel). Supposing that's RGB, that's 3 bytes per pixel.
So, a 640x480 image uses around 1MB in memory.
Something more or less confirmed by analysis of the running process with Windows' process manager, taking in account the base memory used by a simple Java program.
A bit far from your 4.5MB.

That said, if you load 1000 images, it would take nearly a gigabyte, which is a bit too much, and would take quite some time!

Not sure of what you want to do exactly, but perhaps you can anticipate the loading and using requestImage(), keeping a few images in memory (to be displayed) and discarding them as soon as they are used.
Re: 1000 image array
Reply #3 - May 22nd, 2010, 4:39pm
 
Thanks guys.  My calculations must be off.  I was using activity monitor on a mac.....was giving me numbers much larger than 1MB per image.

My query is related to this post:http://processing.org/discourse/yabb2/num_1274495886.html

Trying to come up with efficient frame by frame display.  Image sequences seemed a good way to go about it.

I resized my frames down to 320x240, then scaled them up inside processing.  This allowed me 30 seconds of smooth playback (approx 900 frames).

I am confused about how to free up memory, so I can go longer  Any suggestions

Thanks,
TB

Code:

int sequenceLength = 981;//manually set sequence length
int frame = 0; //frame counter
PImage [] images = new PImage[sequenceLength];

void setup(){
size(640,480);
frameRate(30);

for(int i=0; i < images.length; i++){
  String currentImage = nf(i, 4) + ".jpg";
  images[i] = loadImage(currentImage);
}
}


void draw(){
 frame = (frame+1) % images.length;
 image(images[frame], 0, 0, width, height);
 if(frame == images.length){
  frame = 0;
 }
}
}
Re: 1000 image array
Reply #4 - May 23rd, 2010, 1:32am
 
Well, I answered: don't load all the images at once, but on the fly, using a limited array, eg. 8 to 32 images, so you can discard the old, displayed ones, and load the ones to come. Not obvious if you need to keep a fixed frame rate.

In the state of your program, freeing memory after displaying the images isn't useful, as you have already loaded them all at once, so the memory is already full.
Re: 1000 image array
Reply #5 - May 23rd, 2010, 5:31am
 
Code:

void draw(){
frame = (frame+1) % images.length;
image(images[frame], 0, 0, width, height);
if(frame == images.length){
frame = 0;
}
}


why are you using modulo to wrap the value of frame AND resetting the value yourself? they do the same thing. you can delete the bottom condition.
Re: 1000 image array
Reply #6 - May 23rd, 2010, 10:44am
 
PhiLho: Loading images on the fly compromises the frame rate which, for reasons of accuracy, i want to keep at 30.  I'm not sure if you were acknowledging this, or saying I hadn't specified it was an issue when you said: "not obvious if you need to keep a fixed frame rate."

koogy: that was my mistake - left over from prior to using modulo

Thanks for your help guys,  I'll keep looking.
Re: 1000 image array
Reply #7 - May 23rd, 2010, 1:46pm
 
Well, I actually anticipated your objection... Smiley
Ie. I wrote it isn't obvious to do a proper on the fly loading AND keep a steady/regular frame rate.
A possible way is to use indexed images (3 to 4 times smaller), a bit like Gif images (limited to 256 colors) but Processing can't handle these naturally, so you might need to go the Java way (not even sure if it can manipulate them properly).
Or, load the jpg images in file form, ie. strongly compressed, and uncompress them on the fly, when you need to display them.
Again, it would need some Java knowledge.
Re: 1000 image array
Reply #8 - May 23rd, 2010, 3:24pm
 
thanks for your help PhiLho
Page Index Toggle Pages: 1