how to use filter2

13 visualizaciones (últimos 30 días)
zhang
zhang el 10 de Mayo de 2012
Hi guys
For an image and its associated binary mask, I only want to apply my filter on the pixel whose binary indicator is 1.
How can I do this?
Thanks,
Zhong

Respuestas (1)

Image Analyst
Image Analyst el 10 de Mayo de 2012
Apply the filter to the whole image, then multiply the result by the binary mask. Run this demo:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
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);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
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 by ImageAnalyst','numbertitle','off')
% Filter it
kernel = ones(11);
kernel = kernel / numel(kernel); % Normalize so mean gray level doesn't change.
filteredImage = imfilter(single(grayImage), kernel);
% Display the filtered image.
subplot(2, 2, 2);
imshow(filteredImage, []);
title('Filtered Image', 'FontSize', fontSize);
% Create a mask (ROI)
mask = false(size(grayImage));
mask(70:180, 110:170) = true;
% Display the filtered image.
subplot(2, 2, 3);
imshow(mask, []);
title('Mask Image', 'FontSize', fontSize);
% Replace the image in the mask area with the filtered version.
maskedFilteredImage = grayImage;
maskedFilteredImage(mask) = filteredImage(mask);
% Display the masked filtered image.
subplot(2, 2, 4);
imshow(maskedFilteredImage, []);
title('Masked Filtered Image', 'FontSize', fontSize);
  2 comentarios
zhang
zhang el 11 de Mayo de 2012
But my concern is that applying to part of the image is much faster than the whole image
Sean de Wolski
Sean de Wolski el 11 de Mayo de 2012
Is it that much faster?
And how many times do you have to do this. Don't prematurely optimize.
A = rand(300,300,300)>0.5;
B = rand(300,300,300);
tic
for ii = 1:100
A.*B;
end
toc
%11.8s on my system
Thus it took 11.8 seconds to multiply 100*300^3 elements

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by