MATLAB Answers

noise removal from colored image

197 views (last 30 days)
matlab_image on 4 Aug 2012
Edited: Asif Haque on 6 Sep 2019
Hi all, Please tell me how can I remove noise from colored image. I have tried filters like medfilt2, ordfilt2, wiener2. but it gives error like "Function ORDFILT2 expected its first input, A, to be two-dimensional."
Please suggest me which filter should I apply. Thanks.


Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 4 Aug 2012
There are hundreds, if not thousands, of noise removal methods. Here is a very simple one to remove Salt and Pepper noise using a modified median filter:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual red, green, and blue color channels.
subplot(3, 4, 2);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
title('Blue Channel', 'FontSize', fontSize);
% Generate a noisy image. This has salt and pepper noise independently on
% each color channel so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', 0.05);
subplot(3, 4, 5);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, 1);
greenChannel = noisyRGB(:, :, 2);
blueChannel = noisyRGB(:, :, 3);
% Display the noisy channel images.
subplot(3, 4, 6);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
title('Noisy Blue Channel', 'FontSize', fontSize);
% Median Filter the channels:
redMF = medfilt2(redChannel, [3 3]);
greenMF = medfilt2(greenChannel, [3 3]);
blueMF = medfilt2(blueChannel, [3 3]);
% Find the noise in the red.
noiseImage = (redChannel == 0 | redChannel == 255);
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == 0 | greenChannel == 255);
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);
% Find the noise in the blue.
noiseImage = (blueChannel == 0 | blueChannel == 255);
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);
% Reconstruct the noise free RGB image
rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
subplot(3, 4, 9);
title('Restored Image', 'FontSize', fontSize);


Show 1 older comment
Image Analyst
Image Analyst on 24 Nov 2016
Yes, of course. That's what makes it a demo.
If you already have a noisy image, then you can start off using that image. But if you don't have one already, what are you going to do??? You have to make one right? Well, that's what I did. Otherwise I'd have nothing to demonstrate.
Asif Haque
Asif Haque on 4 Jul 2019
Hello, Mr. Image Analyst, can you please provide the file exchange link for this code, if there is any. If this code is written by you can you please tell me if the code was created for just helping on the answers section, or this code is part of a journal paper and has a name for the filter?
Image Analyst
Image Analyst on 4 Jul 2019
I have not uploaded it to the File Exchange. It's just one of over 300 demos I have. I doubt there are any papers on it - if there are, there shouldn't be because it's nothing worthy of a publication - it's just straightforward simple image processing. No journal worth its salt would publish such an obvious and simple algorithm.

Sign in to comment.

More Answers (0)

Sign in to answer this question.