Create new binary image from pixel values of another image
Show older comments
I have a grayscale uint8 image, and I want to create a second image that is mostly zeros, but has a 1 wherever the pixel value in the first image is equal to a specific value. So, if my image was a 5x5 image such as:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
and I want to 'get' all values equal to 13, the result would be:
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
I'm sure this task is really simple, but I'm having problems working out how to do it - I would appreciate anyone's help.
Accepted Answer
More Answers (2)
Andrei Caragea
on 24 Nov 2011
0 votes
Try this. Let A be the initial matrix, like the one in the example. Do A=A-13; B=ones(size(A)); B(find(A))=0. B will be a matrix like you want.
Image Analyst
on 24 Nov 2011
Response to your comment/question about masking:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
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
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]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
maskImage = grayImage > 80;
% Display the mask image.
subplot(2, 2, 3);
imshow(maskImage, []);
title('Mask Image', 'FontSize', fontSize);
% Mask the image.
% Alternate way:
% maskedImage = bsxfun(@times, grayImage, cast(maskImage,class(grayImage)));
% Mask image must be converted to the same integer type
% as the integer image we want to mask.
% maskImage = cast(maskImage, class(grayImage));
maskedImage = grayImage; % Initialize
maskedImage(maskImage) = 0; % Do the actual masking.
% Display the masked image.
subplot(2, 2, 4);
imshow(maskedImage, []);
title('Masked Image', 'FontSize', fontSize);
Categories
Find more on Convert Image Type 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!