Open In App

Image Denoising using Dictionary Learning in Scikit Learn

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Scikit-learn’s image denoising technique uses dictionary learning to represent the image as a sparse combination of elements from a dictionary, which helps to reduce the amount of noise in the image. The compressed version of the image is represented by the dictionary, which is a set of basis vectors. The noise is subsequently eliminated by setting the dictionary elements’ coefficients that correspond to the noise to zero.

Why do we need to denoise the images?

The presence of noise in an image can reduce its quality, make it visually unpleasant, and complicate subsequent image processing operations. Several variables, including sensor limits, transmission mistakes, and ambient influences, can cause noise in images. Image denoising eliminates undesirable noise from an image while preserving its key elements and structural integrity. Therefore, denoising techniques aim to recover the original, noise-free content of the image, thereby enhancing its clarity, details, and visual appeal.

Image Denoising using Dictionary Learning

Image denoising using dictionary learning is a technique for removing noise from images while preserving important details and structures. Dictionary learning is a well-liked method in image processing and computer vision. It aims to find an overcomplete set of basis functions (atoms) that can represent the image patches sparsely using a set of training images. By learning a dictionary from the noisy image patches, we can effectively denoise the image by representing each patch as a sparse linear combination of the learned dictionary atoms.

The key concept for image denoising using dictionary learning is to use a learned dictionary of basis functions, or atoms, that can sparsely represent image patches and effectively remove noise from the image. Before diving deep, we need to understand the key concepts related to Image Denoising using Dictionary Learning:

  • Dictionary learning: Dictionary learning is an unsupervised machine learning approach that uses data to learn a set of basic functions or atoms. It tries to represent the data as a dictionary formed by a sparse linear combination of these atoms. By iteratively updating the dictionary and sparse codes, the algorithm finds the most compact and informative representation of the data. In the context of image denoising, dictionary learning follows the following steps:
    • The first step is to learn a dictionary from a set of training data that is similar to the images you want to denoise.
    • The dictionary consists of a set of basis functions, or atoms, that can represent different image patches.
    • Dictionary learning algorithms, such as K-SVD or the method of optimal directions (MOD), are used to iteratively update the dictionary to best fit the training data.
  • Sparse Coding: Sparse coding is a technique that represents each data point (e.g., an image patch) as a linear combination of a small number of atoms from the learned dictionary. It follows the following steps to find the sparsest representation.
    • Once the dictionary is learned, each image patch is sparsely represented as a linear combination of atoms from the dictionary.
    • Sparse coding aims to find a sparse representation that uses a minimal number of atoms to represent the image patch or to reduce the reconstruction error between the original data and its representation using dictionary atoms.
    • Techniques like Orthogonal Matching Pursuit (OMP) or Lasso can be employed to find the sparse codes.
  • entire:
    • After obtaining the sparse codes, the noisy image patches are rebuilt using the learned dictionary and the sparse representations.
    • The reconstruction process involves merging atoms from the dictionary based on corresponding sparse codes.
    • Sparse codes are used to regulate how much each atom contributes to the reconstruction, highlighting significant visual elements while suppressing noise components.
  • Patch Extraction: To apply dictionary learning for image denoising, the input image is divided into small patches. These patches capture local image structures and are used as training samples for dictionary learning. The size of the patches and the patch extraction strategy can affect the denoising performance.
    • Image denoising based on dictionary learning frequently works on small portions of an image rather than the entire image at once.
    • By dividing the image into patches, it is possible to adapt to local conditions and preserve better features and textures.
    • The denoising process is performed patch by patch, and the patches are reassembled to generate the denoised image.

The effectiveness of dictionary learning-based denoising depends on the training data’s quality and representativeness. The training data should include images with similar characteristics to those that require denoising. During the training phase, the noise model should also be examined to guarantee that the dictionary reflects the noise characteristics.

Implementations

Let’s first install the scikit-learn scikit-image package

!pip install scikit-learn scikit-image

The general steps involved in image denoising using dictionary learning are as follows:

  1. Load the noisy image. Input image
  2. Extract small patches from the image.
  3. Reshape the patches for dictionary learning.
  4. Perform dictionary learning to learn a set of basis functions (atoms).
  5. Denoise the patches using sparse coding and the learned dictionary.
  6. Reconstruct the denoised image from the denoised patches.
  7. Display the original noisy image and the denoised image.

Python3




# Import the necessary packages
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color, util, restoration
from sklearn.feature_extraction import image
from sklearn.decomposition import MiniBatchDictionaryLearning
 
# Load the noisy image
# Replace with the path to your image file
image_path = 'flower_noisy.jpg' 
noisy_image = io.imread(image_path)
noisy_image = util.img_as_float(noisy_image)
print('Noise Image shape:',noisy_image.shape)
 
# Extract small patches from the grayscale image
patch_size = (7, 7)
patches = image.extract_patches_2d(noisy_image, patch_size)
print('Number of Patches :',patches.shape[0])
print('Shape of patches:',patches.shape)
# Reshape the patches for dictionary learning
data = patches.reshape(patches.shape[0], -1)
print('Shape of Input data :',data.shape)
 
# Perform dictionary learning
n_components = 100  # Number of dictionary atoms to learn
dl = MiniBatchDictionaryLearning(n_components=n_components,
                                 alpha=1.0,
                                 n_iter=500)
# training
dl.fit(data)
 
# Denoise the patches using the learned dictionary
denoised_patches = np.dot(dl.transform(data), dl.components_)
print('Shape of Output Denoised patches:',denoised_patches.shape)
 
# Reshape the denoised patches back to their original shape
denoised_patches = denoised_patches.reshape(patches.shape)
print('After Reshaping, Output Denoised patches:',denoised_patches.shape)
 
# Reconstruct the denoised image from the patches
reconstructed_image = image.reconstruct_from_patches_2d(denoised_patches, noisy_image.shape)
print('reconstructed_image:',reconstructed_image.shape)
 
# Show the original noisy image and the denoised image
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(noisy_image)
plt.title('Noisy Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(reconstructed_image)
plt.title('Denoised Image')
plt.axis('off')
plt.show()


Output:

Noise Image shape: (321, 481, 3)
Number of Patches : 149625
Shape of patches: (149625, 7, 7, 3)
Shape of Input data : (149625, 147)
Shape of Output Denoised patches: (149625, 147)
After Reshaping, Output Denoised patches: (149625, 7, 7, 3)
reconstructed_image: (321, 481, 3)

Image Denoising using Dictionary Learning-Geeksforgeeks

Applications of Image Denoising

  • Photography: In low-light conditions or high ISO settings, digital images can exhibit noise, resulting in a grainy or pixelated appearance. Denoising techniques help improve image quality by reducing noise while preserving important image details.
  • Surveillance and Security: Images captured by surveillance cameras or security systems may suffer from noise introduced by low-light conditions, compression artefacts, or sensor limitations. Denoising such images is essential to enhance the clarity of important details and aid identification or analysis tasks.
  • Remote Sensing: Images acquired from satellites or aerial platforms for applications like land mapping, environmental monitoring, or crop analysis can be corrupted by noise. Denoising is necessary to improve the quality and reliability of the data for accurate interpretation and analysis.
  • Computer Vision and Object Recognition: Image denoising plays a vital role in computer vision tasks, such as object detection, segmentation, or recognition. Noise reduction allows for better feature extraction, improved accuracy, and robustness of algorithms.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads