how to use filter2

11 次查看(过去 30 天)
zhang
zhang 2012-5-10
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

回答(1 个)

Image Analyst
Image Analyst 2012-5-10
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 个评论
zhang
zhang 2012-5-11
But my concern is that applying to part of the image is much faster than the whole image
Sean de Wolski
Sean de Wolski 2012-5-11
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by