viqa.nr_metrics.snr.signal_to_noise_ratio

viqa.nr_metrics.snr.signal_to_noise_ratio(img, signal_center, radius, region_type='cubic', auto_center=False, iterations=5, yuv=True, **kwargs)[source]

Calculate the signal-to-noise ratio (SNR) for an image.

Parameters:
  • img (np.ndarray or Tensor or str or os.PathLike) – Image to calculate score of.

  • signal_center (Tuple(int)) – Center of the signal. Order is (x, y) for 2D images and (x, y, z) for 3D images.

  • radius (int) – Width of the regions.

  • region_type ({'cubic', 'spherical', 'full', 'original'}, optional) – Type of region to calculate the SNR. Default is ‘cubic’. Gets passed to viqa.utils.find_largest_region() if auto_center is True. If auto_center is False, the following options are available: If ‘full’ or ‘original’ the original image is used as signal and background region. If ‘cubic’ a cubic region around the center is used. Alias for ‘cubic’ are ‘cube’ and ‘square’. If ‘spherical’ a spherical region around the center is used. Alias for ‘spherical’ are ‘sphere’ and ‘circle’.

  • auto_center (bool, default False) – Automatically find the center of the image. signal_center and radius are ignored if True.

  • iterations (int, optional) – Number of iterations for morphological operations if auto_center is True. Default is 5.

  • yuv (bool, default True) –

    Important

    Only applicable for color images.

    If True, the input images are expected to be RGB images and are converted to YUV color space. If False, the input images are kept as RGB images.

  • **kwargs (optional) – Additional parameters for data loading. The keyword arguments are passed to viqa.utils.load_data().

Returns:

  • snr_lum (float) – SNR score value for grayscale image.

  • snr_val[…] (float, optional) – SNR score values per channel for color image. The order is Y, U, V for YUV images and R, G, B for RGB images.

    Note

    For RGB images the first return value is the SNR for the luminance channel.

Raises:

ValueError – If the center is not a tuple of integers. If center is too close to the border. If the radius is not an integer. If the image is not 2D or 3D. If the passed region type is not valid.

Notes

This implementation uses a cubic region to calculate the SNR. The calculation for grayscale images is based on the following formula:

\[SNR = \frac{\mu}{\sigma}\]

where \(\mu\) is the mean and \(\sigma\) is the standard deviation.

For color images, the calculation is a lot more complicated. The image is first converted to YUV color space by matrix multiplication with the weighting matrix [1]:

\[\begin{split}\begin{bmatrix} Y \\ U \\ V \\ \end{bmatrix} = \begin{bmatrix} 0.2126 & 0.7152 & 0.0722 \\ -0.09991 & -0.33609 & 0.436 \\ 0.615 & -0.55861 & -0.05639 \\ \end{bmatrix} \begin{bmatrix} R \\ G \\ B \\ \end{bmatrix}\end{split}\]

Then the SNR is calculated for each channel separately [2]:

\[SNR_{channel} = \frac{\mu_{Y}}{\sigma_{channel}}\]

where \(\mu_{Y}\) is the mean of the Y channel and \(\sigma_{channel}\) is the standard deviation of the channel for YUV images and:

\[SNR_{channel} = \frac{\mu_{channel}}{\sigma_{channel}}\]

for RGB images.

References