matlab中kmeans算法程序如下 我要做图像分类 主程序改怎么写那?知道的写下 谢谢了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab中kmeans算法程序如下 我要做图像分类 主程序改怎么写那?知道的写下 谢谢了相关的知识,希望对你有一定的参考价值。
function [cid,nr,centers] = cskmeans(x,k,nc)
% CSKMEANS K-Means clustering - general method.
%
warning off
[n,d] = size(x);
if nargin < 3
% Then pick some observations to be the cluster centers.
ind = ceil(n*rand(1,k));
% We will add some noise to make it interesting.
nc = x(ind,:) + randn(k,d);
end
% set up storage
% integer 1,...,k indicating cluster membership
cid = zeros(1,n);
% Make this different to get the loop started.
oldcid = ones(1,n);
% The number in each cluster.
nr = zeros(1,k);
% Set up maximum number of iterations.
maxiter = 100;
iter = 1;
while ~isequal(cid,oldcid) & iter < maxiter
% Implement the hmeans algorithm
% For each point, find the distance to all cluster centers
for i = 1:n
dist = sum((repmat(x(i,:),k,1)-nc).^2,2);
[m,ind] = min(dist); % assign it to this cluster center
cid(i) = ind;
end
% Find the new cluster centers
for i = 1:k
% find all points in this cluster
ind = find(cid==i);
% find the centroid
nc(i,:) = mean(x(ind,:));
% Find the number in each cluster;
nr(i) = length(ind);
end
iter = iter + 1;
end
% Now check each observation to see if the error can be minimized some more.
% Loop through all points.
maxiter = 2;
iter = 1;
move = 1;
while iter < maxiter & move ~= 0
move = 0;
% Loop through all points.
for i = 1:n
% find the distance to all cluster centers
dist = sum((repmat(x(i,:),k,1)-nc).^2,2);
r = cid(i); % This is the cluster id for x
%%nr,nr+1;
dadj = nr./(nr+1).*dist'; % All adjusted distances
[m,ind] = min(dadj); % minimum should be the cluster it belongs to
if ind ~= r % if not, then move x
cid(i) = ind;
ic = find(cid == ind);
nc(ind,:) = mean(x(ic,:));
move = 1;
end
end
iter = iter+1;
end
centers = nc;
if move == 0
disp('No points were moved after the initial clustering procedure.')
else
disp('Some points were moved after the initial clustering procedure.')
end
warning on
咱能靠谱点不?这回答的是些什么啊
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% kmeans image segmentation
%
% Input:
% ima: grey color image灰度图像
% k: Number of classes指定的图像中类别数目
% Output:
% mu: vector of class means 每个类的均值
% mask: clasification image mask分类后的图像掩膜(mask)
%
% Author: Jose Vicente Manjon Herrera
% Email: jmanjon@fis.upv.es
% Date: 27-08-2005
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check image
ima=double(ima);
copy=ima; % make a copy
ima=ima(:); % vectorize ima将图像向量化,即一维化。
mi=min(ima); % deal with negative
ima=ima-mi+1; % and zero values
s=length(ima);%获得图像像素个数
% create image histogram%创建图像直方图
m=max(ima)+1;%最大像素值加1
h=zeros(1,m);%直方图,有m个bin
hc=zeros(1,m);%标号矩阵,每个像素点的值为该点所隶属的类别号
for i=1:s%s是图像象素个数,即考查每个像素
if(ima(i)>0) h(ima(i))=h(ima(i))+1;end;%直方图中对应bin加1
end
ind=find(h);%找到直方图中不为零的那些bin的序号。
hl=length(ind);%直方图中非零bin的个数
% initiate centroids
mu=(1:k)*m/(k+1);%k为指定的类别数,mu为不同类的分割点,相当于坐标轴上的整点
% start process
while(true)
oldmu=mu;
% current classification
for i=1:hl
c=abs(ind(i)-mu);%就是相当于考察ind(i)在坐标轴上离哪个整点最近!注意mu总共就k个
cc=find(c==min(c));%cc保留距离ind(i)最近整点的序号,序号为1、2、3...k
hc(ind(i))=cc(1);
end
%recalculation of means 下面的程序用于计算每一类的均值位置
for i=1:k,
a=find(hc==i);
mu(i)=sum(a.*h(a))/sum(h(a));%h为直方图
end
if(mu==oldmu) break;end;%循环结束条件
end
% calculate mask
s=size(copy);
mask=zeros(s);
mask1=mask;%增加一个显示矩阵
size(mask1)
for i=1:s(1),
for j=1:s(2),
c=abs(copy(i,j)-mu);
a=find(c==min(c));
mask(i,j)=a(1);
end
end
mu=mu+mi-1; % recover real range
for i = 1 : k
p=find(mask==i);
mask1(p)=1/k*i;
end
figure,imshow(mask1)追问
程序有错 34行,,,,,,
追答for i=1:s%s是图像象素个数,即考查每个像素
if(ima(i)>0)
h(ima(i))=h(ima(i))+1;
end;%直方图中对应bin加1
end
%%%%%%%%%%%%%34行写成这样的形式即可!
x = [1,6,9,13,2,8,7,4,11,5,3,10,12];
numGroups = 4; % 组的数目
xMax = max(x);
xMin = min(x);
boundries = xMin + (0:numGroups) * (xMax - xMin) / (numGroups - 1); % 组的边界
xGroup = zeros(size(x)); % 初始化
for group = 1:numGroups
loc = (x >= boundries(group)) & (x <= boundries(group + 1)); %在这个组的书的坐标
xGroup(loc) = group;
end
结果存在xGroup里
补充:
如果要按照你的那样输出,可以改成这样:
x = [1,6,9,13,2,8,7,4,11,5,3,10,12];
GroupName = ['A','B','C','D'];
numGroups = length(GroupName); % 组的数目
xMax = max(x);
xMin = min(x);
boundries = xMin + (0:numGroups) * (xMax - xMin) / (numGroups - 1); % 组的边界
xGroup = zeros(size(x)); % 初始化
for group = 1:numGroups
loc = (x >= boundries(group)) & (x <= boundries(group + 1)); %在这个组的书的坐标
xGroup(loc) = group;
end
xGroupName = GroupName(xGroup);
for ii = 1:length(x)
fprintf('%d : %s\n', x(ii), xGroupName(ii));
end
matlab如何求kmean聚类中心点的坐标和各个中心包含的样本点数,要用到那些命令
[idx,c]=kmeans(X,k)
其中k是聚类中心个数
X是你存储需要处理的坐标的矩阵
c是一个存储了聚类中心点坐标的矩阵
MATLAB 是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。
MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室)。是由美国mathworks公司发布的主要面对科学计算、可视化以及交互式程序设计的高科技计算环境。
MATLAB和Mathematica、Maple并称为三大数学软件。
MATLAB的基本数据单位是矩阵,它的指令表达式与数学、工程中常用的形式十分相似,故用MATLAB来解算问题要比用C,FORTRAN等语言完成相同的事情简捷得多,并且MATLAB也吸收了像Maple等软件的优点,使MATLAB成为一个强大的数学软件。
在新的版本中也加入了对C,FORTRAN,C++,JAVA的支持。
参考技术A [idx,c]=kmeans(X,k)其中k是聚类中心个数
X是你存储需要处理的坐标的矩阵
c是一个存储了聚类中心点坐标的矩阵
以上是关于matlab中kmeans算法程序如下 我要做图像分类 主程序改怎么写那?知道的写下 谢谢了的主要内容,如果未能解决你的问题,请参考以下文章
图像分割基于matlab麻雀算法优化Kmeans图像分割含Matlab源码 2030期
图像分割基于matlab麻雀算法优化Kmeans图像分割含Matlab源码 2030期
图像分割基于matlab Kmean聚类分水岭oust粒子群算法优化脂肪肝图像分割含Matlab源码 2277期