I have a stack of images in which I want to calculate the average value of each pixel on the stack.
For example, let (x_n,y_n) be the pixel (x,y) in the nth image. Thus, the average pixel value (x,y) for three images in the image stack:
mean-of-(x,y) = (1/3) * ((x_1,y_1) + (x_2,y_2) + (x_3,y_3))
My first thought was to load all the pixel intensities from each image into a data structure using one linear buffer as follows:
|All pixels from image 1| All pixels from image 2| All pixels from image 3|
To find the sum of a pixel in the image stack, I do a series of nested loops like:
for(int col=0; col<img_cols; col++) { for(int row=0; row<img_rows; row++) { for(int img=0; img<num_of_images; img++) { sum_of_px += px_buffer[(img*img_rows*img_cols)+col*img_rows+row]; } } }
Basically img*img_rows*img_cols gives the buffer element of the first pixel in the nth image, and col*img_rows+row gives the pixel (x,y) that I want to find for each n-image on the stack.
Is there a data structure or algorithm that will help me sum the pixel intensities down the image stack, which is faster and more organized than my current implementation?
I am focused on portability, so I will not use OpenCV and use C ++ for Linux.