Create new binary image from pixel values of another image
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Philip
el 24 de Nov. de 2011
Comentada: Rizwan Chughtai
el 7 de Nov. de 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.
0 comentarios
Respuesta aceptada
Image Analyst
el 24 de Nov. de 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.
5 comentarios
Image Analyst
el 6 de Nov. de 2017
That's a new B, not one just listing pixels that are 13 like your first example. This B seems to be
B = A <= 13;
If you don't know or have the 13 yet, but DO have the B array, you can do
threshold = max(A(B));
Then you'd get B again by doing
B = A <= threshold;
Más respuestas (2)
Andrei Caragea
el 24 de Nov. de 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.
0 comentarios
Image Analyst
el 24 de Nov. de 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);
Ver también
Categorías
Más información sobre Get Started with Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!