image_stats.py#
This script loads a spectral image series, specified by util.Settings
,
calculates the mean and standard deviation for the highest SNR image in each spectral bracket,
and saves the results to an H5 file.
- image_stats.main(args=None, **kw)[source]#
Compute stats for a spectral image stack.
Load the image stack using
load_images
and take the mean.Subtract the fixed-pattern noise (dust spots, etc) relative to the mean using
fpn
–the remaining noise is treated as temporal image noise.Finally, the statistics are saved using
CameraSamples
.
If called without parameters, parses arguments from the command line. Arguments may be passed as a list of strings or as keyword arguments. E.g.:
main(['-i','settings.yml','--quiet']) # or main(input='settings.yml',quiet=True)
- Parameters:
input (
-i
,--input
, Path) – input settings yaml fileoutput (
-o
,--output
, Path) – output h5 filequiet (
--quiet
, bool) – suppress status messagesmax (
--max
, float, default 0.995) – set overexposure threshold from (0 - 1.0)
- class image_stats.CameraSamples(path: pathlib.Path | None = None)[source]#
Load and save camera sample data.
- Parameters:
path –
None
or path to h5 file to load.
- nom_wl: numpy.ndarray#
Nominal wavelengths for each sample image. Shape (N,).
- mean: numpy.ndarray#
Mean of each sample image. Shape (N,C) – number of samples, colour channels.
- std: numpy.ndarray#
Standard deviation of each sample image. Shape (N,C).
- n: int#
Number of pixels averaged for each sample image.
- exp_time: numpy.ndarray#
Exposure time of each sample image. Shape (N,).
- ND: float#
Neutral density filter value.
- camera: str#
Camera name/description.
- lens: str#
Lens name/description.
- settings: str#
Camera & lens settings used.
- label: str#
Short name to identify the dataset.
- load(path: Path)[source]#
Load data from
path
.Load data from H5 file at
path
. Missing items will be set toNone
.
- save(path: Path)[source]#
Save data to
path
.Save data to h5 file at
path
. Existing files will be overwritten.
- image_stats.snr(img: array_like) float [source]#
Compute the SNR of a multi-channel image.
Computes the signal to noise ratio (SNR) of an N-channel image, based on the covariance between colour channels. This captures possible correlations between the channels rather than treat them as independent random variables.
noise = sqrt(sum(cov(img.reshape((-1,N)).T)))/N snr = mean(img)/noise
- Parameters:
img (array_like[(..., N)]) – N-channel image.
- Returns:
snr – The signal to noise ratio of the image
- Return type:
float
- image_stats.load_images(settings: Settings, overexp: float = 0.995) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray] [source]#
Load a spectral image stack.
The images are specified by
util.Settings
and cropped bysettings.roi
. Images with any pixel values overoverexp
are rejected, the image with the best SNR from each wavelength bracket is kept and normalized by its exposure time.This function prints status messages. Use
util.QuietPrint
to suppress them.- Parameters:
settings – A
util.Settings
instance describing the image stack.overexp – The overexposure threshold.
- Returns:
wavelengths ((N) ndarray) – The nominal wavelength of each sample.
samples ((N,H,W,C) ndarray) – The sample image, normalized by the exposure time.
exptimes ((N) ndarray) – The exposure time of each sample.
- image_stats.fpn(img_stack: array_like, stimulus: array_like) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray] [source]#
Compute the fixed-pattern noise of an image stack.
Compute the fixed-pattern noise and residuals of an image stack, assuming a linear pixel response. For each pixel
i,j
we fit a linear model:img_stack[:,i,j] = offset[i,j] + bias[i,j]*stimulus[:]
The fixed pattern noise is described by
offset
andbias
. The residuals cover any remaining noise (e.g. temporal).- Parameters:
img_stack (array_like[(N, H, W, C)]) – Stack of N images.
stimulus (array_like[(N,C)]) – Expected pixel values
- Returns:
offset (ndarray[(H,W,C)])
bias (ndarray[(H,W,C)])
residuals (ndarray[(N, H, W, C)]) –
residuals = img_stack - (offset + bias*stimulus)