最大似然分类器
判断每个数据属于各个选取样本的概率,比较概率大小,最大者就归并到这类
简单记录代码:
function re=maxlike()
[filename,pathname]=uigetfile({'*.jpg;*.bmp;*.tif;*.png;*.gif','All Image Files';'*.*','All Files'});%动态打开文件
image = imread([pathname,filename]);%读取
figure(1);imshow(image);
image=double(image);
[m,n,bands]=size(image);
k=input('请输入要分类的个数: ');%输入要选取的样本类别数
aver=zeros(k,bands);%申请矩阵空间存储样本数据各类别各波段均值
thi=zeros(k,1);%每个样本类别方差
%对所选取样本数据进行处理求得各类别均值
for i=1:k%对选中类别循环
str=['请在屏幕图像上选择第',num2str(i),'种分类样本,选择完毕请回车确定'];
disp(str);
[y,x]=getpts;%从图像上获取数据点
A=[round(y),round(x)];
for band=1:bands%对图像每个波段进行计算,求取指定类别每个波段均值
Sum=[];
temp=image(:,:,band);
for count=1:size(A,1)%对取得点循环
Sum=[Sum;temp(A(count,2),A(count,1))];
end
aver(i,band)=mean(Sum);%求均值
thi(i,band)=cov(Sum);
end
end
image=reshape(image,m*n,bands);%将图像数据重塑成为一列为一个波段数据的形式,便于计算
p=zeros(k,m*n);%申请矩阵空间存储每个类别图像各个点数据分别到各类的概率
for i=1:m*n
for t=1:k
for band=1:bands
p(t,i)=p(t,i)+exp(-(image(i,band)-aver(t,band))^2/(2*thi(t,band)))/sqrt(2*pi*thi(t,band));
end
end
end
[~,index]=max(p);
index=reshape(index,m,n);
re=index/k;
figure(2),imshow((re));
end
具体操作和参数输入都从命令行输入:
运行结果: