clear;
fid1=fopen('d:/char1.txt','rt');
b=fscanf(fid1,'%d');
sta=fclose(fid1);
fid1=fopen('d:/char2.txt','r');
g=fscanf(fid1,'%s');
stb=fclose(fid1);
%file=[c,'01.bmp'];
%I=imread(file);
s={};%m=1;n=9;
%if n~=m
for i=1
fi=[g,'0' num2str(i) '.bmp'];
s{i}=imread(fi);
% t{i}=ind2rgb(s{i},map);
% s{i}=imread(['c\0' num2str(i) '.bmp']);
% m=m+1;
% else n=n+9;
end
%end
%end
%figure(1),imshow(s{1});
%figure(2),imshow(s{2});figure(3),imshow(s{3});figure(4),imshow(s{4});figure(5),imshow(s{5});figure(6),imshow(s{6});
PicData=[s{1}]; % 横向拼接
imwrite(PicData, 'PicOut.jpg'); % 输出图形
%, 'Quality', 75
%I=PicOut;
I=imread('PicOut.jpg');
figure,imshow(I);
switch b(1)
case 1
%function OstuMethod
%最大类间方差法求阈值并二值化(大津法)
%clear all
threshold = graythresh(I);
a=im2bw(I,threshold);
figure;
imshow(a);
title('最大类间方差法处理');
case 2
%function Max_Shang
%最大熵法求阈值并二值化
%clear all
%s=imread('1_副本.bmp');
% figure,imshow(I)
a=rgb2gray(I);
count=imhist(a);
[m,n]=size(a);
N=m*n;
L=256;
count=count/N;%%每一个像素的分布概率
count
for i=1:L
if count(i)~=0
st=i-1;
break;
end
end
st
for i=L:-1:1
if count(i)~=0
nd=i-1;
break;
end
end
nd
f=count(st+1:nd+1); %f是每个灰度出现的概率
size(f)
E=[];
for Th=st:nd-1 %%%设定初始分割阈值为Th
av1=0;
av2=0;
Pth=sum(count(1:Th+1));
%%%第一类的平均相对熵为
for i=0:Th
av1=av1-count(i+1)/Pth*log(count(i+1)/Pth+0.00001);
end
%%%第二类的平均相对熵为
for i=Th+1:L-1
av2=av2-count(i+1)/(1-Pth)*log(count(i+1)/(1-Pth)+0.00001);
end
E(Th-st+1)=av1+av2;
end
position=find(E==(max(E)));
th=st+position-1
for i=1:m
for j=1:n
if a(i,j)>th
a(i,j)=255;
else
a(i,j)=0;
end
end
end
a=255-a;
figure,imshow(a);
title('最大熵法处理');
case 3
%function Decide
%人工二值化处理
%I=imread('1_副本.bmp');
% figure(1); imshow(I);%显示
% a= rgb2gray(I);%转化为灰度图像
a=I;
%figure(2);
%imshow(I);
J=find(a<b(2));
a(J)=0;
J=find(a>=b(2));
a(J)=255;
a=255-a;
figure;
imshow(a);
title('图像作人工二值化处理( 域值自定 )');
otherwise,end
%se=strel('disk',15);
%BW=imclose(BW,se);
%figure(2);
%imshow(BW);
%BW=imfill(BW,[1 1]);
%figure(2);
%imshow(BW);
a=imfill(a,'holes');%填充
%%%极小颗粒及超大面积暗影去除
L = bwlabel(a);
mianj=regionprops(L,'Area');%颗粒面积大小
allArea = [mianj.Area];
x = ismember(L, find(8<=[mianj.Area]));
L = bwlabel(x);
mianj=regionprops(L,'Area');%颗粒面积大小
allArea = [mianj.Area];
x = ismember(L, find([mianj.Area]<=500));
L = bwlabel(x); %颗粒标记
%边缘不完整颗粒去除
D=zeros(size(L));
D([1,end],:)=L([1,end],:);
D(:,[1,end])=L(:,[1,end]);
[i,j]=find(D~=0);
[m,n]=size(i);
C=zeros(1,m);
for y=1:m
C(y)=D(i(y),j(y));
end
for y=1:m
L(find(L==C(y)))=0;
end
figure,imshow(L);
%L = bwlabel(L);
%A=watershed(L);
%figure,imshow(A);
%加点东西,如果可以的话就要把边沿颗粒去除后的图像显示出来????????????
%T=L;
%T(find(T~=0))=1;
%imshow(T);
duanz=regionprops(L,'MinorAxisLength');%近似为椭圆模型的短轴长
allMinorAxisLength = [duanz.MinorAxisLength];
%hist(allMinorAxisLength);grid on;
x=allMinorAxisLength>2;
allMinorAxisLength=allMinorAxisLength(x);
changz=regionprops(L,'MajorAxisLength');%近似为椭圆模型的长轴长
allMajorAxisLength = [changz.MajorAxisLength];
mianj=regionprops(L,'Area');%颗粒面积大小
allArea = [mianj.Area];
%allArea
fangx=regionprops(L,'Orientation');%与X轴的夹角,即运动方向
allOrientation = [fangx.Orientation];
z=sqrt(allArea/pi)*2.0;
if length(z)==0
return;
end
%d=d(find(d>5));% 对很小的粒子忽略
%set(handles.edit1,'string',num2str(mean(z))); %平均直径
%set(handles.edit2,'string',num2str(length(z)));%颗粒个数
%stateMax=regionprops(L,'MajorAxisLength');
%Maxd=[stateMax.MajorAxisLength];
%set(handles.edit6,'string',num2str(mean(Maxd)));%最大粒径
%stateMin=regionprops(L,'MinorAxisLength');
%Mind=[stateMin.MinorAxisLength];
%set(handles.edit7,'string',num2str(mean(Mind)));%最小粒径
%stateCArea=regionprops(L,'ConvexArea');
%ConvexArea=[stateCArea.ConvexArea];
%set(handles.edit8,'string',num2str(mean(ConvexArea)));
%stateEDia=regionprops(L,'EquivDiameter');
%EquivDiameter=[stateEDia.EquivDiameter];
%set(handles.edit9,'string',num2str(mean(EquivDiameter)));
%stateFArea=regionprops(L,'FilledArea');
%FilledArea=[stateFArea.FilledArea];
%set(handles.edit10,'string',num2str(mean(FilledArea)));
%axes(handles.axes1) % Select the proper axes
%cla;
%imshow(L);
%axes(handles.axes2) % Select the proper axes
%cla;
%hist(z); grid on;
%xlabel('不同象素颗粒');
%ylabel('分布 ');
%AllResult.size=mean(z);
AllResult.num=length(z);
%AllResult.z=z;
%AllResult.Maxd=Maxd;
%AllResult.Mind=Mind;
%AllResult.ConvexArea=ConvexArea;
%AllResult.EquivDiameter=EquivDiameter;
%AllResult.FilledArea=FilledArea;
% % % i=get(popupmenu3,'string');
% % % x1=AllResult.d(i);
allArea(find(allArea==0))=[];%
allOrientation(find(allOrientation==0))=[];%
allMajorAxisLength(find(allMajorAxisLength==0))=[];%
allMinorAxisLength(find(allMinorAxisLength==0))=[];%
fid1=fopen('d:/char3.txt','rt');
d=fscanf(fid1,'%s');
file1=[d,'mji.txt'];
file2=[d,'fxiang.txt'];
file3=[d,'czhou.txt'];
file4=[d,'dzhou.txt'];
mji=fopen(file1,'w');
%fprintf(fid,'%f\t%f\n',AllResult.num,AllResult.size);
for j=1:length(allArea)
fprintf(mji,'%.1f\t%.3f\n',allArea(j));
end
fclose(mji);
fxiang=fopen(file2,'w');
%fprintf(fid,'%f\t%f\n',AllResult.num,AllResult.size);
for j=1:length(allOrientation)
fprintf(fxiang,'%.1f\t%.3f\n',allOrientation(j));
end
fclose(fxiang);
czhou=fopen(file3,'w');
%fprintf(fid,'%f\t%f\n',AllResult.num,AllResult.size);
for j=1:length(allMajorAxisLength)
fprintf(czhou,'%.1f\t%.3f\n',allMajorAxisLength(j));
end
fclose(czhou);
dzhou=fopen(file4,'w');
%fprintf(fid,'%f\t%f\n',AllResult.num,AllResult.size);
for j=1:length(allMinorAxisLength)
fprintf(dzhou,'%.3f\t%.3f\n',allMinorAxisLength(j));
end
fclose(dzhou);