1 迭代法实践代码:
clc;
clear all;
f=imread('cat.bmp');
f=rgb2gray(f);
f=im2double(f);
T=0.5*(min(f(:))+max(f(:)));
done=false;
while ~done
g=f>=T;
Tn=0.5*(mean(f(g))+mean(f(~g)));
done=abs(T-Tn)<0.1;
T=Tn;
end
T
r=im2bw(f,T);
subplot(121),imshow(f),title('原图');
subplot(122),imshow(r),title('阈值分割算法研究--迭代法');
2 迭代法实践截图:
3 均匀性度量法实践代码:
clc;
clear all;
I=imread('cat.bmp');
I=rgb2gray(I);
I=double(I);
[m,n]=size(I);
Smin=-1;
for T=0:255
sum1=0; num1=0;
sum2=0; num2=0;
for i=1:m
for j=1:n
if I(i,j)>=T
sum2=sum2+I(i,j);
num2=num2+1;
else
sum1=sum1+I(i,j);
num1=num1+1;
end
end
end
ave1=sum1/num1;
ave2=sum2/num2;
d1=-1;
d2=-1;
for i=1:m
for j=1:n
if I(i,j)>=T
d=(I(i,j)-ave2)^2;
if d2==-1
d2=d;
else
d2=d2+d;
end
else
d=(I(i,j)-ave1)^2;
if d1==-1
d1=d;
else
d1=d1+d;
end
end
end
end
p1=num1/(m*n);
p2=num2/(m*n);
S=p1*d1+p2*d2;
if(Smin==-1)
Smin=S;
else
if(S<Smin)
Smin=S;
Th=T;
end
end
end
Th
subplot(121),imshow(uint8(I)),title('原图');
for i=1:m
for j=1:n
if I(i,j)>=Th
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(122),imshow(I),title('阈值分割算法研究--均匀性度量法');
4 均匀性度量法实践截图:
5 类间最大距离法实践代码:
clc;
clear all;
I=imread('cat.bmp');
I=rgb2gray(I);
I=double(I);
[m,n]=size(I);
Smax=0;
for T=0:255
sum1=0; num1=0;
sum2=0; num2=0;
for i=1:m
for j=1:n
if I(i,j)>=T
sum2=sum2+I(i,j);
num2=num2+1;
else
sum1=sum1+I(i,j);
num1=num1+1;
end
end
end
ave1=sum1/num1;
ave2=sum2/num2;
S=((ave2-T)*(T-ave1))/(ave2-ave1)^2;
if(S>Smax)
Smax=S;
Th=T;
end
end
Th
subplot(121),imshow(uint8(I)),title('原图');
for i=1:m
for j=1:n
if I(i,j)>=Th
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(122),imshow(I),title('阈值分割算法研究--类间最大距离法');
6 类间最大距离法实践截图:
7 最大类内类间方差比法实践代码:
clc;
clear all;
I=imread('cat.bmp');
I=rgb2gray(I);
I=double(I);
[m,n]=size(I);
Smax=-1;
for T=0:255
sum1=0; num1=0;
sum2=0; num2=0;
for i=1:m
for j=1:n
if I(i,j)>=T
sum2=sum2+I(i,j);
num2=num2+1;
else
sum1=sum1+I(i,j);
num1=num1+1;
end
end
end
ave1=sum1/num1;
ave2=sum2/num2;
ave=(sum1+sum2)/(m*n);
d1=-1;
d2=-1;
for i=1:m
for j=1:n
if I(i,j)>=T
d=(I(i,j)-ave2)^2;
if d2==-1
d2=d;
else
d2=d2+d;
end
else
d=(I(i,j)-ave1)^2;
if d1==-1
d1=d;
else
d1=d1+d;
end
end
end
end
p1=num1/(m*n);
p2=num2/(m*n);
S1=p1*(ave1-ave)^2+p2*(ave2-ave)^2;
S2=p1*d1+p2*d2;
S=S1/S2;
if S>Smax
Smax=S;
Th=T;
end
end
Th
subplot(121),imshow(uint8(I)),title('原图');
for i=1:m
for j=1:n
if I(i,j)>=Th
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(122),imshow(I),title('阈值分割算法研究--最大类内类间方差比法');
8 最大类内类间方差比法实践截图:
9 最大熵法实践代码:
clc;
clear all;
I=imread('cat.bmp');
I=rgb2gray(I);
subplot(121),imshow(I);title('原始图像');
h=imhist(I);
h1=h;
len=length(h);
[m,n]=size(I);
h1=h1/(m*n);
for i=1:(len-1)
if h(i)~=0
P1=sum(h1(1:i));
P2=sum(h1((i+1):len));
else
continue;
end
H1(i)=-(sum(P1.*log(P1)));
H2(i)=-(sum(P2.*log(P2)));
H(i)=H1(i)+H2(i);
end
m1=max(H);
Th=find(H==m1);
Th
for i=1:m
for j=1:n
if I(i,j)>=Th
I(i,j)=255;
else
I(i,j)=0;
end
end
end
subplot(122),imshow(I);title('阈值分割算法研究--最大熵法');
10 最大熵法实践截图:
11 局部阈值实践代码:
clc;
clear all;
I=imread('cat.bmp');
I=rgb2gray(I);
I=im2double(I);
subplot(121),imshow(I),title('原图');
se=strel('disk',10);
ft=imtophat(I,se);
Th=graythresh(ft);
Th
G=im2bw(ft,Th);
subplot(122),imshow(G),title('局部阈值');
12 局部阈值实践截图: