masking some desired portion of an image
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Image Analyst
le 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);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Modify Image Colors dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!