The histogram threshold method is implemented by using MATLAB histogram threshold method:
clc;clear;close;
I=imread (' e:\role0\003i.bmp ');
I1=rgb2gray (I);
Figure;
Subplot (2,2,1);
Imshow (I1);
Title (' Grayscale image ')
grid on;% display gridlines axis on
; % display coordinate system
[M,n]=size (I1); % measurement image size parameter
gp=zeros (1,256); % pre-created vector for
k=0:255
GP (k+1) =length (Find (I1==k))/(M*n) that holds the probability of grayscale occurrence; % calculates the probability of each level of grayscale appearing, storing it in GP in the corresponding position
end
subplot (2,2,2), bar (0:255,GP, ' g ') % plotting histogram
title (' Grayscale histogram ')
Xlabel (' Gray value ')
ylabel (' probability of occurrence ')
I2=IM2BW (i,150/255);
Subplot (2,2,3), Imshow (I2);
Title (' Segmented image of threshold 150 ')
grid on; % Display grid line
axis on; % display coordinate system
I3=IM2BW (i,200/255); %
subplot (2,2,4), Imshow (I3);
Title (' Segmented image of Threshold 200 ')
grid on; % Display grid line
axis on; % Display coordinate system