MATLAB: How to display the input image, after when the threshold values exceeds certain limit

Image Processing Toolboximage segmentation

now i am setting the threshold value manually as 150. input image size is [256 256].. I want to display the image which has values greater than 150. The main purpose of this work is to do segmentation.

Best Answer

It's just 3 lines. Just mask the original image:
binaryImage = grayImage > 150;
maskedImage = zeros(size(grayImage)); % Initialize.

maskedImage(binaryImage) = grayImage(binaryImage);
See the full demo below:
% Demo by ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'coins.png';
% 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')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
yl = ylim();
line([150 150], [0 yl(2)], 'Color', 'r');
% Threshold at 150
binaryImage = grayImage > 150;
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Mask the image
maskedImage = zeros(size(grayImage)); % Initialize.
% Assign values from grayImage that are > 150.
maskedImage(binaryImage) = grayImage(binaryImage);
% Display the original gray scale image.
subplot(2, 2, 4);
imshow(maskedImage, []);
title('Masked Image', 'FontSize', fontSize);