Create new binary image from pixel values of another image

조회 수: 6 (최근 30일)
Philip
Philip 2011년 11월 24일
댓글: Rizwan Chughtai 2017년 11월 7일
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
Image Analyst 2011년 11월 24일
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
Image Analyst
Image Analyst 2017년 11월 6일
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;
Rizwan Chughtai
Rizwan Chughtai 2017년 11월 7일
Thank you @image anaylst

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrei Caragea
Andrei Caragea 2011년 11월 24일
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
Image Analyst 2011년 11월 24일
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);
  댓글 수: 1
Philip
Philip 2011년 11월 25일
Wow, thanks for all your effort with that - it's pretty perfect!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by