Maroun Sassine Maroun Sassine - 4 years ago 200
Python Question

create layers array and perform median python

I've been struggling to create an array of layers, and each layer contains an image. Then perform median on this layer array to get one image.

Every added layer (from

) is 2D with shape
(460, 640)
containing floats.

In matlab you could easily do:

r_n = cell(1, num_filters);

for i = 1:num_filters
layers = o{i,1};
for j = 2:num_faces
layers = cat(3, layers, o{i,j});
r_n{i} = median(layers, 3);

The thing that I'm new to python, and maybe I'm still thinking in a Matlabish way

I tried:

for i in range(0,num_filters):
layers = o[i+k,0]
for j in range(1,num_faces):
layers = np.array([layers,o[i,j]]); ### HERE IS MY PROBLEM
print layers.shape;
r_n[i] = np.median(layers, axis = 0);
k = k + 65;

my layers array is wrong... what is a proper way to do it ?

Answer Source

You could stack them with np.stack (creating a new axis) and then apply the median:

# just some random arrays
layers = [np.random.random((10, 10)), 
          np.random.random((10, 10)), 
          np.random.random((10, 10))]  
np.median(np.stack(layers, axis=0), axis=0)

Or with a for-loop:

layers = [o[i+k,0]]
for j in range(1,num_faces):
np.median(np.stack(layers, axis=0), axis=0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download