def fix_badpix_vip(img, bpm, box=5):
'''
Corrects the bad pixels, marked in the bad pixel mask.
The bad pixels are replaced by the median of the adjacent pixels
in a box of the povided box size. This function is very fast but
works best with isolated (sparse) pixels or very small clusters.
Copied and adapted from the the Vortex Image Processing package,
https://github.com/vortex-exoplanet/VIP, in which the function is
called fix_badpix_isolated.
This version is improved with respect to the VIP one by replacing
the bad pixels with NaNs in the image before applying the
median_filter. This allows to make sure that adjacent bad pixels
will not be taken into account when calculating the median.
Parameters
----------
img : array_like
Input 2D image
bpm : array_like, optional
Input bad pixel map. Good pixels have a value of 0, bad pixels
a value of 1.
box : odd int, optional
The size the box (box x box) of adjacent pixels for the
median filter. Default value is 5
Return
------
img_clean : array_like
Cleaned image
'''
if not img.ndim == 2:
raise ValueError('Main input is not a 2D array')
if not bpm.ndim == 2:
raise ValueError('Bad pixel map input is not a 2D array')
if box % 2 == 0:
raise ValueError('Box size of the median blur kernel must be an odd integer')
bpm = bpm.astype('bool')
bp = np.where(bpm)
img_clean = img.copy()
img_clean[bp] = np.nan
smoothed = ndimage.median_filter(img_clean, box, mode='mirror')
img_clean[bp] = smoothed[bp]
# replace uncorrected bad pixels with original value
mask = ~np.isfinite(img_clean)
img_clean[mask] = img[mask]
return img_clean
评论列表
文章目录