1.迭代法
I=imread('rice.png');
ZMax=max(max(I));
ZMin=min(min(I));
TK=(ZMax+ZMin)/2;
bCal=1;
iSize=size(I);
while(bCal)
iForeground=0;
iBackground=0;
ForegroundSum=0;
BackgroundSum=0;
for i=1:iSize(1)
for j=1:iSize(2)
tmp=I(i,j);
if(tmp>=TK)
iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmp);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmp);
end
end
end
ZO=ForegroundSum/iForeground;
ZB=BackgroundSum/iBackground;
TKTmp=uint8((ZO+ZB)/2);
if(TKTmp==TK)
bCal=0;
else
TK=TKTmp;
end
end
disp(strcat('迭代后的域值:',num2str(TK)));
newI=im2bw(I,double(TK)/255);
subplot(121),imshow(I)
subplot(122),imshow(newI)
运行结果:迭代后的域值:131
I=imread('rice.png');
ZMax=max(max(I));
ZMin=min(min(I));
TK=(ZMax+ZMin)/2;
bCal=1;
iSize=size(I);
while(bCal)
iForeground=0;
iBackground=0;
ForegroundSum=0;
BackgroundSum=0;
for i=1:iSize(1)
for j=1:iSize(2)
tmp=I(i,j);
if(tmp>=TK)
iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmp);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmp);
end
end
end
ZO=ForegroundSum/iForeground;
ZB=BackgroundSum/iBackground;
TKTmp=uint8((ZO+ZB)/2);
if(TKTmp==TK)
bCal=0;
else
TK=TKTmp;
end
end
disp(strcat('迭代后的域值:',num2str(TK)));
newI=im2bw(I,double(TK)/255);
subplot(121),imshow(I)
subplot(122),imshow(newI)
运行结果:迭代后的域值:131
2.大津法
%大津法
I=imread('coins.png');
subplot(131),imshow(I);
title('原始图像')
level=graythresh(I);
BW=im2bw(I,level);
subplot(132),imshow(BW)
title('graythresh计算阈值')
disp(strcat('graythresh计算灰度阈值:',num2str(uint8(level*255))))
iMax=max(max(I));
iMin=min(min(I));
T=double(iMin:iMax);
iSize=size(I);
muxSize=iSize(1)*iSize(2);
for i=1:length(T)
TK=T(1,i);
iForeground=0;
iBzckground=0;
ForegroundSum=0;
BzckgroundSum=0;
for j=1:iSize(1)
for k=1:iSize(2)
tmpData=I(j,k);
if(tmpData>=TK)
iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmpData);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmpData);
end
end
end
w0=iForeground/muxSize;
w1=iBackground/muxSize;
u0=ForegroundSum/iForeground;
u1=BackgroundSum/iBackground;
T(2,i)=w0*w1*(u0-u1)*(u0-u1);
end
oMax=max(T(2,:);
idx=find(T(2,:)>=oMax);
T=uint8(T(1,idx));
disp(strcat('简化大津法计算灰度阈值:',num2str(T)))
BW=im2bw(I,double(T)/255);
subplot(133),imshow(BW)
title('简化大津法计算灰度阈值')运行结果:
graythresh计算灰度阈值:126
%大津法
I=imread('coins.png');
subplot(131),imshow(I);
title('原始图像')
level=graythresh(I);
BW=im2bw(I,level);
subplot(132),imshow(BW)
title('graythresh计算阈值')
disp(strcat('graythresh计算灰度阈值:',num2str(uint8(level*255))))
iMax=max(max(I));
iMin=min(min(I));
T=double(iMin:iMax);
iSize=size(I);
muxSize=iSize(1)*iSize(2);
for i=1:length(T)
TK=T(1,i);
iForeground=0;
iBzckground=0;
ForegroundSum=0;
BzckgroundSum=0;
for j=1:iSize(1)
for k=1:iSize(2)
tmpData=I(j,k);
if(tmpData>=TK)
iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmpData);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmpData);
end
end
end
w0=iForeground/muxSize;
w1=iBackground/muxSize;
u0=ForegroundSum/iForeground;
u1=BackgroundSum/iBackground;
T(2,i)=w0*w1*(u0-u1)*(u0-u1);
end
oMax=max(T(2,:);
idx=find(T(2,:)>=oMax);
T=uint8(T(1,idx));
disp(strcat('简化大津法计算灰度阈值:',num2str(T)))
BW=im2bw(I,double(T)/255);
subplot(133),imshow(BW)
title('简化大津法计算灰度阈值')运行结果:
graythresh计算灰度阈值:126