当前位置:首页 > 代码 > 正文

kmeansmatlab代码(kmeans代码实现)

admin 发布:2022-12-19 10:08 167


本篇文章给大家谈谈kmeansmatlab代码,以及kmeans代码实现对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

matlab 自带kmeans怎么用 求一个简单例子 急!!!

matlab 自带kmeans是k-均值聚类函数。例如:

rng default;

X = [randn(100,2)*0.75+ones(100,2);   randn(100,2)*0.5-ones(100,2)];

opts = statset('Display','final');

[idx,C,sumd,D] = kmeans(X,2,'Distance','cityblock','Replicates',5,'Options',opts)

matlab自带的kmeans代码可在哪看,它初始点选择使用参数sample,这个具体是怎么选择的,我怎么能找出来啊

以MATLAB  R2012b为例:

一、点击下图中红圈指示“Find Files”。

二、执行完步骤一,出现下图,先在红圈1中输入“kmeans”,再在红圈2中选择文件类型为“.m“,再在红圈3中选择搜索范围”Entire MATLAB path“,再点击红圈4中”Find“,然后就会出现红圈5中的”kmeans.m“,这时双击”kmeans.m“即可打开kmeans函数的源代码。

三、对于其他版本的MATLAB,查找kmeans函数的源代码则大同小异了,而kmeans函数的使用方法,可在MATLAB的help中找到。至于如何选择初始点,好像有随机选择k个点法、选择批次距离尽可能远的k个点等等方法,具体还要查阅相关资料。

用MATLAB 实现k-means算法数据 data随机产生100个数。 分成两类,即k=2.代码

%随机获取100个点

X =[randn(100,2)+ones(100,2);randn(100,2)+[ones(100,1),-ones(100,1)]];

opts =statset('Display','final');

%调用Kmeans函数

%X N*P的数据矩阵

%Idx N*1的向量,存储的是每个点的聚类标号

%Ctrs K*P的矩阵,存储的是K个聚类质心位置

%SumD 1*K的和向量,存储的是类间所有点与该类质心点距离之和

%D N*K的矩阵,存储的是每个点与所有质心的距离;

[Idx,Ctrs,SumD,D] =kmeans(X,2,'Replicates',2,'Options',opts);

%画出聚类为1的点。X(Idx==1,1),为第一类的样本的第一个坐标;X(Idx==1,2)为第二类的样本的第二个坐标

plot(X(Idx==1,1),X(Idx==1,2),'r.','MarkerSize',14)

hold on

plot(X(Idx==2,1),X(Idx==2,2),'b.','MarkerSize',14)

%绘出聚类中心点,kx表示是圆形

plot(Ctrs(:,1),Ctrs(:,2),'kx','MarkerSize',14,'LineWidth',4)

plot(Ctrs(:,1),Ctrs(:,2),'kx','MarkerSize',14,'LineWidth',4)

plot(Ctrs(:,1),Ctrs(:,2),'kx','MarkerSize',14,'LineWidth',4)

legend('Cluster 1','Cluster2','Center 1','Center 2','Location','NW')

Ctrs

SumD

怎样用matlab实现多维K-means聚类算法

直接用kmeans函数。。。

idx = kmeans(X,k)

idx = kmeans(X,k,Name,Value)

[idx,C] = kmeans(___)

[idx,C,sumd] = kmeans(___)

[idx,C,sumd,D] = kmeans(___)

idx = kmeans(X,k) performs k-means clustering to partition the observations of the n-by-p data matrix X into k clusters, and returns an n-by-1 vector (idx) containing cluster indices of each observation. Rows of X correspond to points and columns correspond to variables.

By default, kmeans uses the squared Euclidean distance measure and the k-means++ algorithm for cluster center initialization.

example

idx = kmeans(X,k,Name,Value) returns the cluster indices with additional options specified by one or more Name,Value pair arguments.

For example, specify the cosine distance, the number of times to repeat the clustering using new initial values, or to use parallel computing.

example

[idx,C] = kmeans(___) returns the k cluster centroid locations in the k-by-p matrix C.

example

[idx,C,sumd] = kmeans(___) returns the within-cluster sums of point-to-centroid distances in the k-by-1 vector sumd.

example

[idx,C,sumd,D] = kmeans(___) returns distances from each point to every centroid in the n-by-k matrix D.

基于k-means的图像分割MATLAB程序

close all;

clear;

I_rgb = imread('color-cam4-f0.bmp'); %读取文件数据

figure(1);

subplot(1,2,1);

imshow(I_rgb); %显示原图

title('原始图像');

%将彩色图像从RGB转化到lab彩色空间

C = makecform('srgb2lab'); %设置转换格式

I_lab = applycform(I_rgb, C);

%进行K-mean聚类将图像分割成3个区域

ab = double(I_lab(:,:,2:3)); %取出lab空间的a分量和b分量

nrows = size(ab,1);

ncols = size(ab,2);

ab = reshape(ab,nrows*ncols,2);

nColors = 4; %分割的区域个数为

[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',100); %重复聚类3次

pixel_labels = reshape(cluster_idx,nrows,ncols);

figure(1);

subplot(1,2,2);

imshow(pixel_labels,[]), title('聚类结果');

%显示分割后的各个区域

segmented_images = cell(1,nColors);

rgb_label = repmat(pixel_labels,[1 1 3]);

for k = 1:nColors

color = I_rgb;

color(rgb_label ~= k) = 0;

segmented_images{k} = color;

end

for i=1:nColors

figure(2),subplot(1,nColors,i);imshow(segmented_images{i}), title('分割结果');

end

matlab中kmeans算法程序如下 我要做图像分类 主程序改怎么写那?知道的写下 谢谢了

function [mu,mask]=kmeans(ima,k)%k为指定类别数

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

% 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)

kmeansmatlab代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于kmeans代码实现、kmeansmatlab代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/6444.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载