A better way?

I am trying to average 40 images pixel by pixel:

  
void testApp::setup(){	   
	ofBackground(255,255,255);	  
  
	processedPixels = new unsigned char[imageIn.width * imageIn.height * 4];  
	  
	int imageup = 141;  
	int pixelAverager = 0;	  
  
	int num = imageIn.width * imageIn.height * 3;  
	  
	for(int i = 0; i < num; i++){  
	  for(int a = 100; a < imageup; a++){  
  
	  imageIn.loadImage("signature"+ofToString(a)+".png");  
  
	  //this points to the the pixels of the loaded image  
	  //ptr can be treated as an array that is the size of the number of bytes in the image.  
	  unsigned char * ptr = imageIn.getPixels();  
	  
	  pixelAverager = pixelAverager + ptr[i];  
  
	}  
        //average the pixels or it wouldn't be a pixelAverager  
        pixelAverager = pixelAverager / 40;  
        //first we set our output pixel to black to make sure it is clean to start off with  
	processedPixels[i] = 0;  
	//gives the channel's pixel value into output array  
	processedPixels[i] = pixelAverager;  
        pixelAverager = 0;  
	printf ("Characters: %c \n", 'a');  
}  
  
	imageOut.setFromPixels(processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);   
  
   imageOut.saveImage("averageImage.png");	  
}  

Okay… so first off, to go through 1,920,000 sets of 40 pixels seems like it may take about two months by the method I am doing it (judging by the rate my “Characters: a” message prints to the console). Is there a better way for doing this that won’t require a designated computer?

Secondly, How do I get it to print out my [i] value to the console? The “Characters a” code I stole off some website isn’t going to cut it in the long run.

-Randy :smiley:

hi randy,

yeah a better way would be to reverse these two for loops:

  
  
   for(int i = 0; i < num; i++){  
     for(int a = 100; a < imageup; a++){  
     imageIn.loadImage("signature"+ofToString(a)+".png");  
  

this loads in each one of your images, per pixel !! that’s *alot* more loading then you need to do .

this:

  
  
 for(int a = 100; a < imageup; a++){  
 imageIn.loadImage("signature"+ofToString(a)+".png");  
 unsigned char pix = imageIn.getPixels();  
   for(int i = 0; i < num; i++){  
  

loads the images, and for every image, does all of the pixels… rediculously less work and I bet it will be multitudes faster.

hope that helps
zach

Thanks. That makes a lot of sense. I clearly was not thinking straight. Loading the images 40 times is a lot better than loading them all 1,920,000. It’s been a long day.