Sunday, 8 September 2013

Finding median of masked ndarrays representing images

Finding median of masked ndarrays representing images

I have 5 grayscale images in the form of 288x288 ndarrays. The values in
each ndarray are just numpy.float32 numbers ranging from 0.0 to 255.0. For
each ndarray, I've created a numpy.ma.MaskedArray object as follows:
def bool_row(row):
return [value == 183. for value in row]
mask = [bool_row(row) for row in nd_array_1]
masked_array_1 = ma.masked_array(nd_array_1, mask=mask)
The value 183. represents "garbage" in the image. All 5 images have a bit
of "garbage" in them. I want to take the median of the masked images,
where taking the median for each point should ignore any masked values.
The result would be the correct image with no garbage.
When I try:
ma.median([masked_array_1, masked_array_2, masked_array_3, masked_array_4,
masked_array_5], axis=0)
I get what seems to be the median except instead of ignoring masked
values, it treats them as 183., so the result just has the superimposed
garbage from all the pictures. When I just take the median of two masked
images:
ma.median([masked_array_1, masked_array_2], axis=0)
It looks like it started to do the right thing, but then placed the value
of 183. even where both masked arrays contain a MaskedConstant.
I could do something like the following, but I feel there's probably a way
to make ma.median just behave as expected:
unmasked_array_12 = ma.median([masked_array_1, masked_array_2], axis=0)
mask = [bool_row(row) for row in unmasked_array_12]
masked_array_12 = ma.masked_array(unmasked_array_12, mask=mask)
unmasked_array_123 = ma.median([masked_array_12, masked_array_3], axis=0)
mask = [bool_row(row) for row in unmasked_array_123]
masked_array_123 = ma.masked_array(unmasked_array_123, mask=mask)
...
How do I make ma.median work as expected without resorting to the above
unpleasantness?

No comments:

Post a Comment