how to use filter2

조회 수: 9 (최근 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에서 3-D Volumetric Image Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by