Median combining fits images in python
Asked Answered
S

1

10

I have three fits images in the form of 2D numpy arrays. I want to median combine them, that is, generate an output array in which each pixel is the median of the same pixel in the three input arrays. This can be done easily on IRAF using imcombine. Is there a way to do this on Python without looping through the entire array and taking the median of each pixel?

Strophanthin answered 6/12, 2012 at 21:50 Comment(0)
P
14

The easiest way to do this is:

  • Stack the 2d arrays to form a 3d array
  • Compute the median using numpy.median passing axis=0 to compute along the dimension of stacking.

You're essentially computing an element-wise median. Here's a simple example of what I would do:

>>> import numpy
>>> a = numpy.array([[1,2,3],[4,5,6]])
>>> b = numpy.array([[3,4,5],[6,7,8]])
>>> c = numpy.array([[9,10,11],[12,1,2]])
>>> d = numpy.array([a,b,c])
>>> d
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12,  1,  2]]])
>>> d.shape
(3, 2, 3)

>>> numpy.median(d, axis=0)
array([[ 3.,  4.,  5.],
       [ 6.,  5.,  6.]])
Palmar answered 6/12, 2012 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.