Python Forum
Masked median filters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Masked median filters
#1
As part of studying towards a MSc in Astronomy, I'm trying to filter an image so that the value of each pixel is equal to the value of the median of the pixels within a 50x50 square around it, excluding any masked pixels.
Here's my latest attempt:

import numpy as np
from astropy.io import fits
from skimage.util.shape import view_as_windows

#   Use the fits files as input image and mask
hdulist = fits.open('xbulge-w1.fits')
image = hdulist[0].data
hdulist3 = fits.open('xbulge-mask.fits')
mask = 1 - hdulist3[0].data
imagemasked = np.ma.masked_array(image, mask = mask)

side = 20
window_shape = (side, side)

Afiltered = view_as_windows(imagemasked, window_shape)

# collapse the last two dimensions in one
flatten_view = Afiltered.reshape(Afiltered.shape[0], Afiltered.shape[1], -1)
print(flatten_view.shape)

# resampling the image by taking median
median_view = np.ma.median(flatten_view, axis=2)
In the code here, I've used some methods from the documentation of skimage.util.view_as_windows to produce the filtered image.
It looks like it's ignoring the mask, but apart from that, I think it works fine. Any advice on processing the masked image?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculate median for school grades per subject siki 1 1,935 May-10-2021, 11:41 AM
Last Post: jefsummers
  Help on Creating Program to find the Median and Mode by hand EvanCahill 3 2,943 Jul-19-2018, 06:17 PM
Last Post: woooee
  Median of the age row - tsv file sonxy 3 3,022 Apr-09-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020