Does Matlab have a mode filter?

9 ビュー (過去 30 日間)
Nicholas
Nicholas 2012 年 8 月 28 日
コメント済み: Image Analyst 2020 年 11 月 25 日
I'm trying to filter an image with a mode filter just like medfilt2 but using the mode rather than the median. Does this exist?

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 8 月 28 日
編集済み: Sean de Wolski 2012 年 8 月 28 日
Nope, but you could use colfilt to do this really easily.
doc colfilt
  1 件のコメント
Nicholas
Nicholas 2012 年 8 月 29 日
colfilt(image, [5 5], 'sliding', @mode) seems to work for me. Thanks for the help.

サインインしてコメントする。

その他の回答 (2 件)

Sailesh Sidhwani
Sailesh Sidhwani 2020 年 11 月 25 日
A new 'modefilt' function for 2D and 3D data was added to Image Processing Toolbox in R2020a:
Both 'colfilt' and 'nlfilter' are generic sliding window functions and can be used to perform mode filtering but the expectation is this 'modefilt' to be faster than both of these functions and also work on 3D grayscale data.
  1 件のコメント
Image Analyst
Image Analyst 2020 年 11 月 25 日
Awesome! Very nice. This modefilt() function will be very useful for cleaning up noise in labeled images!

サインインしてコメントする。


Image Analyst
Image Analyst 2012 年 8 月 28 日
See my demo. Copy, paste, save as test.m, and run.
% Demo to take the local mode of a gray scale image.
% userImage, if passed in, is used as the image.
% If userImage is not passed in, user is asked to use a demo image.
function test(userImage)
% Clean up.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
% Don't use these lines if you're calling this from another 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
% Initialize.
fontSize = 20;
if nargin == 0
% No image passed in on the command line.
% Read in one of the standard MATLAB demo images
% as our original gray scale image and display it.
promptMessage = sprintf('Which image do you want to use.\nThe coins or the cameraman?');
button = questdlg(promptMessage, 'Select Image', 'Coins', 'Cameraman', 'Coins');
if strcmp(button, 'Coins')
grayImage = double(imread('coins.png')); % Cast to double.
else
grayImage = double(imread('cameraman.tif')); % Cast to double.
end
else
% Use the image array passed in on the command line.
grayImage = double(userImage); % Cast to double.
end
% Start timing.
startTime = tic;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Perform a mode filter.
% Output image is the mode of the input image in a 3 by 3 sliding window.
ModeFilterFunction = @(x) mode(x(:));
modeImage = nlfilter(grayImage, [3 3], ModeFilterFunction);
% An alternate way of doing the mode filter is on the next line:
% modeImage = reshape(mode(im2col(originalImage,[3 3],'sliding')), size(originalImage)-2);
subplot(2, 2, 2);
imshow(modeImage, [])
title('Mode Image', 'FontSize', fontSize);
elapsedTime = toc(startTime);
message = sprintf('Done!\n\nElapsed time = %.2f seconds.', elapsedTime);
msgbox(message);
return; % End of local_mode() function.
  3 件のコメント
Image Analyst
Image Analyst 2012 年 8 月 29 日
You can see from this line of code:
ModeFilterFunction = @(x) mode(x(:));
that it calls mode(). It's an anonymous function handle that you can use in nlfilter(). You should learn how to use nlfilter() because it will let you do so much more than colfilt().
Sean de Wolski
Sean de Wolski 2012 年 8 月 30 日
True nlfilter is more powerful, but any time you can apply colfilt it will likely be faster.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by