Need code for Median Filtering on Color images

13 visualizaciones (últimos 30 días)
Jagadeesh p
Jagadeesh p el 22 de Mzo. de 2012
Editada: SARATH JV el 23 de Sept. de 2017
Need Code for Median Filtering on Color images
Cheers
Jagadeesh
  1 comentario
Oleg Komarov
Oleg Komarov el 22 de Mzo. de 2012
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

Iniciar sesión para comentar.

Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 22 de Mzo. de 2012
Either do the median filter on the individual R,G and B planes. Or trasform the RGB image to some other colour format, for example HSV/HSI and do the median filtering on the Hue, Saturaion and Intensity planes and then transfer back to RGB. Matlab has a function for 2-D median filtering:
help medfilt2
HTH

Más respuestas (4)

Image Analyst
Image Analyst el 22 de Mzo. de 2012
Here's a demo I've posted before. It gets rid of salt and pepper noise in a color image by median filtering the individual color planes and replacing the "salt" or "pepper" (bad) pixels with pixels taken from the corresponding location in the median filtered image. It's well commented so I'm sure you'll be easily able to follow it and make any modifications that you desire.
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
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);
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);
  3 comentarios
selvaraj k
selvaraj k el 21 de Feb. de 2017
sir i can't understand this code part..please help me..
% 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);
Image Analyst
Image Analyst el 22 de Feb. de 2017
noiseImage is a binary image that is true for pixels that are pure black or pure white.
Doing noiseFreeRed = redChannel; initializes the output to the noisy input.
Doing
noiseFreeRed(noiseImage) = redMF(noiseImage);
replaces the pixels that are in the mask (pixels that are pure black or pure white only) with the corresponding pixels in the same location in the median filtered image. So the whole image is not changed to be the median filter, only the corrupted pixels are changed and replaced with the fixed/good median filtered values.

Iniciar sesión para comentar.


uvan siya
uvan siya el 30 de En. de 2013
i need coding for mean shift filtering alone can anyone post for me plsssss

Latha
Latha el 13 de Jul. de 2017
Can we apply the wiener filter(wiener2) in the same way i.e without using rgb2gray

SARATH JV
SARATH JV el 23 de Sept. de 2017
Editada: SARATH JV el 23 de Sept. de 2017
@ Image Analyst Worked perfectly simple and explained prgm with great coding well done and thanks for uploading..

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by