# 阈值分割-毕业设计论文

### 时间：2017-6-30 17:36:52 点击：

核心提示：阈值分割...

1 阈值分割

1.1 图像分割的概述

1.2 阈值分割的基本原理

clc

clear all;

%%%%%%%%%%%%极小值图像切割%%%%%%%%%%%%%%%%%%%%%%%%%

figure(1);

subplot(121),imhist(I); % 观察灰度直方图， 灰度80处有谷，确定阈值T=80

title('直方图');

subplot(1,2,2),imshow(I);

title('原图')

I1=im2bw(I,80/255); % im2bw函数需要将灰度值转换到[0,1]范围内

figure(2)

subplot(3,2,1),imshow(I1);

title('极小值点阈值切割');

%%%%%%%%%%%%%%%%%迭代法图像切割%%%%%%%%%%%%%%%%%%%%%%%%

[x,y]=size(I);

a=imhist(I);

I=double(I);

max=1;

for i=2:x

if a(max)<a(i)

max=i;

end

end

min=1;

for i=2:x

if a(min)>a(i)

min=i;

end

end

z0=max

z1=min

T=(z0+z1)/2;

TT=0;

S0=0; n0=0;

S1=0; n1=0;

allow=0.5;

d=abs(T-TT);

count=0;

while(d>=allow)

count=count+1;

for i=1:x

for j=1:y

if (I(i,j)>=T)

S0=S0+I(i,j);

n0=n0+1;

end

if (I(i,j)<T)

S1=S1+I(i,j);

n1=n1+1;

end

end

end

T0=S0/n0;

T1=S1/n1;

TT=(T0+T1)/2;

d=abs(T-TT);

T=TT;

end

tmax2=T %tmax2 =77.2717

Seg=zeros(x,y);

for i=1:x

for j=1:y

if(I(i,j)>=T)

Seg(i,j)=1;

end

end

end

subplot(322),imshow(Seg);

title('迭代阈值分割1');

SI=1-Seg;

subplot(323),imshow(SI);

title('迭代阈值分割2');

%%%%%%%%%%%%%%%%%%最优法切割%%%%%%%%%%%%%%%%%%%%%%%%%%

[m,n]=size(I);

dt = 1;

d = 0.3;

I9 = I;

I9=double(I9);

while (abs(dt-d)>0.001)

background = 0;

object = 0;

bm = 0;%背景象素数

om = 0;%前景象素数

for i=1:m

for j=1:n

if (I9(i,j)<d)

background = background +I9(i,j);

bm = bm+1;

else

object = object + I9(i,j);

om = om+1;

end

end

end

dt = d;

d = (background/bm+object/om)/2; %假设先验概率相同

end

tmax3=d  %tmax3 =79.9102

for i = 1:m

for j = 1:n

if I9(i,j)<d

I9(i,j)=0;

else

I9(i,j)=1;

end

end

end

subplot(324),imshow(I9);

title('最优阈值法切割');

%%%%%%%%%%%%%%%%%%%%最大方差法图像切割%%%%%%%%%%%%%%%%%%%%%%%%%%%

[r,t]=size(I);

count=imhist(I);

n=r*t;

l=256;

count=count/n;%各级灰度出现的概率

for i=2:l

if count(i)~=0

st=i-1;

break

end

end

%以上循环语句实现寻找出现概率不为0的最小灰度值

for i=l:-1:1

if count(i)~=0;

nd=i-1;

break

end

end

%实现找出出现概率不为0的最大灰度值

f=count(st+1:nd+1);

p=st;q=nd-st;%pq分别是灰度的起始和结束值

u=0;

for i=1:q;

u=u+f(i)*(p+i-1);

ua(i)=u;

end

%计算图像的平均灰度值

for i=1:q;

w(i)=sum(f(1:i));

end

%计算出选择不同k的时候，A区域的概率

d=(u*w-ua).^2./(w.*(1-w));%求出不同k值时类间方差

[y,tp]=max(d);%求出最大方差对应的灰度级

tmax4=tp % tmax=96

y1=zeros(r,t);

for i=1:r

for j=1:t

x1(i,j)=double(I(i,j));

end

end

for i=1:r

for j=1:t

if (x1(i,j)>tp)

y1(i,j)=x1(i,j);

else

y1(i,j)=0;

end

end

end

%上面一段代码实现分割

subplot(325),imshow(y1);

title('最大方差法分割的图像');

%%%%%%%%%%%%%%%%%%%最大熵法图像切割%%%%%%%%%%%%%%%%%%%%%%%%

X=double(I);

[M,N]=size(I);

hist=zeros(1,256);

tic;

for i=1:1:M

for j=1:1:N

fi=X(i,j);

hist(fi+1)=hist(fi+1)+1;

end

end

i=1:1:256;

p=zeros(1,256);

for i=1:1:256

p(i)=hist(i)/(M*N);

end

E1=zeros(1,256);

E2=zeros(1,256);

P=zeros(1,256);

E=zeros(1,256);

for t=1:1:256

for i=1:1:t

P(t)=P(t)+p(i);

end

if(P(t)>0&&P(t)<1)

for i=1:1:t

E1(t)=E1(t)-(p(i)/P(t))*log(p(i)/P(t)+eps);

end

for i=t+1:1:256

E2(t)=E2(t)-(p(i)/(1-P(t)))*log(p(i)/(1-P(t))+eps);

end

end

E(t)=E1(t)+E2(t);

end

MAX=0.000001;

for t=68:1:156

if(E(t)>MAX)

MAX=E(t);

th=t;

end

end

tmax5=th  %tmax5=84

g=zeros(M,N);

for i=1:1:M

for j=1:1:N

if(th<X(i,j))

X(i,j)=1;

g(i,j)=X(i,j);

else X(i,j)=0;

g(i,j)=X(i,j);

end

end

end

X=X.*255;

X=uint8(X);

subplot(326),imshow(X);

title('最大熵阈值分割图');

t_eclapsed=toc;

• 百度搜索