ultrasound_metrics.metrics.sharpness

ultrasound_metrics.metrics.sharpness#

Image sharpness metrics, such as Tenengrad.

Functions#

tenengrad(...)

Compute the Tenengrad sharpness metric of an image.

Module Contents#

ultrasound_metrics.metrics.sharpness.tenengrad(image: jaxtyping.Num[ultrasound_metrics._utils.array_api.ArrayAPIObj, height width], roi: Optional[jaxtyping.Bool[ultrasound_metrics._utils.array_api.ArrayAPIObj, height width]] = None, sobel_kernel: Optional[jaxtyping.Num[ultrasound_metrics._utils.array_api.ArrayAPIObj, kernel_height kernel_width]] = None, reduce_sum: bool = True, normalize: bool = False) ultrasound_metrics._utils.array_api.ArrayAPIObj[source]#

Compute the Tenengrad sharpness metric of an image.

Tenengrad sharpness [1][2] is a robust and accurate measure for focusing quality that performs well in digital photography. The metric applies a lateral Sobel filter to detect edges primarily in the horizontal direction, then sums the magnitude of the filtered response within a region of interest.

This implementation is particularly suitable for ultrasound images where the lateral focusing quality is of primary interest.

Parameters:
  • image – Input image for sharpness calculation. Should be a 2D array.

  • roi – Binary mask defining the region of interest. If provided, only pixels where roi is True (or non-zero) will contribute to the sharpness metric. If None, the entire image is used. Can be boolean or numeric array.

  • sobel_kernel – Custom convolution kernel to use for edge detection. If None, uses the default lateral Sobel filter optimized for ultrasound applications [3]: ` [ 1  0 -1] [ 2  0 -2] [ 1  0 -1] ` Custom kernels should be 2D arrays with odd dimensions.

  • reduce_sum – If True (default), returns the summed Tenengrad value as a scalar. If False, returns the magnitude image after Sobel filtering for visualization.

  • normalize – If True and reduce_sum=True, returns the mean instead of sum to normalize by ROI size. This makes the metric independent of ROI area. Default is False.

Returns:

If reduce_sum=True: The Tenengrad sharpness value (scalar). Higher values indicate sharper images. If normalize=True, returns mean instead of sum. If reduce_sum=False: The magnitude image after Sobel filtering (same shape as input).

Return type:

ndarray

Notes

The Tenengrad metric is computed as:

\[F = \sum |G(r)|\]

where \(G(r)\) is the result of convolving the image with the lateral Sobel filter, and the sum is taken over all pixels r in the region of interest.

For ultrasound applications, it’s recommended to exclude near-field artifacts by using an ROI that begins at approximately 10mm depth.

The Tenengrad metric was originally developed for autofocus [1] and has been extensively studied for digital photography [2]. The lateral Sobel kernel used here follows the ultrasound-specific implementation in [3].

References