## [Restored title] Fuzzy impulse detection in grayscale image

### Jo (view profile)

on 19 Nov 2012
Latest activity Commented on by jerin

### jerin (view profile)

on 1 Jul 2014
Accepted Answer by Image Analyst

### Image Analyst (view profile)

[Restored question]

Hello there,

I need help please. I want a matlab code that detect salt & pepper noise in grayscale image using Fuzzy technique. Then apply median filter to all noisy pixels.

Anwar

Image Analyst

### Image Analyst (view profile)

on 19 Nov 2012

Why is fuzzy necessary? It's so easy to find because salt and pepper noise is completely saturated. Why make it complicated with fuzzy when you don't have to, unless you're just playing around learning fuzzy stuff and using this as an exercise?

Jan Simon

### Jan Simon (view profile)

on 8 Dec 2012

@Jo: Why did you delete the contents of the question and your comments? The nature of this forum is to share solutions. Removing the question after Image Analyst has posted a large answer is not friendly.

The only cause I can image for deleting a question is that somebody wants to submit a solution copied from this forum as his/her own work, e.g. for a homework. This would be a misuse of the forum and because I do not want cheating to be supported, I've restored the original text question. Btw., the original text is stored in Google's cache, such that removing it in the forum is not effective at all.

jerin

### jerin (view profile)

on 1 Jul 2014

Thanks a lot for median filter. Could you provide fuzzy based one please? It would be very helpful.

## Tags

No tags are associated with this question.

## Products

No products are associated with this question.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 19 Nov 2012
```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);
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);
```

Image Analyst

### Image Analyst (view profile)

on 19 Nov 2012

[Restored former comment, Jan] Jo has written:

` But these for color image. What about if it is grayscale image?`

Make the obvious modifications. If you can't get it, I'll help you out later tonight, but I have to leave now.

Image Analyst

### Image Analyst (view profile)

on 20 Nov 2012

OK, maybe it's a lot tougher than I thought. Here's the gray scale version. The real part of the algorithm is simply 4 lines: a call to medfilt2(), finding the noise, initializing an output image, and replacing the pixels. Everything else is just making it a fancy demo.

```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 demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'coins.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 = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
title('Original Gray Scale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
```
```% Generate a noisy image with salt and pepper noise.
noisyImage = imnoise(grayImage,'salt & pepper', 0.05);
subplot(2, 2, 2);
imshow(noisyImage);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
```
```% ===============================================
% Everything before was just to get a noisy image.
% Here's where the algorithm really starts.
% Median Filter the image:
medianFilteredImage = medfilt2(noisyImage, [3 3]);
```
```% Find the noise.  It will have a gray level of either 0 or 255.
noiseImage = (noisyImage == 0 | noisyImage == 255);
% Get rid of the noise by replacing with median.
noiseFreeImage = noisyImage; % Initialize
noiseFreeImage(noiseImage) = medianFilteredImage(noiseImage); % Replace.
% Display the image.
subplot(2, 2, 3);
imshow(noiseFreeImage);
title('Restored Image', 'FontSize', fontSize);
```

#### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today