## noise removal by setting a threshold

### FIR (view profile)

on 22 Dec 2012
Accepted Answer by Image Analyst

### Image Analyst (view profile)

I have a RGB image ,i have to remove noise in it by setting a theshold value please help

## Products

No products are associated with this question.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 22 Dec 2012

I'm not sure how this question is different than the one you already asked and I already answered in http://www.mathworks.com/matlabcentral/answers/56515#answer_68417 In fact, you already accepted someone else's answer to this question. Also, I have posted code to do that for a color image, but it's easier to just paste it here again than to find it again. If you want a threshold other than 255, just make obvious change by changing ==255 to >=someThresholdValue (whatever value you want):

```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';
% 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);
uiwait(warndlg(errorMessage));
return;
end
end
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
imshow(rgbImage);
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);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
imshow(blueChannel);
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);
imshow(noisyRGB);
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);
imshow(redChannel);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
imshow(greenChannel);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
imshow(blueChannel);
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);
imshow(rgbFixed);
title('Restored Image', 'FontSize', fontSize);
```

### Wayne King (view profile)

Answer by Wayne King

### Wayne King (view profile)

on 22 Dec 2012
Edited by Wayne King

### Wayne King (view profile)

on 22 Dec 2012

What do you mean by remove noise by setting a threshold. Normally, in a noisy image model, the value at a particular pixel is assumed to be signal plus noise. To simply set the threshold in the image domain, would therefore remove signal. The usual practice for denoising images is to threshold in a different representation of the image (wavelet domain or frequency domain), then invert the thresholded values. For example, if you have the Wavelet Toolbox, you can denoise an RGB image by obtaining the 2-D wavelet transform, thresholding the wavelet coefficients and inverting.

Below is an example with an image which isn't noisy but the general process is illustrated. There are utilities in the Wavelet Toolbox fo determing "optimal" thresholds.

```   X = imread('arms.jpg');
wname = 'sym4';
level = 4;
sorh = 's';    % Specified soft or hard thresholding
thrSettings =  [...
4.658112694014694      4.374202852603576      4.092379407660262      3.815939516130920 ; ...
4.658112694014694      4.374202852603576      4.092379407660262      3.815939516130920 ; ...
4.658112694014694      4.374202852603576      4.092379407660262      3.815939516130920   ...
];
roundFLAG = true;```
```[coefs,sizes] = wavedec2(X,level,wname);
[XDEN,cfsDEN,dimCFS] = wdencmp('lvd',coefs,sizes, ...
wname,level,thrSettings,sorh);
if roundFLAG , XDEN = round(XDEN); end
if isequal(class(X),'uint8') , XDEN = uint8(XDEN); end
```

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi