- function th=zbthresh(m,n,im)
- % a=imread('mytu.tif');
- % im=rgb2gray(a);
- [M,N]=size(im); u=M/m; v=N/n;
- %将图像分为u×v大小的子图
- f=cell(m,n);
- for i=1:m, for j=1:n
- f{i,j}=im((i-1)*u+1:i*u,(j-1)*v+1:j*v);
- end; end
- %检测子图的直方图是否为双峰,同时求出阈值
- th=zeros(m,n);
- for i=1:m, for j=1:n
- th(i,j)=iteration(f{i,j});
- end; end
- %进行灰度插值计算(最邻近插值)
- [I,J]=find(th==0); k=size(I,1);
- TH=zeros(m+2,n+2); %将阈值矩阵的外边框补0
- TH(2:m+1,2:n+1)=th;
- for i=1:k
- T(1)=TH(I(i)+1,J(i)); T(2)=TH(I(i)+1,J(i)+2);
- T(3)=TH(I(i),J(i)+1); T(4)=TH(I(i)+2,J(i)+1);
- for j=1:4
- if T(j)~=0, TH(I(i)+1,J(i)+1)=T(j); end
- end
- end
- th=TH(2:m+1,2:n+1);
- for i=1:m
- for j=1:n
- f{i,j}=(f{i,j}>th(i,j)); im((i-1)*u+1:i*u,(j-1)*v+1:j*v)=f{i,j};
- end
- end
- im=logical(im);
- figure, imshow(im);
复制代码
[ 本帖最后由 ChaChing 于 2010-1-1 23:03 编辑 ] |