I am new in python programming, so forgive me if my questions are too basic. I've been helped a lot by this forum before and thanks to you guys for all your contributions.
This time I have a set of 12,000 image data which I am performing singular value decomposition (svd) on and calculating their mean. Some of the images have pixels with very high positive or negative values which I don't want to use during computation, so I used
numpy.ma.masked_array
to exclude them from both svd and mean computation. And some images are smaller than others and they were padded with zeros values to make all images to have the same (pixel) dimension. But I also don't want the 'zero paddings' to be used during computation, so I used numpy.ma.masked_array
to exclude them from both svd and mean calculation.
Here are some example images:
The problem is that when I perform both svd and mean calculation, the masked values (array elements) are not excluded during computation. I have tried all that I know to resolve this without success. Below are the steps that I took.
from numpy.linalg import svd
import numpy as np
from numpy.ma import masked_array
n, x, y = images.data.shape
Z = []
meanimage = []
for icount in range(n):
image = images[icount,:,:] # current image
# creating a mask for too positively or negatively high values
mask = (np.abs(image) > 2).astype(int);
yindex = 0; xindex = 0;
# --- creating a mask for zero padded values
for i in range(y/2): # get the index of the first none zero pixel
if image[i,x/2] != 0:
yindex = i
break
for i in range(x/2): # get the index of the first none zero pixel
if image[y/2,i] != 0:
xindex = i
break
mask[:yindex,:] = 1;mask[-yindex:,:] = 1;
mask[:xindex,:] = 1;mask[-xindex:,:] = 1;
# ---
image = masked_array(images[icount,:,:], mask)
Z.append(image.ravel()) # accummulating matrix for svd computation
meanimage.append(image) # accummulating matrix for for mean computation
# calc. SVD
u,s,v = svd(masked_array(Z))
#calc. mean image
meanimage = masked_array(meanimage).mean(axis=0)
bimage = np.dot(np.dot(u[:,:2],np.diag(s[:2])),np.transpose(v)[:2,:])
eigenimage = bimage[2,:].reshape(x, y)
The final results - eigenimage
and meanimage
- that I get does not exclude the masked values from computation. I don't know what I did wrong. Please, I need some ideas that will help me to resolve this.
Above are some samples of the images (beams) data that I am working with.
The final images that I get after computation for the eigenimage and meanimage are :
Eigen (beam) image (with SVD)
Mean (beam) image (masked_array mean)
From the above figures, both the eigenimage and meanimage loses a lot of side lobes information which are not desired.
But I was expecting the final eigen images to be like