0

I am trying to get averaged pixel intensity using numpy in Python. I just started Python so I have some difficulties. My python script is

import numpy as np
import glob, os
from scipy import misc

path = "C:\\train"
files = []
for name in os.listdir(path):
    if os.path.isfile(os.path.join(path, name)):
       files.append(os.path.join(path, name))
divider = 1
averaged = []
for file in files:
   image = misc.imread(file)
   image = image.astype(np.float32, copy=False)
   if(divider == 1):
       averaged = image
       divider+=1
   else:
       for i in range(703):
          for j in range(1247):
             averaged[i][j] = averaged[i][j] + image[i][j]
             averaged[i][j] /= 2

averaged_mean = np.transpose(averaged,[2,0,1])
averaged_mean = averaged_mean.astype(np.float32, copy=False)
np.save("image_mean.npy", averaged_mean,fmt='%f')

I have two issues I need to make them improved. (1)My matrix dimensions are 704 x 1248 x 3. So taking average as I calculate shown above takes long time. I have 2000 images. How can I change the way to make it faster?

(2)When I save, I got the header in binary file as “NUMPY V {'descr': '<f4', 'fortran_order': False, 'shape': (3L, 704L, 1248L), }. I want to save as “NUMPY F {'descr': '<f8', 'fortran_order': False, 'shape': (3, 704, 1248), } How can I change it? Thanks

1 Answer 1

3

You can replace

for i in range(703):
      for j in range(1247):
         averaged[i][j] = averaged[i][j] + image[i][j]
         averaged[i][j] /= 2

With averaged = (averaged + image) / 2 if averaged is defined to be a numpy array in the first place of the same shape as image. In terms of your code,

averaged = reduce(lambda l, r: (l + r)/2, 
    (misc.imread(f).astype(np.float32, copy = False) for f in files))

Note that this will put more weight on later images. To weight everything evenly, you can use

np.mean([misc.imread(f).astype(np.float32, copy = False) for f in files], axis = 0)

EDIT: This implements the second approach without having to load all images as once.

averaged2 = reduce(lambda l, r: l + r, 
    (misc.imread(f).astype(np.float32, copy = False) for f in files))\
    / len(files)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. For saving the float32 array to npy binary file, why I can't save as float type? I save as np.save("file.npy", averaged_mean)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.