masking some desired portion of an image

3 views (last 30 days)
How to mask a portion of a rgb(tif) and a gray image by using a gif mask(0/255)?
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
this solution is not working.Please help.

Accepted Answer

Image Analyst
Image Analyst on 10 Mar 2012
It definitely DOES work, but you need to make your mask in the range 0-1. Here's proof:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
mask = zeros(rows, columns, 'uint8');
mask(30:300, 50:150) = 1;
% Normalize to 0 - 1.
mask = mask / 255;
% Display the mask.
subplot(2, 2, 2);
imshow(mask, []);
title('Mask', 'FontSize', fontSize);
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
% Display the mask.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
title('MaskedImage', 'FontSize', fontSize);

More Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!