stouputils.data_science.data_processing.image.denoise module#

nlm_denoise_image(image: ndarray[Any, dtype[Any]], h: float = 10, template_window_size: int = 7, search_window_size: int = 21, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply Non-Local Means denoising to an image.

This algorithm replaces each pixel with an average of similar pixels found anywhere in the image. It is highly effective for removing Gaussian noise while preserving edges and details.

Parameters:
  • image (NDArray[Any]) – Image to denoise

  • h (float) – Filter strength (higher values remove more noise but may blur details)

  • template_window_size (int) – Size of the template window for patch comparison (should be odd)

  • search_window_size (int) – Size of the search window (should be odd)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Denoised image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> denoised = nlm_denoise_image(image.astype(np.uint8), 10, 3, 5)
>>> denoised.shape == image.shape
True
>>> ## Test with colored image
>>> rgb = np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8)
>>> denoised_rgb = nlm_denoise_image(rgb, 10, 5, 11)
>>> denoised_rgb.shape == rgb.shape
True
>>> ## Test invalid inputs
>>> nlm_denoise_image("not an image", 10)
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> nlm_denoise_image(image.astype(np.uint8), "10")
Traceback (most recent call last):
        ...
AssertionError: h must be a number, got <class 'str'>
>>> nlm_denoise_image(image.astype(np.uint8), 10, 4)
Traceback (most recent call last):
        ...
AssertionError: template_window_size must be odd, got 4
bilateral_denoise_image(image: ndarray[Any, dtype[Any]], d: int = 9, sigma_color: float = 75, sigma_space: float = 75, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply Bilateral Filter denoising to an image.

Bilateral filtering smooths images while preserving edges by considering both spatial proximity and color similarity between pixels.

Parameters:
  • image (NDArray[Any]) – Image to denoise

  • d (int) – Diameter of each pixel neighborhood

  • sigma_color (float) – Filter sigma in the color space

  • sigma_space (float) – Filter sigma in the coordinate space

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Denoised image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> denoised = bilateral_denoise_image(image.astype(np.uint8))
>>> denoised.shape == image.shape
True
>>> ## Test with colored image
>>> rgb = np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8)
>>> denoised_rgb = bilateral_denoise_image(rgb)
>>> denoised_rgb.shape == rgb.shape
True
>>> ## Test invalid inputs
>>> bilateral_denoise_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> bilateral_denoise_image(image.astype(np.uint8), "9")
Traceback (most recent call last):
        ...
AssertionError: d must be a number, got <class 'str'>
tv_denoise_image(image: ndarray[Any, dtype[Any]], weight: float = 0.1, iterations: int = 30, method: Literal['chambolle', 'bregman'] = 'chambolle', ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply Total Variation denoising to an image.

Total Variation denoising removes noise while preserving sharp edges by minimizing the total variation of the image.

Parameters:
  • image (NDArray[Any]) – Image to denoise

  • weight (float) – Denoising weight (higher values remove more noise)

  • iterations (int) – Number of iterations

  • method (str) – Method to use (“chambolle” or “bregman”)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Denoised image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> denoised = tv_denoise_image(image.astype(np.uint8), 0.1, 30)
>>> denoised.shape == image.shape
True
>>> ## Test invalid inputs
>>> tv_denoise_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> tv_denoise_image(image.astype(np.uint8), "0.1")
Traceback (most recent call last):
        ...
AssertionError: weight must be a number, got <class 'str'>
wavelet_denoise_image(image: ndarray[Any, dtype[Any]], sigma: float | None = None, wavelet: str = 'db1', mode: str = 'soft', wavelet_levels: int = 3, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply Wavelet denoising to an image.

Wavelet denoising decomposes the image into wavelet coefficients, applies thresholding, and reconstructs the image with reduced noise.

Parameters:
  • image (NDArray[Any]) – Image to denoise

  • sigma (float) – Noise standard deviation. If None, it’s estimated from the image.

  • wavelet (str) – Wavelet to use

  • mode (str) – Thresholding mode (‘soft’ or ‘hard’)

  • wavelet_levels (int) – Number of wavelet decomposition levels

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Denoised image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> import importlib.util
>>> has_pywt = importlib.util.find_spec('pywt') is not None
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> if has_pywt:
...     denoised = wavelet_denoise_image(image.astype(np.uint8))
...     denoised.shape == image.shape
... else:
...     True
True
>>> ## Test invalid inputs
>>> wavelet_denoise_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> wavelet_denoise_image(image.astype(np.uint8), wavelet=123)
Traceback (most recent call last):
        ...
AssertionError: wavelet must be a string, got <class 'int'>
adaptive_denoise_image(image: ndarray[Any, dtype[Any]], method: Literal['nlm', 'bilateral', 'tv', 'wavelet'] | str = 'nlm', strength: float = 0.5, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply adaptive denoising to an image using the specified method.

This is a convenience function that selects the appropriate denoising method and parameters based on the image content and noise level.

Parameters:
  • image (NDArray[Any]) – Image to denoise

  • method (str) – Denoising method to use (“nlm”, “bilateral”, “tv”, or “wavelet”)

  • strength (float) – Denoising strength (0.0 to 1.0)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Denoised image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> denoised = adaptive_denoise_image(image.astype(np.uint8), "nlm", 0.5)
>>> denoised.shape == image.shape
True
>>> ## Test invalid inputs
>>> adaptive_denoise_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> adaptive_denoise_image(image.astype(np.uint8), "invalid_method")
Traceback (most recent call last):
        ...
AssertionError: method must be one of: nlm, bilateral, tv, wavelet