Open In App

Image Denoising using Dictionary Learning in Scikit Learn

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:



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.




# 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)

Applications of Image Denoising


Article Tags :