Create new binary image from pixel values of another image

25 views (last 30 days)
Philip on 24 Nov 2011
Commented: Rizwan Chughtai on 7 Nov 2017
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.

Image Analyst on 24 Nov 2011
Yes it is very simple - just one single line. Simply say:
M_matched = M == matchValue;
Where M is your input matrix, matchValue is the value you want to find (e.g. 13), and M_matched is the output matrix. M_Matched is a logical matrix (which is the data type of "binary" images). A logical (binary) matrix is convenient because you can then use it for logical indexing to extract certain values from a matrix. For example you can say
M_extracted = M(M2);
M_extracted will be 13 in your example but it could be several 13's if you have more than one 13 or other values if you use ">=" instead of "==" in my first line of code.
But you can also cast M_matched to int32 or double or whatever you want, if you need to do that.
Rizwan Chughtai on 7 Nov 2017
Thank you @image anaylst

Andrei Caragea on 24 Nov 2011
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
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
% 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.
subplot(2, 2, 3);
% Alternate way:
% Mask image must be converted to the same integer type
% as the integer image we want to mask.
subplot(2, 2, 4);
Philip on 25 Nov 2011
Wow, thanks for all your effort with that - it's pretty perfect!