I am a beginner in MATLAB.
I have a procedure of removing impulse noise from the picture, but in this, I have to first find the difference in the median and the value of the pixels(which I have already achieved), then I need to arrange the window(3x3) pixels (new ones) in the ascending order and again find median...
What I am unable to do yet is that I dunno the command to arrange data (here, pixels) in ascending or descending order and then selecting the median.
Any help will be tremendously appreciated.
Thanks and Regards
Manpreet Kaur Kapoor
Hello,
You don't need to pre-sort the data in order to calculate its median value. You can simply use median(data(:)) to achieve that.
Hope that helps,
Ram-Li-Czech
I think I did some mistake while typing, the problem is as follows:
The process of removing impulse filter involves:
1. Take a window of size 3x3 centered on the pixel of interest in corrupted image. (Done after separating the three color spaces, i.e. Red, Green and Blue of the picture with noise)
2. Arrange all pixels of the window as a vector. Sort vector in an increasing order and compute median. (Done using medfilt2() function on red Channel)
3. Calculate the difference in the each window pixel and the median of the vector.(Done by subtracting the pixel value of the red Channel and the median, will be repeated for blue and green Channel as well )
4. Arrange all window pixels having differences less than or equal to δ = 0.3 in a vector. (The problem arises here. Because, according to my pixel data, the difference value is either 0 or a whole number greater than 1, which makes the next step result to be generally 0)
5. Sort the new vector, and obtain the median of the sorted vector(second time).
This new median will serve as the corrected term for each pixel in noisy image ( The purpose of which is failed, since all the values are coming to be zero, hence the corrected term is exactly the same as the noisy image)
All I need to confirm is that :
1. Is there any function in MATLAB which performs sorting of pixels in ascending and descending order in a window?
2. How do you arrange all pixels in the window after comparing it with a value? Is there a function in MATLAB for the same?
3. Is there a possibility in step 4 above that the median obtained is in decimals, as I am receiving all real numbers.
Any help will be tremendously appreciated.
Regards
Manpreet Kaur Kapoor
Any help will be tremendously appreciated.
Regards
Manpreet Kaur Kapoor
>
> All I need to confirm is that :
> 1. Is there any function in MATLAB which performs sorting of pixels in ascending and descending order in a window?
> 2. How do you arrange all pixels in the window after comparing it with a value? Is there a function in MATLAB for the same?
I must repeat over and over again in the NG. I'm always reluctant to tackle question when the description in made in verbose without specific example. That leads to miss understanding, and I really dislike wasting my time solving something that is completely off. So please give a short example what you want to achieve.
In waiting, you can just look at the code I posted here where pixels in 3x3 windows are sorted in vector (called 'B' in the function)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/251787
> 3. Is there a possibility in step 4 above that the median obtained is in decimals, as I am receiving all real numbers.
The median is the median. How on earth you want to change the definition of it to cope with your need? It does not make any sense.
Bruno
Well, I have taken up the example of the standard image "Lena'' and performed the above said procedure.
The problem I am facing is that:
Step 2 says, arrange the pixels in ascending order and then find median. I used the function medfilt2
Step - 3 says Calculate the difference between each window pixel and the median of the vector.
And step - 4 says that Arrange all the window pixels having the differences less than or equal to a parameter δ in a vector. (δ = 0.3), the median of which shall become the correcting term.
My issue lies here where I dont have any value less than 0.3 other than 0. Following which my correction term becomes 0.
I am already thinking of talking this out with my guide, but I wanted to be sure that I am not making any mistakes in understanding the sequence of steps discussed earlier in my post.
% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% function test1
clc;
clear; % Erase all existing variables.
close all;
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the
Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No',
'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% 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(2, 3, 1);
imshow(rgbImage, []);
title('Original color Image', 'FontSize', fontSize);
% Add noise
noisyrgbImage = imnoise(rgbImage, 'salt & pepper', .03);
% Display the noisy color image.
subplot(2, 3, 1);
imshow(noisyrgbImage, []);
title('Noisy Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Set the threshold value
thresholdValue = 50;
outputImage = noisyrgbImage; % Initialize to get size.
for colorChannel = 1 : 3
% Extract the individual red, green, or blue color channel.
oneChannel = noisyrgbImage(:, :, colorChannel);
subplot(2, 3, 2);
imshow(oneChannel, []);
caption = sprintf('Color Channel #%d', colorChannel);
title(caption, 'FontSize', fontSize);
% Median Filter the image.
medianFilteredImage = medfilt2(oneChannel, [15 15]);
subplot(2, 3, 3);
imshow(medianFilteredImage, []);
caption = sprintf('Median Filter of Color Channel #%d',
colorChannel);
title(caption, 'FontSize', fontSize);
% Subtract and threshold
diffImage = abs(single(oneChannel) - single(medianFilteredImage));
binaryImage = diffImage > thresholdValue;
subplot(2, 3, 4);
imshow(binaryImage, []);
drawnow;
caption = sprintf('Where the noise is in\nColor Channel #%d',
colorChannel);
title(caption, 'FontSize', fontSize);
% Fix the noise
denoisedImage = oneChannel; % Initialize.
% imshow(denoisedImage, []);
denoisedImage(binaryImage) = medianFilteredImage(binaryImage);
outputImage(:, :, colorChannel) = denoisedImage;
subplot(2, 3, 5);
imshow(denoisedImage, []);
caption = sprintf('Denoised Color Channel #%d', colorChannel);
title(caption, 'FontSize', fontSize);
subplot(2, 3, 6);
imshow(outputImage, []);
drawnow;
caption = sprintf('Noise Cleaned through\nColor Channel #%d',
colorChannel);
title(caption, 'FontSize', fontSize);
% Prompt user to do next channels.
if colorChannel < 3
promptMessage = sprintf('Do you want to Continue processing,\nor
Cancel to abort processing?');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel',
'Continue');
if strcmp(button, 'Cancel')
return;
end
end
end
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original color Image', 'FontSize', fontSize);
% Display the noisy color image again.
subplot(2, 3, 2);
imshow(noisyrgbImage, []);
title('Noisy Color Image', 'FontSize', fontSize);
subplot(2, 3, 6);
imshow(outputImage, []);
drawnow;
title('Final Denoised Image', 'FontSize', fontSize);
"Manpreet Kapoor" <golde...@gmail.com> wrote in message
news:im4hvo$p07$1...@fred.mathworks.com...
> Hi,
>
> I think I did some mistake while typing, the problem is as follows:
>
> The process of removing impulse filter involves: 1. Take a window of size
> 3x3 centered on the pixel of interest in corrupted image. (Done after
> separating the three color spaces, i.e. Red, Green and Blue of the picture
> with noise)
> 2. Arrange all pixels of the window as a vector. Sort vector in an
> increasing order and compute median. (Done using medfilt2() function on
> red Channel)
> 3. Calculate the difference in the each window pixel and the median of the
> vector.(Done by subtracting the pixel value of the red Channel and the
> median, will be repeated for blue and green Channel as well )
> 4. Arrange all window pixels having differences less than or equal to
> δ = 0.3 in a vector. (The problem arises here. Because, according to
> my pixel data, the difference value is either 0 or a whole number greater
> than 1, which makes the next step result to be generally 0)
That suggests to me that your image data is of an integer type (int8, int16,
int32, int64, uint8, uint16, uint32, uint64); you can check this with the
WHOS function. If this is the case, convert your data into a floating point
type using DOUBLE or SINGLE before performing the subtraction.
*snip*
--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com