Median filter for rgb images

For 2d images for performing median filtering we have inbuilt function medfilt2
is there any function for performing median filtering rgb images

 Risposta accettata

Image Analyst
Image Analyst il 8 Set 2012

0 voti

The help is a good place to look for this information. If you type in median there, you'll see that you can use medfilt2() in the Image Processing Toolbox. I have a demo that uses medfilt2() to remove salt and pepper noise in RGB images, if you want to see it.

9 Commenti

FIR
FIR il 9 Set 2012
ok thanks Analyst ,i there any algorithm for Emprical mode decomposition for rgb images
Jürgen
Jürgen il 9 Set 2012
hey I A,
just out of curiosity, which demo to you mean? because in the example in help that I looked at I think there is a transformation to grayscale first, no? regardsJ
The example in the help applies it to a grayscale image. You could use that to apply it to individual color channels if you want, like I do in my demo below. The demo I referred to is a demo I wrote and posted in Answers before. Here it is again:
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
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);
Jürgen
Jürgen il 9 Set 2012
Ok, I misunderstood the above approach also uses the different channels, I thought you were talking about a different approach, thanks for the clarification
Latha
Latha il 13 Lug 2017
Modificato: Walter Roberson il 13 Lug 2017
Question1: How to calculate PSNR and MSE from this
Question 2: For creating noisy image i used the code below. Is it correct?
R = I(:,:,1); % Red chanel
G = I(:,:,2); % Green Chanel
B = I(:,:,3); % Blue Chanel
NOISY_R = imnoise(R,'salt & pepper',0.02);
subplot(1,3,1);
imshow(NOISY_R);axis off;
title('Red Noisy Chanel','fontname','Times New Roman','fontsize',12);
%please suggest is this correct%
NOISY_G = imnoise(G,'salt & pepper',0.02);
subplot(1,3,2);
imshow(NOISY_G);axis off;
title('Green Noisy Chanel','fontname','Times New Roman','fontsize',12);
NOISY_B = imnoise(B,'salt & pepper',0.02);
subplot(1,3,3);
imshow(NOISY_B);axis off;
title('Blue Noisy Chanel','fontname','Times New Roman','fontsize',12);
Fin_noisy RGB = cat(3,NOISY_R,NOISY_G,NOISY_B);
Noisy image_RGB=imshow(Fin_noisy);
%please suggest is this correct%
%Applying filter at each Nisy component%
Denoise_R = medfilt2(Noisy_R, [3 3]);
Denoise_G = medfilt2(Noisy_G, [3 3]);
Denois_B = medfilt2(Noisy_B, [3 3]);
%Reconstruct the noise free RGB%
Fin_denoise = cat(3,Denoise_R,Denoise_G,Denoise_B);
Please share your views
You can use the built-in functions psnr() and immse().
Your code should work, since it's basically my code, just with renamed variables. My grayscale and color demos are attached.
Latha
Latha il 13 Lug 2017
No. You applied noise to the RGB image and separate the components. But I separate the components of the original RGB image and apply the noise to each channel and create the noisy image. Is this way correct or not I want to know. In psnr calculation, Whether we have to calculate for each channel separately and add or between rgbFixed and the rgbImage alone.
Your salt and pepper noise is not the same in every channel if you do it separately. So while a particular (row,column) might be 255 for red, you may not have it also be 255 for green and blue. Thus your "salt" will be colored salt, not pure white salt.
I believe you can use psnr either way, either on each color channel separately, or on the RGB image together, in which case I think it gives you one overall number rather than 3 psnrs.
Latha
Latha il 14 Lug 2017
Thank you Analyst!!!

Accedi per commentare.

Più risposte (3)

Jürgen
Jürgen il 8 Set 2012

0 voti

Hey,
I would perform it on one of the channels depinding on which features afterwards you want to extract, or perform it on each channel.
or first transform to an other color space
regardsJ
Noor Abbas
Noor Abbas il 16 Ott 2016

0 voti

Hello every one, Could you please help with code for adaptive medium filter. Thanks

3 Commenti

Sure. What help do you need? Are you using my adaptive median filter code from my answer above?
Noor Abbas
Noor Abbas il 12 Set 2017
thanks for your consideration(Image Analyst) I would like to use the adaptive median filter with mammogram images
So start a new question and attach your/my code, and your image, and explain what help you need, such as why it's not working the way you like, or some error that your code (after you modified mine) is throwing.

Accedi per commentare.

DGM
DGM il 9 Gen 2023
Modificato: DGM il 9 Gen 2023
If you want something akin to medfilt2() that works with multichannel images, then you have a few options.
% an RGB uint8 image
inpict = imread('peppers.png');
inpict = imresize(inpict,0.5);
% window size
ws = [11 11];
% using nlfilter() (slow)
sz = size(inpict);
op1 = zeros(sz,class(inpict));
for c = 1:size(inpict,3)
op1(:,:,c) = nlfilter(inpict(:,:,c),ws,@(x) median(x(:)));
end
% using medfilt2()
sz = size(inpict);
op2 = zeros(sz,class(inpict));
for c = 1:size(inpict,3)
op2(:,:,c) = medfilt2(inpict(:,:,c),ws,'symmetric');
end
% using medfilt3() (R2016b or newer)
op3 = medfilt3(inpict,[ws 1]);
% using MIMT nhfilter()
op4 = nhfilter(inpict,'median',ws);
Note that the last three examples have identical output for the specified options, whereas nlfilter() has no padding options, and will exhibit edge effects (pay attention to the corners). IPT medfilt2() will behave similarly with the default padding option.
Using nlfilter() for something simple like a median filter isn't really practical in comparison, but it is an option.
The first three tools are from the Image Processing Toolbox, whereas nhfilter() is from MIMT. While nhfilter() does not require IPT, it will run much faster if IPT is installed.
If instead of what OP asked, you want a noise removal filter instead, you have a few options. You can either reimplement the whole thing, or you can find one. MIMT has two options.
% an RGB uint8 image
inpict0 = imread('peppers.png');
inpict0 = imresize(inpict0,0.5);
% add noise
inpict = imnoise(inpict0,'salt & pepper',0.2);
% use a fixed-window median noise removal filter (similar to IA's demo)
op1 = fmedfilt(inpict,11);
% use an adaptive median noise removal filter
op2 = amedfilt(inpict,5);
See also:

Richiesto:

FIR
il 8 Set 2012

Modificato:

DGM
il 9 Gen 2023

Community Treasure Hunt

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

Start Hunting!

Translated by