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.
MATLAB: How to display the input image, after when the threshold values exceeds certain limit
Image Processing Toolboximage segmentation
Related Solutions
Try this:
clc;clearvars;close all;imtool close all; % Close all imtool figures.
workspace;format longg;format compact;fontSize = 20;% Read in gray scale demo image.
folder = 'C:\Users\Lalit\Documents';baseFileName = '8.jpg';% 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; endendgrayImage = 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.
% Threshold the image.
binaryImage = grayImage <= 50;% Fill holes.
binaryImage = imfill(binaryImage, 'holes');% Display the binary image.
subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image', 'FontSize', fontSize);% Mask the image.
maskedImage = grayImage; % Initialize.
maskedImage(~binaryImage) = 0; % Do the masking.
% Display the masked image,
% scaling its intensity for better viewing.
subplot(2, 2, 4);imshow(maskedImage, []);title('Masked Image', 'FontSize', fontSize);
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');if ~hasIPT % User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?'); reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes'); if strcmpi(reply, 'No') % User said No, so exit.
return; endend% Read in a demo image.
folder = pwd % or wherever the image lives
baseFileName = 'batik22.PNG';%===============================================================================
% Read in a color reference image.
% 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; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage);title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);% Take the red channel
grayImage = rgbImage(:,:, 1);subplot(2, 2, 2);imshow(grayImage);axis on;title('Red Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');% Threshold the image
binaryImage = grayImage < 127;subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');% Take the 8 largest blobs, then invert the image
binaryImage = ~bwareafilt(binaryImage, 8);subplot(2, 2, 4);imshow(binaryImage, []);caption = sprintf('Final Binary Image with 8 largest blobs');title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer