Image Denoising using Dictionary Learning in Scikit Learn
Last Updated :
24 Apr, 2025
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:
- Load the noisy image. Input image
- Extract small patches from the image.
- Reshape the patches for dictionary learning.
- Perform dictionary learning to learn a set of basis functions (atoms).
- Denoise the patches using sparse coding and the learned dictionary.
- Reconstruct the denoised image from the denoised patches.
- 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)
.png)
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.
Similar Reads
Denoising techniques in digital image processing using MATLAB
Denoising is the process of removing or reducing the noise or artifacts from the image. Denoising makes the image more clear and enables us to see finer details in the image clearly. It does not change the brightness or contrast of the image directly, but due to the removal of artifacts, the final i
6 min read
Sparse Coding with a Precomputed Dictionary in Scikit Learn
A sparse array/matrix is a special type of matrix whose most of the elements are having a value of zero. Generally, the number of non-zero elements in a sparse matrix is equal to the number of rows or columns of the matrix. So, sparse coding can be defined as a representation learning method that is
5 min read
Identifying Overfitting in Machine Learning Models Using Scikit-Learn
Overfitting is a critical issue in machine learning that can significantly impact the performance of models when applied to new, unseen data. Identifying overfitting in machine learning models is crucial to ensuring their performance generalizes well to unseen data. In this article, we'll explore ho
7 min read
Learning Model Building in Scikit-learn
Building machine learning models from scratch can be complex and time-consuming. However with the right tools and frameworks this process can become significantly easier. Scikit-learn is one such tool that makes machine learning model creation easy. It provides user-friendly tools for tasks like Cla
10 min read
Normal and Shrinkage Linear Discriminant Analysis for Classification in Scikit Learn
In this article, we will try to understand the difference between Normal and Shrinkage Linear Discriminant Analysis for Classification. We will try to implement the same using sci-kit learn library in Python. But first, let's try to understand what is LDA. What is Linear discriminant analysis (LDA)?
4 min read
Recognizing HandWritten Digits in Scikit Learn
Scikit learn is one of the most widely used machine learning libraries in the machine learning community the reason behind that is the ease of code and availability of approximately all functionalities which a machine learning developer will need to build a machine learning model. In this article, w
10 min read
Color Quantization using K-Means in Scikit Learn
In this article, we shall play around with pixel intensity value using Machine Learning Algorithms. The goal is to perform a Color Quantization example using KMeans in the Scikit Learn library. Color Quantization Color Quantization is a technique in which the color spaces in an image are reduced to
2 min read
Shrinkage Covariance Estimation in Scikit Learn
The Ledoit and Wolf proposed a formula for shrinkage which is generally used for regularizing the usual maximum likelihood estimation. This formula is called the Ledoit-Wolf covariance estimation formula. This formula is able to compute asymptotically optimal shrinkage parameters by minimizing the m
3 min read
K-Means clustering on the handwritten digits data using Scikit Learn in Python
K - means clustering is an unsupervised algorithm that is used in customer segmentation applications. In this algorithm, we try to form clusters within our datasets that are closely related to each other in a high-dimensional space. In this article, we will see how to use the k means algorithm to i
5 min read
Multidimensional Scaling (MDS) using Scikit Learn
Multidimensional scaling (MDS) is a dimensionality reduction technique that is used to project high-dimensional data onto a lower-dimensional space while preserving the pairwise distances between the data points as much as possible. MDS is based on the concept of distance and aims to find a projecti
7 min read