clear all
clc
I = imread('C:\Documents and Settings\Administrator\桌面\新的极实验\1_256.tif');
% I = imread('lena_B.jpg');
[m,n,l]=size(I);
if l==3
I=rgb2gray(I);
end
figure(1),
imshow(I,[])
title('初始图像');
figure(2),
imhist(I)
title('初始图像的直方图');
T=30;%可以修改的阈值
f=double(I);
[m,n]=size(I);
shed1=zeros(3,round(m*n/2)); %存储区域生长方向上的点和该区域的均值的绝对差值和该点的坐标
sp1=0; % sp1 相当于指针,指向 shed1 中的最后放入的值和坐标
shed2=zeros(2,m); %存储符合生长条件的点的坐标
sp2=0; % sp2 指针,指向 shed2 中的最后放入点的坐标
Cut=ones(size(f))*260; % Cut 为区域生长后的新图像
%%Cut=ones(m,n)*260;
hmin=min(min(f));
hmax=max(max(f));
% if hmax>253
% hmax=253;
% end
h1 = waitbar(0,'Please wait...');
for q=hmin:hmax
for i=1:m
for j=1:n
if (f(i,j)==q&Cut(i,j)==260) %确定该点满足作为种子的条件,且未并入已% 有生长区域
% Cut(i,j)=259; % 259 时,标记该点在原图像的对应点已并入生长区域
ave=f(i,j); %确定新区域的均值的起始值
k=1; %设置生成的区域的象素个数
[Cut,shed1,sp1]=ruzhan(f,Cut,shed1,sp1,ave,i,j,m,n); %把周围的 8 个点入%栈
[shed1,sp1]=arrange(shed1,sp1); %对栈 shed1 的数据进行由大到小的排序
[shed1,sp1,shed2,sp2]=listed(shed1,sp1,T,shed2,sp2); %% 确定符合条件的生%%长点,将它从 shed1 中取出,并放入 shed2 中
end
% 根据生长点开始用 8 连通方式进行生长
while (sp2~=0) %当 sp2=0 时表示找不到符合的点,
% if (sp2~=0) %当有新的值加入区域时,求新的平均值
sum=ave*k;
for t=1:sp2
x=shed2(1,t); y=shed2(2,t);
sum=sum+f(x,y);
k=k+1;
end
ave=sum/k;%%%%合并后的区域的均值
% end
for t=1:sp2 %合并栈 shed2 中的点,生成新的区域
x=shed2(1,t); y=shed2(2,t);
Cut(x,y)=q;
[Cut,shed1,sp1]=ruzhan(f,Cut,shed1,sp1,ave,x,y,m,n);
end
sp2=0;
[shed1,sp1]=arrange(shed1,sp1);
[shed1,sp1,shed2,sp2]=listed(shed1,sp1,T,shed2,sp2);
end
% 在一片区域生成之后,对栈 shed1(1,:)中未并入区域的值进行处理
if (sp1~=0)
for t=1:sp1
x=shed1(2,t); y=shed1(3,t);
Cut(x,y)=260;
end
sp1=0;
shed1=zeros(3,round(m*n/2));
shed2=zeros(2,m);
end
end
end
waitbar(q/(hmax-1))
end
max(max(Cut))
% I=find(Cut>=254);
% Cut(I)=0;
% Cut=im2uint8(Cut);
Cut=uint8(Cut);
imshow(Cut)
BW2 = edge(Cut,'sobel',0.000000000000001);
figure(22), imshow(BW2)
%%%%Cut为区域生长之后的图像
%%%%%%将Cut中的区域的个数放在Cut1中,查看区域生长之后区域的个数和分布
clear k
Cut1=zeros(m,n);
Cut2=zeros(m,n);
Cut2=im2bw(Cut2);
k=1;
a=min(min(Cut));
b=max(max(Cut));
a=double(a);
b=double(b);
for i=a:b
II=find(Cut==i);
if length(II)~=0
Cut2(II)=1;
[L,num]=bwlabel(Cut2,8);
for j=1:num
III=find(L==j);
Cut1(III)=k;
k=k+1;
end
Cut2(II)=0;
end
end
a1=max(max(Cut1))
a2=min(min(Cut1))
%%%%%%%将Cut1中的区域进行合并,将合并之后的区域放在Z4中
a1=double(a1);
f=I;
f=double(f);
Z1=Cut1;
Z2=Z1;
Z3=Z1;
[m,n]=size(Z1);
for i=1:a1
[L,H]=find(Z1==i);
if length(L)<=200%若区域太小,则合并
j=1;
for i1=1:length(L)
if (L(i1)-1>0)&(H(i1)-1>0)
if Z1(L(i1)-1,H(i1)-1)~=i
zhan(1,j)=Z1(L(i1)-1,H(i1)-1);
j=j+1;
end
end
if L(i1)-1>0
if Z1(L(i1)-1,H(i1))~=i
zhan(1,j)=Z1(L(i1)-1,H(i1));
j=j+1;
end
end
if (L(i1)-1>0)&(H(i1)+1<=n)
if Z1(L(i1)-1,H(i1)+1)~=i
zhan(1,j)=Z1(L(i1)-1,H(i1)+1);
j=j+1;
end
end
if (H(i1)-1>0)
if Z1(L(i1),H(i1)-1)~=i
zhan(1,j)=Z1(L(i1),H(i1)-1);
j=j+1;
end
end
if (H(i1)+1<=n)
if Z1(L(i1),H(i1)+1)~=i
zhan(1,j)=Z1(L(i1),H(i1)+1);
j=j+1;
end
end
if (H(i1)+1<=n&L(i1)+1<=m)
if Z1(L(i1)+1,H(i1)+1)~=i
zhan(1,j)=Z1(L(i1)+1,H(i1)+1);
j=j+1;
end
end
if (L(i1)+1<=m)
if Z1(L(i1)+1,H(i1))~=i
zhan(1,j)=Z1(L(i1)+1,H(i1));
j=j+1;
end
end
if (L(i1)+1<=m&H(i1)-1>0)
if Z1(L(i1)+1,H(i1)-1)~=i
zhan(1,j)=Z1(L(i1)+1,H(i1)-1);
j=j+1;
end
end
end %%%%%%%%%%%%%%将和这些点相连的那些点放入矩阵zhan中
if ~isempty(zhan)
y(1,1)=zhan(1,1);
j1=2;
for i3=2:length(zhan)
for i4=1:length(y)
if zhan(1,i3)~=y(1,i4)
k=1;
break
else
k=0;
end
end
if k==1
y(1,j1)=zhan(1,i3);
j1=j1+1;
end
end
end
%%%%%%以上将zhan里面的元素不同的元素放在y矩阵中
%%%以下将当前区域合并到与之相邻的区域中,规则为平均灰度值最接近,合并后的矩阵为Z2;
if length(y)==1
for i5=1:length(L)
Z2(L(i5),H(i5))=y(1,1);
end
else
s1=0;
for i6=1:length(L)
s1=s1+f(L(i6),H(i6));%%%%为要处理的像素的总灰度值
end
s=0;
II=find(Z1==y(1,1));
for i7=1:length(II)
s=s+f(II(i7)); %%%%%为第一个总灰度值
end
cha=abs(s-s1);
log=y(1,1);
for i8=2:length(y)
II=find(Z1==y(1,i8));
s2=0;
for i9=1:length(II)
s2=s2+f(II(i9));
end
cha1=abs(s1-s2);
if cha1<cha
cha=cha1;
log=y(1,i8);
end
end
for i10=1:length(L)
Z2(L(i10),H(i10))=log;%%%%合成之后的为Z2矩阵
end
end
end
Z1=Z2;
zhan=[];
y=[];
k=0;
end
Z4=zeros(m,n);
k=1;
m1=min(min(Z1));
m2=max(max(Z1));
for i11=m1:m2
II=find(Z1==i11);
if ~isempty(II)
Z4(II)=k;
k=k+1;
end
end
max(max(Z4)) %%%%%分成的区域的个数
min(min(Z4))
%%%%a1为Cut中的区域个数,
BW2 = edge(Z4,'sobel',0.000000000000001);
figure(30),imshow(BW2);
Lrgb = label2rgb(Z4);
figure(33), imshow(Lrgb), title('Lrgb')
imwrite(Lrgb,'C:\Documents and Settings\Administrator\桌面\lena30_200.tif')
III=find(BW2==1);
I(III)=255;
figure(32),imshow(I)
imwrite(I,'C:\Documents and Settings\Administrator\桌面\lena_I.tif')
% im1=imread('C:\Documents and Settings\Administrator\桌面\2.tif');
im2=xiaos_ipolartrans(Z4, 256, 256, 128, 128,'log','valid');
figure(44),imshow(im2,[])
X44=mat2gray(im2)
imwrite(X44,'C:\Documents and Settings\Administrator\桌面\lena_huanyuan.tif')