how to display an image for threshold value greater than certain level ?
15 views (last 30 days)
Show older comments
In my code, i wished to keep threshold value as 150. I want to display the image only for values greater than 150. What should be the code?
0 Comments
Answers (1)
Image Analyst
on 6 Oct 2012
You just use logical indexing, like this:
binaryImage = grayImage > 150;
maskedImage = grayImage;
maskedImage(~binaryImage) = 0; % Zero out where mask is 0.
Here's a full-blown demo:
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.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', 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]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
binaryImage = grayImage > 150;
% Display the binary mask image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Mask Image', 'FontSize', fontSize);
% Get a masked image.
maskedImage = grayImage; % Initialize.
maskedImage(~binaryImage) = 0; % Zero out where mask is 0.
% Display the binary mask image.
subplot(2, 2, 3);
imshow(maskedImage, []);
title('Masked Grayscale Image', 'FontSize', fontSize);
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!