I am trying to read values from a stack of images and store them in a structure that will allow for easy readability. My current idea is to create a loop
for x in width:
for y in length:
for z in height:
and try appending the z value to a list at [x, y]. The issue with this technique is that I do not know how to prevent losing the x and y position through the iteration and it seems rather inefficient.
The input data will consist of z values extracted from tiff images. I am reading them by first creating a stack through a loop statement
for i, fname in enumerate(image_files):
img = cv2.imread(fname, cv2.CV_LOAD_IMAGE_UNCHANGED)
img_stack[:,:,i] = img
which I found here: https://stackoverflow.com/a/20056062/1342580 (CV2 is only returning nan for me but that's a different question)
After the stack is created I used to loop at the beginning of my question to go through all of the pixels (which at the moment is 3858 x 3742) and append the z value of an x,y location from each image to the x,y value (for example if I had 4 images in the stack the result at position 1, 1 would be [1, 1, Z1, Z2, Z3, Z4]. Then I append that to another list that will contain a list for every x,y position
Is there a better technique for doing this?
Best Answer
I would probably store the values in nested dictionaries, with the keys corresponding to x and y coordinates. The structure would be something like the following for a set of 3x3 images (where all of the
x
,y
andz
variables would be replaced with the actual x- and y-coordinates, and corresponding z-values, respectively):This way, a list of all z-values for coordinates x=3, y=2 (for example), would be returned by
z_stack[3][2]
.To construct this dictionary, you can use a modified your existing loop like this: