If youโve ever wondered how to add noise to image in MATLAB, youโre in the right place! Noise addition is a crucial step in testing image processing algorithms, understanding how they handle real world imperfections, or simply experimenting with different filtering techniques. This blog will guide you through the process of how to add noise to image in MATLAB step by step, including the most commonly used noise types: Gaussian Noise, Salt & Pepper Noise, and Speckle Noise.
By the end of this article, you’ll not only understand the types of noise you can add but also learn how to implement a MATLAB script that applies all these noise types to any image. ๐
What is Noise in Images?
Before we jump into MATLAB, letโs quickly understand what image noise is. Noise refers to unwanted random variations in pixel values, often caused by factors like:
- Sensor imperfections ๐ท
- Environmental interference ๐ฉ๏ธ
- Transmission errors ๐ก
In the context of image processing, noise can degrade the quality of an image, making it look grainy, blurry, or distorted. One way is using different filters you can find application of MMSE & Adaptive Median Filter here
Types of Noise You Can Add in MATLAB
MATLAB provides a straightforward way to add noise using the imnoise
function. Here are the most commonly used types:
1. Gaussian Noise ๐
Gaussian noise, also known as normal noise, follows a bell-shaped distribution. Each pixel value is randomly altered according to this distribution.
The formula for adding Gaussian noise is:
Where
Use Case: Testing smoothing filters like the Gaussian filter.
2. Salt & Pepper Noise ๐ง๐ถ๏ธ
Salt & pepper noise randomly sets some pixels to black (pepper) and others to white (salt). This creates a “sprinkled” effect on the image.
Use Case: Evaluating the performance of median filters and noise removal techniques.
3. Speckle Noise โจ
Speckle noise multiplies pixel values by a random noise factor, simulating interference commonly seen in medical and radar imaging.
Use Case: Testing despeckling algorithms, especially for SAR (Synthetic Aperture Radar) images.
4. Poisson Noise
Poisson noise arises from photon-counting mechanisms in devices like cameras, making it common in low-light conditions.
Use Case: Simulating low-light scenarios for testing algorithms.
How to Add Noise to Images in MATLAB
Letโs now dive into MATLAB and explore how to add these types of noise to an image.
Step-by-Step Tutorial: Add Noise to Image in MATLAB
Hereโs a detailed guide on how to add noise to image in MATLAB. So here we are taking our site logo as the base example thus all the example images would be of the Logo of the site Hishuanigami โค๏ธโค๏ธโค๏ธ
Step 1: Import an Image
First, letโs load an image into MATLAB. If the image is in color, weโll convert it to grayscale for simplicity.
% Step 1: Import an image
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files (*.jpg, *.png, *.bmp)'}, 'Select an Image to Add Noise');
if isequal(filename, 0)
disp('No file selected. Exiting...');
return;
end
% Read the image
input_image = imread(fullfile(pathname, filename));
% Convert to grayscale if the image is in RGB
if size(input_image, 3) == 3
input_image = rgb2gray(input_image);
disp('Converted the image to grayscale.');
end
% Display the original image
figure;
imshow(input_image);
title('Original Image');
Step 2: Add Different Types of Noise
Now weโll use MATLABโs imnoise
function to add Gaussian noise, salt & pepper noise, and speckle noise to the image.
% Step 2: Add Gaussian Noise
gaussian_noisy = imnoise(input_image, 'gaussian', 0, 0.01); % Mean=0, Variance=0.01
% Add Salt & Pepper Noise
saltpepper_noisy = imnoise(input_image, 'salt & pepper', 0.02); % Density=0.02
% Add Speckle Noise
speckle_noisy = imnoise(input_image, 'speckle', 0.04); % Variance=0.04
Step 3: Display and Save the Noisy Images
Weโll display the original and noisy images side-by-side for easy comparison. Each noisy image will also be saved to your working directory.
% Display and save images
figure;
subplot(2, 2, 1), imshow(input_image), title('Original Image');
subplot(2, 2, 2), imshow(gaussian_noisy), title('Gaussian Noise');
subplot(2, 2, 3), imshow(saltpepper_noisy), title('Salt & Pepper Noise');
subplot(2, 2, 4), imshow(speckle_noisy), title('Speckle Noise');
% Save the noisy images
imwrite(gaussian_noisy, 'noisy_image_gaussian.png');
imwrite(saltpepper_noisy, 'noisy_image_saltpepper.png');
imwrite(speckle_noisy, 'noisy_image_speckle.png');
disp('Noisy images saved as noisy_image_gaussian.png, noisy_image_saltpepper.png, and noisy_image_speckle.png.');
So, in the above image we can see the noise, but it is not very prominent so what i did was just adjusted the parameters in Step 2 and tuned them up a bit here are the updated parameters
% Step 2: Add Noise
gaussian_noisy = imnoise(input_image, 'gaussian', 0, 0.07);
saltpepper_noisy = imnoise(input_image, 'salt & pepper', 0.09);
speckle_noisy = imnoise(input_image, 'speckle', 0.05);
And the Result of this is actually a noise that can be seen and not just sprinkle here and there, you can adjust these parameters in whatever way you want.
Complete Script
Hereโs the full script for convenience:
% Add Noise to an Image and Save It
% Step 1: Import an image
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files (*.jpg, *.png, *.bmp)'}, 'Select an Image to Add Noise');
if isequal(filename, 0)
disp('No file selected. Exiting...');
return;
end
input_image = imread(fullfile(pathname, filename));
if size(input_image, 3) == 3
input_image = rgb2gray(input_image);
disp('Converted the image to grayscale.');
end
figure; imshow(input_image); title('Original Image');
% Step 2: Add Noise Update the Parameters in whatever way you want to see results
gaussian_noisy = imnoise(input_image, 'gaussian', 0, 0.01);
saltpepper_noisy = imnoise(input_image, 'salt & pepper', 0.02);
speckle_noisy = imnoise(input_image, 'speckle', 0.04);
% Display and Save
figure;
subplot(2, 2, 1), imshow(input_image), title('Original Image');
subplot(2, 2, 2), imshow(gaussian_noisy), title('Gaussian Noise');
subplot(2, 2, 3), imshow(saltpepper_noisy), title('Salt & Pepper Noise');
subplot(2, 2, 4), imshow(speckle_noisy), title('Speckle Noise');
imwrite(gaussian_noisy, 'noisy_image_gaussian.png');
imwrite(saltpepper_noisy, 'noisy_image_saltpepper.png');
imwrite(speckle_noisy, 'noisy_image_speckle.png');
disp('Noisy images saved as noisy_image_gaussian.png, noisy_image_saltpepper.png, and noisy_image_speckle.png.');
Why Add Noise to an Image in MATLAB?
The are of addition of noise is not just for fun it serves critical purposes in image processing:
- Testing Filters: Check the effectiveness of noise reduction techniques.
- Simulating Real-World Scenarios: Prepare algorithms to handle imperfect inputs.
- Enhancing Robustness: Make systems resilient to environmental challenges.
Final Thoughts
Learning how to add noise to image in MATLAB is a powerful skill that bridges the gap between theoretical concepts and practical applications. Whether you’re a beginner learning about noise or an advanced researcher, this tutorial gives you the tools to experiment and test effectively. ๐ ๏ธ
Try out the script and unleash your creativity with noisy images! If you have any questions or cool ideas to share, drop them in the comments below. Happy Coding & MATLAB-ing! ๐๐๐
Thank for reading our guide on how to add noise to image in MATLAB
If you want to Learn how to apply Gaussian Filter to any image to reduce the Gaussian Noise, you can find that out here and if you want to learn how to Implement MMSE filter or Adaptive Median Filter you can find that out here