How can be implemented the mode filter on image Through GUI in matlab

3 views (last 30 days)
How can be implemented the mode filter on image Through GUI in matlab

Answers (2)

Image Analyst
Image Analyst on 20 Mar 2015
Try nlfilter. Demo attached. Change it to use mode().

Image Analyst
Image Analyst on 20 Mar 2015
Edited: Image Analyst on 20 Mar 2015
Alright, here, I did it for you. Try this:
% Demo to threshold an image with a locally adaptive mode using nlfilter.
function nlfilter_demo()
clc;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% 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 gray scale demo image.
folder = pwd; % Change this if the image is not in the same folder as this m-file.
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo to Threshold Document', 'NumberTitle', 'Off')
% Let's compute and display the histogram, just for fun.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
drawnow;
%--------------------------------------------------------------------------
% Do a local mode of the gray level image using the nlfilter function.
% We'll tell nlfilter to use our LocalMode() function to do its operations.
fun = @(x) LocalMode(x);
doubleImage = im2double(grayImage); % Cast to double.
% Here comes the actual filtering.
localMode = nlfilter(doubleImage, [3, 3], fun);
%--------------------------------------------------------------------------
% All done!
% Display the image.
subplot(2, 2, 3);
imshow(localMode, []);
title('Local Mode', 'FontSize', fontSize);
% Function to take the Otsu threshold of the small patch of gray levels passed in by nlfilter().
function newPixelValue = LocalMode(grayImagePatch)
newPixelValue = false;
try
% New pixel value is the mode.
newPixelValue = mode(grayImagePatch(:));
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from LocalMode()

Tags

Community Treasure Hunt

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

Start Hunting!