Source code for visualastro.analysis.image_utils

"""
Author: Elko Gerville-Reache, Qiushi Chris Tian
Date Created: 2025-09-22
Date Modified: 2026-11-17
Description:
    Utility functions for image manipulations.
"""

from __future__ import annotations

import glob
import warnings
from collections.abc import Sequence
from pathlib import Path
from typing import Literal

import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
from astropy.io import fits
from astropy.io.fits import HDUList, Header, ImageHDU, PrimaryHDU
from astropy.units import Quantity
from astropy.wcs import WCS
from numpy.typing import DTypeLike, NDArray
from scipy.ndimage import convolve

from visualastro.core.array_types import _is_iterable
from visualastro.core.configuration import (
    _UNSET,
    _resolve_default,
    _Unset,
    config,
)
from visualastro.core.data import get_data, get_value
from visualastro.core.io import _get_dtype, get_errors
from visualastro.core.units import get_unit
from visualastro.datamodels.datacube import DataCube
from visualastro.datamodels.fitsfile import FitsFile
from visualastro.optional_dependencies._regions import (
    _HAS_REGIONS,
    EllipseAnnulusPixelRegion,
    EllipsePixelRegion,
    PixCoord,
)
from visualastro.optional_dependencies._spectralcube import (
    _HAS_SPECTRAL_CUBE,
    SpectralCube,
)
from visualastro.optional_dependencies._tqdm import tqdm
from visualastro.optional_dependencies.register import (
    _offer_dependency,
    _require_dependency,
)
from visualastro.utils.wcs_utils import _reproject_wcs

__all__ = [
    'get_hdu',
    'get_hdu_from_hdu_list',
    'load_data_cube',
    'load_fits',
    'load_spectral_cube',
    'stack_cube',
    'mask_image',
    'compute_line',
    'detect_edges',
    'compute_sobel_filter',
    'image_2_grayscale',
    'get_filter_wavelength',
]


# I/O Functions
# -------------
[docs] def get_hdu( input_: str | Path | HDUList | ImageHDU | PrimaryHDU, hdu_idx: int | _Unset = _UNSET, concatenate_jwst_header: bool = True ) -> ImageHDU | PrimaryHDU: """ Get an HDU object from a path, `~astropy.io.fits.HDUList`, or an HDU object. Parameters ---------- input_ : str | Path | HDUList | ImageHDU | PrimaryHDU The input to get the HDU object from. hdu_idx : int | _Unset, optional, default=_UNSET The index of the HDU object to get. concatenate_jwst_header : bool, optional, default=True Whether to concatenate the header of the JWST primary HDU with the header of the image HDU if `concatenate_jwst_header` is True. Returns ------- hdu : `~astropy.io.fits.ImageHDU` | `~astropy.io.fits.PrimaryHDU` The HDU object. """ if isinstance(input_, (ImageHDU, PrimaryHDU)): return input_ if isinstance(input_, (str, Path)): input_ = fits.open(input_) if not isinstance(input_, HDUList): raise ValueError( 'Input must be a path, HDUList, ImageHDU, or PrimaryHDU. ' f'Got {type(input_).__name__!r}.' ) return get_hdu_from_hdu_list(input_, hdu_idx, concatenate_jwst_header)
[docs] def get_hdu_from_hdu_list( hdu_list: HDUList, hdu_idx: int | _Unset = _UNSET, concatenate_jwst_header: bool = True ) -> ImageHDU | PrimaryHDU: """ Get an HDU object from an `~astropy.io.fits.HDUList` object. JWST data products store the image in the second HDU, with the primary HDU containing metadata about the observation. This function will concatenate the header of the primary HDU with the header of the image HDU if `concatenate_jwst_header` is True. Parameters ---------- hdu_list : `~astropy.io.fits.HDUList` The HDU list to get the HDU object from. hdu_idx : int | _Unset, optional, default=_UNSET The index of the HDU object to get. concatenate_jwst_header : bool, optional, default=True Whether to concatenate the header of the JWST primary HDU with the header of the image HDU if `concatenate_jwst_header` is True. Returns ------- hdu : `~astropy.io.fits.ImageHDU` | `~astropy.io.fits.PrimaryHDU` The HDU object. """ hdu_0_header = hdu_list[0].header if ( concatenate_jwst_header and hdu_idx == _UNSET and hdu_0_header['TELESCOP'] == 'JWST' and hdu_0_header['NAXIS'] < 1 ): hdu = hdu_list[1] hdu_1_header = hdu.header.copy() hdu.header = hdu_0_header hdu.header.update(hdu_1_header) else: hdu_idx = _resolve_default(hdu_idx, config.hdu_idx) hdu = hdu_list[hdu_idx] return hdu
def load_data_cube( filepath: str, error: bool = True, hdu: int | _Unset = _UNSET, dtype: DTypeLike | str | _Unset = _UNSET, print_info: bool | _Unset =_UNSET, transpose: bool | _Unset = _UNSET, invert_wcs: bool | _Unset = _UNSET ) -> DataCube: """ Load a sequence of FITS files into a 3D data cube. This function searches for all FITS files matching a given path pattern, loads them into a NumPy array of shape (T, M, N), and bundles the data, headers, errors, and WCS into a `DataCube` object. Parameters ---------- filepath : str Path pattern to FITS files. Wildcards are supported. i.e. `'Spectro-Module/raw/HARPS*.fits'` error : bool, optional, default=True If `True`, try to extract the error extension. Converts variance to errors using `get_errors`. hdu : int | _Unset, optional, default=_UNSET Hdu extension to use. If `_UNSET`, uses `config.hdu_idx`. dtype : np.dtype | str | _Unset, optional, default=_UNSET Data type for the loaded FITS data. If `_UNSET`, will use the dtype of the provided data, promoting integer or unsigned to `config.default_dtype`. print_info : bool | _Unset, optional, default=_UNSET If True, print summary information about the loaded cube. If `_UNSET`, uses `config.print_info`. transpose : bool | _Unset, optional, default=_UNSET If True, transpose each 2D image before stacking into the cube. This will also transpose each error array if available and swap the WCS axes for consistency. The swapping of the WCS can be disabled by `config.invert_wcs_if_transpose`. If `_UNSET`, uses `config.transpose`. invert_wcs : bool | _Unset, optional, default=_UNSET If True, will perform a swapaxes(0,1) on the wcs if `transpose=True`. If `_UNSET`, uses `config.invert_wcs_if_transpose`. Returns ------- cube : DataCube A DataCube object containing: * `cube.data` : np.ndarray of shape (T, M, N) * `cube.header` : list of astropy.io.fits.Header objects * `cube.error` : np.ndarray of shape (T, M, N) * `cube.wcs` : list of `astropy.wcs.wcs.WCS` Examples -------- Search for all fits files starting with 'HARPS' with .fits extention and load them: >>> filepath = 'Spectro-Module/raw/HARPS.*.fits' """ _offer_dependency('tqdm') hdu = _resolve_default(hdu, config.hdu_idx) dtype = _resolve_default(dtype, config.default_dtype) print_info = _resolve_default(print_info, config.print_info) transpose = _resolve_default(transpose, config.transpose) invert_wcs = _resolve_default(invert_wcs, config.invert_wcs_if_transpose) # searches for all files within a directory fits_files = sorted(glob.glob(filepath)) if not fits_files: raise FileNotFoundError(f'No FITS files found for pattern: {filepath}') # allocate ixMxN data cube array and header array n_files = len(fits_files) # load first file to determine shape, dtype, and check for errors with fits.open(fits_files[0]) as hdul: if print_info: hdul.info() data = hdul[hdu].data header = hdul[hdu].header err = get_errors(hdul, dtype) dt = _get_dtype(data, dtype) try: wcs = WCS(header) except ValueError: wcs = None if transpose: data = data.T if wcs is not None and invert_wcs: wcs = wcs.swapaxes(0,1) if err is not None: err = err.T # preallocate data cube and headers datacube = np.zeros((n_files, data.shape[0], data.shape[1]), dtype=dt) datacube[0] = data.astype(dt) headers = [] headers.append(header) wcs_list = [] wcs_list.append(wcs) # preallocate error array if needed and error exists error_array = None if error and err is not None: error_array = np.zeros_like(datacube, dtype=dt) error_array[0] = err.astype(dt) # loop through remaining files for i, file in enumerate(tqdm(fits_files[1:], desc='Loading FITS')): with fits.open(file) as hdul: data = hdul[hdu].data headers.append(hdul[hdu].header) err = get_errors(hdul, dt) try: wcs = WCS(headers[i+1]) except ValueError: wcs = None if transpose: data = data.T if wcs is not None and invert_wcs: wcs = wcs.swapaxes(0,1) if err is not None: err = err.T datacube[i+1] = data.astype(dt) if error_array is not None and err is not None: error_array[i+1] = err.astype(dt) wcs_list.append(wcs) if all(w is None for w in wcs_list): wcs_list = None elif any(w is None for w in wcs_list): missing_indices = [i for i, w in enumerate(wcs_list) if w is None] raise ValueError( f'Inconsistent WCS: files at indices {missing_indices} have no WCS, ' f'but other files do. Either all files must have WCS or none should.' ) return DataCube(datacube, headers, error_array, wcs_list) def load_fits(filepath, header=True, error=True, print_info=None, transpose=None, dtype=None, target_wcs=_UNSET, invert_wcs=None, **kwargs): """ Load a FITS file and return its data, header, and errors. The WCS is also extracted if possible. Optionally, the data and errors can be reprojected onto a target wcs. Parameters ---------- filepath : str Path to the FITS file to load. header : bool, default=True If True, return the FITS header along with the data as a FitsFile object. If False, only the data is returned. error : bool, default=True If True, return the 'ERR' extention of the fits file. print_info : bool or None, default=None If True, print HDU information using 'hdul.info()'. If None, uses `config.print_info`. transpose : bool or None, default=None If True, transpose the data array before returning. This will also transpose the error array and swap the WCS axes for consistency. The swapping of the WCS can be disabled by `config.invert_wcs_if_transpose`. If None, uses `config.transpose`. dtype : np.dtype, default=None Data type to convert the FITS data to. If None, determines the dtype from the data. Will convert to np.float64 if not floating. target_wcs : Header, WCS or None, optional, default=`_UNSET` Reproject the input data onto the WCS of another data set. Input data must have a valid header to extract WCS from. If None, will not reproject the input data. If `_UNSET`, uses `config.target_wcs`. invert_wcs : bool or None, optional, default=None If True, will perform a swapaxes(0,1) on the wcs if `transpose=True`. If None, uses `config.invert_wcs_if_transpose`. **kwargs : dict, optional Additional parameters. Supported keywords: * `reproject_method` : {'interp', 'exact'} or None, default=`config.reproject_method` Reprojection method: * 'interp' : use `reproject_interp` * 'exact' : use `reproject_exact` * `return_footprint` : bool or None, optional, default=`config.return_footprint` If True, return both reprojected data and reprojection footprints. If False, return only the reprojected data. * `parallel` : bool, int, str, or None, optional, default=`config.reproject_parallel` If True, the reprojection is carried out in parallel, and if a positive integer, this specifies the number of threads to use. The reprojection will be parallelized over output array blocks specified by `block_size` (if the block size is not set, it will be determined automatically). * `block_size` : tuple, 'auto', or None, optional, default=`config.reproject_block_size` The size of blocks in terms of output array pixels that each block will handle reprojecting. Extending out from (0,0) coords positively, block sizes are clamped to output space edges when a block would extend past edge. Specifying 'auto' means that reprojection will be done in blocks with the block size automatically determined. If `block_size` is not specified or set to None, the reprojection will not be carried out in blocks. Returns ------- FitsFile If header or error is True, returns an object containing: * data: `np.ndarray` of the FITS data * header: `astropy.io.fits.Header` if `header=True` else None * error: `np.ndarray` of the FITS error if `error=True` else None * wcs: `astropy.wcs.wcs.WCS` if `header=True` else None By default, is extracted from the header. If a `target_wcs` is passed in, will override the default header. data : np.ndarray If header is False, returns just the data component. """ # ---- KWARGS ---- reproject_method = kwargs.get('reproject_method', config.reproject_method) return_footprint = kwargs.get('return_footprint', config.return_footprint) parallel = kwargs.get('parallel', config.reproject_parallel) block_size = kwargs.get('block_size', config.reproject_block_size) print_info = _resolve_default(print_info, config.print_info) transpose = _resolve_default(transpose, config.transpose) target_wcs = _resolve_default(target_wcs, config.target_wcs) invert_wcs = _resolve_default(invert_wcs, config.invert_wcs_if_transpose) # disable transpose if reprojecting if target_wcs is not None and transpose: warnings.warn( '`transpose=True` ignored because `target_wcs` was provided.', stacklevel=2 ) transpose = False data = None fits_header = None errors = None wcs = None footprint = None # print fits file info with fits.open(filepath) as hdul: if print_info: hdul.info() # extract data and optionally the header from the file # if header is not requested, return None for hdu in hdul: if hdu.data is not None: # type: ignore data = hdu.data # type: ignore fits_header = hdu.header if header else None # type: ignore break if data is None: raise ValueError( f'No image HDU with data found in file: {filepath}!' ) dt = _get_dtype(data, dtype) data = data.astype(dt, copy=False) if error: errors = get_errors(hdul, dt, transpose) # reproject wcs if user inputs a reference wcs or header # otherwise try to extract wcs from fits header if target_wcs is not None: _require_dependency('reproject') # ensure target_wcs has wcs information if isinstance(target_wcs, Header): wcs = WCS(target_wcs) elif isinstance(target_wcs, WCS): wcs = target_wcs else: raise ValueError( f'target_wcs must be Header or WCS, got {type(target_wcs).__name__}' ) input_wcs = WCS(fits_header).celestial data, footprint = _reproject_wcs((data, input_wcs), wcs, method=reproject_method, return_footprint=True, parallel=parallel, block_size=block_size) if errors is not None: errors = _reproject_wcs((errors, input_wcs), wcs, method=reproject_method, return_footprint=False, parallel=parallel, block_size=block_size) else: # try extracting wcs from header if fits_header is not None: try: wcs = WCS(fits_header) except Exception: wcs = None if transpose: data = data.T if wcs is not None and invert_wcs: wcs = wcs.swapaxes(0, 1) if header or error: fitsfile = FitsFile(data, fits_header, errors, wcs) fitsfile.footprint = footprint if return_footprint else None return fitsfile else: return data def load_spectral_cube( filepath, hdu, error=True, header=True, dtype=None, print_info=None ): """ Load a spectral cube from a FITS file, optionally including errors and header. Parameters ---------- filepath : str Path to the FITS file to read. hdu : int or str HDU index or name to read from the FITS file. error : bool, optional, default=True If True, load the associated error array using `get_errors`. header : bool, optional, default=True If True, load the HDU header. dtype : data-type, optional, default=None Desired NumPy dtype for the error array. If None, inferred from FITS data, promoting integer and unsigned to `np.float64`. print_info : bool or None, optional, default=None If True, print FITS file info to the console. If None, uses `config.print_info`. Returns ------- DataCube A `DataCube` object containing: * data : SpectralCube Fits file data loaded as SpectralCube object. * header : astropy.io.fits.Header Fits file header. * error : np.ndarray Fits file error array. * value : np.ndarray Fits file data as np.ndarray. Ex: data = cube.data """ _require_dependency('spectral-cube') print_info = _resolve_default(print_info, config.print_info) spectral_cube = SpectralCube.read(filepath, hdu=hdu) # initialize error and header objects error_array = None hdr = None # open fits file with fits.open(filepath) as hdul: # print fits info if print_info: hdul.info() # load error array if error: error_array = get_errors(hdul, dtype) # load header if header: hdr = hdul[hdu].header # type: ignore return DataCube(spectral_cube, header=hdr, error=error_array) # Cube Manipulation Functions # --------------------------- def stack_cube( cube, *, idx: int | tuple[int, int] | None = None, method: Literal['mean', 'median', 'sum', 'max', 'min', 'std'] | _Unset = _UNSET, axis: int = 0 ): """ Stack or extract slices from a data cube. This operation converts a 3D cube into a 2D cube. 2D data is left untouched. Parameters ---------- cube : ndarray, Quantity, SpectralCube, or DataCube 3D data cube to stack. idx : int | tuple[int, int] | None, optional, default=None Index specification: * int: extract single slice * tuple[start, end]: extract range (inclusive) * None: use entire cube method : {'mean', 'median', 'sum', 'max', 'min', 'std'} | _Unset, default=_UNSET Stacking method. If `_UNSET`, uses `config.stack_cube_method`. axis : int, optional, default=0 Axis along which to stack. Returns ------- ndarray, Quantity, or SpectralCube Stacked result (2D if axis=0) or extracted slice. """ stack_method = _resolve_default(method, config.stack_cube_method) if stack_method not in config._STACK_METHODS: raise ValueError( f"Unknown method '{stack_method}'. " f"Options: {', '.join(config._STACK_METHODS.keys())}" ) is_rgb = getattr(cube, 'is_rgb', False) cube = get_data(cube) if cube.ndim < 3 or is_rgb: return cube if idx is not None: if isinstance(idx, int): return cube[idx] if isinstance(idx, list): if len(idx) == 1: return cube[idx[0]] if len(idx) != 2: raise ValueError('idx must be an int, [start, end], or None') start, end = idx cube = cube[start:end+1] else: raise TypeError(f'idx must be int, list, or None; got {type(idx).__name__}') if _HAS_SPECTRAL_CUBE and isinstance(cube, SpectralCube): stack_func = getattr(cube, stack_method) return stack_func(axis=axis) return config._STACK_METHODS[stack_method](cube, axis=axis) # Cube/Image Masking Functions # ---------------------------- def mask_image( image, ellipse_region=None, region=None, line_points=None, invert_region=False, above_line=True, preserve_shape=True, existing_mask=None, combine_method='union', **kwargs): """ Mask an image with modular filters. Supports applying an elliptical or annular region mask, an optional line cut (upper or lower half-plane), and combining with an existing mask. Parameters ---------- image : array-like, DataCube, FitsFile, or SpectralCube Input image or cube. If higher-dimensional, the mask is applied to the last two axes. ellipse_region : `EllipsePixelRegion` or `EllipseAnnulusPixelRegion`, optional, default=None Region object specifying an ellipse or annulus. region : str {'annulus', 'ellipse'}, optional, default=None Type of region to apply. Ignored if `ellipse_region` is provided. line_points : array-like, shape (2, 2), optional, default=None Two (x, y) points defining a line for masking above/below. Ex: [[0,2], [20,10]] invert_region : bool, default=False If True, invert the region mask. above_line : bool, default=True If True, keep the region above the line. If False, keep below. preserve_shape : bool, default=True If True, return an array of the same shape with masked values set to NaN. If False, return only the unmasked pixels. existing_mask : ndarray of bool, optional, default=None An existing mask to combine (union) with the new mask. combine_method : {'union', 'intersect'}, optional, default=None If 'union', combine masks with `|`. If 'intersect', use `&`. **kwargs : dict, optional Additional parameters. Supported keywords: * center : tuple of float, optional, default=None Center coordinates (x, y). * w : float, optional, default=None Width of ellipse. * h : float, optional, default=None Height of ellipse. * angle : float, optional, default=0 Rotation angle in degrees. * tolerance : float, list, or tuple, optional, default=2 ±Tolerance (distance from radius) for annulus inner/outer radii. If array-like, uses the first element as the minus bound, and the second element as the positive. Returns ------- masked_image : ndarray or SpectralCube Image with mask applied. Type matches input. masks : ndarray of bool or list If multiple masks are combined, returns a list containing the master mask followed by individual masks. Otherwise returns a single mask. """ # ---- Kwargs ---- center = kwargs.get('center') w = kwargs.get('w') h = kwargs.get('h') angle = kwargs.get('angle', 0) tolerance = kwargs.get('tolerance', 2) if (isinstance(tolerance, (list, tuple)) and len(tolerance) == 2): mtol = tolerance[0] ptol = tolerance[1] else: mtol = tolerance ptol = tolerance # extract units unit = get_unit(image) # ensure working with array if isinstance(image, (DataCube, FitsFile)): image = image.data else: image = np.asarray(image) # determine image shape N, M = image.shape[-2:] y, x = np.indices((N, M)) # empty list to hold all masks masks = [] # early return if just applying an existing mask if (ellipse_region is None and region is None and line_points is None and existing_mask is not None): if existing_mask.shape != image.shape[-2:]: raise ValueError('existing_mask must have same shape as image') if isinstance(image, np.ndarray): if preserve_shape: masked_image = np.full_like(image, np.nan, dtype=float) masked_image[..., existing_mask] = image[..., existing_mask] else: masked_image = image[..., existing_mask] if isinstance(unit, u.UnitBase) and not isinstance(image, Quantity): masked_image *= unit else: # if spectral cube or similar object masked_image = image.with_mask(existing_mask) return masked_image # ---- Region Mask ---- # if ellipse region is passed in use those values if ellipse_region is not None: center = ellipse_region.center a = ellipse_region.width / 2 b = ellipse_region.height / 2 angle = ellipse_region.angle if ellipse_region.angle is not None else 0 # accept user defined center, w, and h values if used elif None not in (center, w, h): a = w / 2 b = h / 2 # stop program if attempting to plot a region without necessary data elif region is not None: raise ValueError("Either 'ellipse_region' or 'center', 'w', 'h' must be provided.") # construct region if _HAS_REGIONS and region is not None: if region.lower() == 'annulus': region_obj = EllipseAnnulusPixelRegion( center=PixCoord(center[0], center[1]), # type: ignore inner_width=2*(a - mtol), inner_height=2*(b - mtol), outer_width=2*(a + ptol), outer_height=2*(b + ptol), angle=angle * u.deg ) elif region.lower() == 'ellipse': region_obj = EllipsePixelRegion( center=PixCoord(center[0], center[1]), # type: ignore width=2*a, height=2*b, angle=angle * u.deg ) else: raise ValueError("region must be 'annulus' or 'ellipse'") # filter by region mask region_mask = region_obj.to_mask(mode='center').to_image((N, M)).astype(bool) if invert_region: region_mask = ~region_mask masks.append(region_mask.copy()) else: # empty mask if no region region_mask = np.ones((N, M), dtype=bool) # ---- Line Mask ---- if line_points is not None: # start from previous mask line_mask = region_mask.copy() # compute slope and intercept of line m, b_line = compute_line(line_points) # filter out points above/below line line_mask &= (y >= m*x + b_line) if above_line else (y <= m*x + b_line) # add line region to mask array masks.append(line_mask.copy()) else: # empty mask if no region line_mask = region_mask.copy() # ---- Combine Masks ---- # start master mask with line_mask (or region if no line) mask = line_mask.copy() # union with existing mask if provided if existing_mask is not None: if existing_mask.shape != mask.shape: raise ValueError('existing_mask must have the same shape as the image') if combine_method == 'union': mask |= existing_mask elif combine_method == 'intersect': mask &= existing_mask else: raise ValueError( f"`combine_method` has to be 'union' or 'intersect'! " f'Got {combine_method}.' ) # ---- Apply Mask ---- # if numpy array: if isinstance(image, np.ndarray): if preserve_shape: masked_image = np.full_like(image, np.nan, dtype=float) masked_image[..., mask] = image[..., mask] else: masked_image = image[..., mask] if isinstance(unit, u.UnitBase) and not isinstance(image, Quantity): masked_image *= unit # if spectral cube object else: masked_image = image.with_mask(mask) # ---- Final Mask List ---- # Return master mask as first element masks = [mask] + masks if len(masks) > 1 else mask return masked_image, masks def compute_line(points): """ Compute the slope and intercept of a line passing through two points. Parameters ---------- points : list or tuple of tuples A sequence containing exactly two points, each as (x, y), e.g., [(x0, y0), (x1, y1)]. Returns ------- m : float Slope of the line. b : float Intercept of the line (y = m*x + b). Notes ----- * The function assumes the two points have different x-coordinates. * If the x-coordinates are equal, a ZeroDivisionError will be raised. """ m = (points[0][1] - points[1][1]) / (points[0][0] - points[1][0]) b = points[0][1] - m*points[0][0] return m, b def detect_edges( input_data: NDArray | str, mode: Literal['rgb', 'sum'] = 'rgb', show_plot: bool | _Unset = _UNSET, origin: Literal['lower', 'upper'] = 'upper', **kwargs ) -> NDArray: """ Detect edges on an image using a sobel filter. Works on images with `ndim ∈ [2,3]`. Parameters ---------- input_data : np.ndarray | str Input data to run sobel filter on. Can either be an array-like or a path to an image readable by `plt.imread`. RGB and RGBA images are supported. mode : {'rgb', 'sum'}, optional, default='rgb' Method to convert to grayscale if ndim > 2. * `'rgb'` : Only take into account `image[:,:,0:3]`. * `'sum'` : Sum the entire second axis. show_plot : bool | _Unset, optional, default=_UNSET If `True`, plot the output image. If `_UNSET`, uses `config.show_plot`. origin : {'lower', 'upper'}, optional, default= 'upper' Pixel origin convention for `ax.imshow`. kwargs : Any keyword argument valid for `visualastro.plotting.ax.imshow`. Returns ------- sobel_img : np.ndarray Output edge detected image. """ show_plot = _resolve_default(show_plot, config.show_plot) if isinstance(input_data, str): image = np.asarray(plt.imread(input_data), dtype=np.float64) else: image = np.asarray(get_value(input_data)) grayscale = image_2_grayscale(image, mode=mode) sobel_img = compute_sobel_filter(grayscale) if show_plot: from visualastro.plotting.ax import ax as _ax _ax.imshow(sobel_img, origin=origin, **kwargs) return sobel_img def compute_sobel_filter(image: NDArray) -> NDArray: """ Convolve an image with the sobel operator. Brings out the edges of an image. Users should use `detect_edges` for a more stable api, which supports images with more than 2 dimensions (i.e. RGB / RGBA). Parameters ---------- image : np.ndarray 2D numpy array. Returns ------- np.ndarray : Edge detected image. """ Kx = np.array([[1., 0., -1.], [2., 0., -2.], [1., 0., -1.]]) Ky = np.array([[1., 2., 1.], [0., 0., 0.], [-1., -2., -1.]]) Gx = convolve(image, Kx, mode='constant') Gy = convolve(image, Ky, mode='constant') return np.sqrt(Gx**2 + Gy**2) def image_2_grayscale( image: NDArray, mode: Literal['rgb', 'sum'] = 'rgb' ) -> NDArray: """ Convert an image to grayscale. RGBA and RGB images are flattened into a NxM image. 2D images are left untouched. Parameters ---------- image : np.ndarray Image to convert to grayscale. mode : {'rgb', 'sum'}, optional, default='rgb' Method to convert to grayscale if ndim > 2. * `'rgb'` : Only take into account `image[:,:,0:3]`. * `'sum'` : Sum the entire second axis. Returns ------- grayscale : np.ndarray Grayscale version of the image, now with ndim=2. """ image = np.asarray(get_value(image)) if image.ndim < 2 or image.ndim > 3: raise ValueError( f'Incorrect image shape! Should have ndim ∈ [2,3], got {image.ndim}.' ) stack_mode = str(mode).lower() if image.ndim == 2: return image if stack_mode == 'rgb': return image[:,:,0:3] @ [0.299, 0.587, 0.114] else: return np.sum(image, axis=2) # Filter Functions # ---------------- def get_filter_wavelength( filter: str | Sequence[str], error_mode: str = 'every' ): """ Convert filter names to wavelengths. https://en.wikipedia.org/wiki/Photometric_system # TODO future astroquerry function Parameters ---------- filter : str or sequence A filter name or a sequence of filter names. error_mode : str If 'every', raises ValueError if any conversion fails. If 'all', raises ValueError if all conversions fail, otherwise uses nan. If 'nan', uses nan for failed conversions even if all nan. No error raised in any scenario. Default: 'every'. Returns ------- wavelength : list List of wavelengths. Raises ------- ValueError If `error_mode` is not in `('every', 'all', 'nan')`. If `error_mode` is 'every' and a filter is not found. If `error_mode` is 'all' and all conversions fail. """ map = { 'u': 365, 'b': 445, 'g': 464, 'v': 551, 'r': 658, 'i': 806, 'z': 900, 'j': 1220, 'h': 1630, 'k': 2190, 'l': 3450, 'm': 4750, 'n': 10500, 'q': 21000 } scalar = False if not _is_iterable(filter): filter = [filter] scalar = True wavelengths = [] for f in filter: try: wavelengths.append(map[f.casefold().replace("'", '')]) except KeyError: if error_mode == 'every': raise ValueError(f'Filter {f} not found.') elif error_mode in ('all', 'nan'): wavelengths.append(np.nan) else: raise ValueError(f'Invalid error_mode: {error_mode}') if error_mode == 'all' and all(np.isnan(wavelengths)): raise ValueError('All filters not found.') return wavelengths[0] if scalar else wavelengths