c++ - Fast data structure or algorithm to find mean of each pixel in a stack of images -


i have stack of images in want calculate mean of each pixel down stack.

for example, let (x_n,y_n) (x,y) pixel in nth image. thus, mean of pixel (x,y) 3 images in image stack is:

mean-of-(x,y) = (1/3) * ((x_1,y_1) + (x_2,y_2) + (x_3,y_3)) 

my first thought load pixel intensities each image data structure single linear buffer so:

|all pixels image 1| pixels image 2| pixels image 3| 

to find sum of pixel down image stack, perform series of nested loops so:

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 buffer element of first pixel in nth image , col*img_rows+row gives (x,y) pixel want find each n image in stack.

is there data structure or algorithm me sum pixel intensities down image stack faster , more organized current implementation?

i aiming portability not using opencv , using c++ on linux.

the problem nested loop in question it's not cache friendly. go skipping through memory long stride, rendering data cache useless. you're going spend lot of time accessing memory.

if can spare memory, can create image-sized buffer accumulate totals each pixel walk through pixels in images in memory order. single pass through buffer division.

your accumulation buffer may need use larger type use individual pixel values, since has accumulate many of them. if pixel values are, say, 8-bit integers, accumulation buffer might need 32-bit integers or floats.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -