Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 基于高斯混合模型的图像聚类_Matlab_Machine Learning_Classification_Cluster Analysis_Mixture Model - Fatal编程技术网

Matlab 基于高斯混合模型的图像聚类

Matlab 基于高斯混合模型的图像聚类,matlab,machine-learning,classification,cluster-analysis,mixture-model,Matlab,Machine Learning,Classification,Cluster Analysis,Mixture Model,我想使用GMM(高斯混合模型)对二值图像进行聚类,还想在二值图像本身上绘制聚类质心。 我将此作为我的参考: 这是我的初始代码 I=im2double(imread('sil10001.pbm')); K = I(:); mu=mean(K); sigma=std(K); P=normpdf(K, mu, sigma); Z = norminv(P,mu,sigma); X = mvnrnd(mu,sigma,1110); X=reshape(X,111,10);

我想使用GMM(高斯混合模型)对二值图像进行聚类,还想在二值图像本身上绘制聚类质心。

我将此作为我的参考:

这是我的初始代码

 I=im2double(imread('sil10001.pbm'));
  K = I(:);
  mu=mean(K);
  sigma=std(K);
  P=normpdf(K, mu, sigma);
   Z = norminv(P,mu,sigma);
  X = mvnrnd(mu,sigma,1110);
  X=reshape(X,111,10);


 scatter(X(:,1),X(:,2),10,'ko');

options = statset('Display','final');
gm = fitgmdist(X,2,'Options',options);



idx = cluster(gm,X);
cluster1 = (idx == 1);
cluster2 = (idx == 2);


 scatter(X(cluster1,1),X(cluster1,2),10,'r+');
 hold on

  scatter(X(cluster2,1),X(cluster2,2),10,'bo');
  hold off
  legend('Cluster 1','Cluster 2','Location','NW')


  P = posterior(gm,X);

 scatter(X(cluster1,1),X(cluster1,2),10,P(cluster1,1),'+')
 hold on
 scatter(X(cluster2,1),X(cluster2,2),10,P(cluster2,1),'o')
 hold off
 legend('Cluster 1','Cluster 2','Location','NW')
 clrmap = jet(80); colormap(clrmap(9:72,:))
 ylabel(colorbar,'Component 1 Posterior Probability')
但问题是,我无法在主二值图像中绘制从GMM接收到的簇质心。我如何做到这一点?

**现在假设我在一个序列中有10个这样的图像,我想把它们的平均位置信息存储在两个单元格数组中,那么我该怎么做呢

    images=load('gait2go.mat');%load the matrix file
    for i=1:10

   I{i}=images.result{i};
  I{i}=im2double(I{i});

   %determine 'white' pixels, size of image can be [M N], [M N 3] or [M N 4]
  Idims=size(I{i});
  whites=true(Idims(1),Idims(2));

    df=I{i};
      %we add up the various color channels
 for colori=1:size(df,3)
  whites=whites & df(:,:,colori)>0.5;
 end

%choose indices of 'white' pixels as coordinates of data
[datax datay]=find(whites);

%cluster data into 10 clumps
  K = 10;               % number of mixtures/clusters
  cInd = kmeans([datax datay], K, 'EmptyAction','singleton',...
   'maxiter',1000,'start','cluster');

%get clusterwise means
 meanx=zeros(K,1);
 meany=zeros(K,1);  
  for i=1:K
   meanx(i)=mean(datax(cInd==i));
   meany(i)=mean(datay(cInd==i));

 end

 xc{i}=meanx(i);%cell array contaning the position of the mean for the 10    
 images
  xb{i}=meany(i);

figure;
gscatter(datay,-datax,cInd); %funky coordinates for plotting according to      
 image
 axis equal;
  hold on;
  scatter(meany,-meanx,20,'+'); %same funky coordinates


 end

我能得到10张分割的图像,但没有存储在单元格数组xc和xb中的平均值。它们只存储[]而不是均值的值。我相信你一定在绘图中犯了一个天真的错误,这就是为什么你只看到一条直线:你只绘制了
x


在我看来,scatter命令中的第二个参数应该是
X(cluster1,2)
X(cluster2,2)
取决于代码中使用的是哪个
scatter
命令。

我相信您一定在绘图中犯了一个天真的错误,这就是为什么您只看到一条直线:您只绘制了
x


在我看来,scatter命令中的第二个参数应该是
X(cluster1,2)
X(cluster2,2)
,这取决于代码中使用的
scatter
命令。

我决定对您的问题发布一个答案(您的问题由最大似然猜测确定:p),但我写了一篇详尽的介绍。请仔细阅读,因为我认为您很难理解想要使用的方法,也很难理解为什么其他人不能帮助您使用通常的提问方法。您的问题有几个问题,包括代码相关的问题和概念性问题。让我们从后者

问题与问题 你说你想用高斯混合模型对你的图像进行聚类。虽然我对聚类不太熟悉,但在浏览了你的和(以及快速入门)之后,我认为你完全走错了方向

顾名思义,高斯混合建模是用高斯(即正态)分布的混合对数据集进行建模。这种方法流行的原因是,当你对各种数量进行测量时,在许多情况下,你会发现你的数据主要是以正态分布的形式分布的(这实际上是它被称为正常的原因)。这背后的原因是,这意味着在许多情况下,合理独立的随机变量之和趋于正常

另一方面,聚类仅仅意味着根据一些标准将数据集分成不相交的小簇。主要标准通常是(某种)距离,因此您希望找到“紧密的数据块”在更大的数据集中。在执行GMM之前,通常需要对数据进行聚类,因为在不猜测聚类的情况下找到数据背后的高斯分布已经很难了。我不太熟悉所涉及的过程,无法判断GMM算法在处理原始数据时的效果(但我希望许多实现都是从集群步骤开始的)

更接近你的问题:我猜你想做一些图像识别。看图片,你想得到更多的强相关肿块。这是聚类。如果你看一张动物园的图片,你会看到,比如说,一头大象和一条蛇。它们都有各自不同的形状,并且彼此之间很好地分开。如果你喜欢聚类改变你的形象(蛇不是骑在大象上的,),你会发现两块:一块大象形状,一块蛇形状。现在,在这些数据集上使用GMM是没有意义的:大象,尤其是蛇,形状不像多元高斯分布。但是如果你只想知道不同的动物在你的象形图中的位置,你一开始就不需要这个re

仍然使用这个示例,您应该确保将数据分为适当数量的子集。如果您尝试将动物园图片分为3个簇,您可能会得到第二条假蛇:大象的鼻子。随着簇数量的增加,您的分区可能会越来越没有意义

你的方法 你的代码没有给你任何合理的东西,这有一个很好的理由:它从一开始就没有意义。看看开头:

I=im2double(imread('sil10001.pbm'));
K = I(:);
mu=mean(K);
sigma=std(K);
X = mvnrnd(mu,sigma,1110);
X=reshape(X,111,10);
你读取你的二值图像,将其转换为双精度,然后将其拉伸成一个向量,并计算该向量的平均值和偏差。你基本上将你的初始图像涂抹成两个值:平均强度和偏差。然后你用这些参数生成
111*10
标准法线点,并尝试进行G在前两组111上,它们都是独立的正态分布,具有相同的参数。所以你可能会得到两个重叠的高斯分布,在相同的平均值附近,具有相同的偏差

我认为你在网上找到的例子让你感到困惑。当你做GMM时,你已经有了你的数据,所以不应该涉及伪正态数。但是当人们发布例子时,他们也会尝试提供可复制的输入(好吧,有些人会,轻推轻推眨眼)。一种简单的方法是生成简单高斯数的并集,然后将其输入GMM

因此,我的观点是,你不必生成随机数,而必须使用图像数据本身作为过程的输入。你可能只想对图像进行聚类,而不是实际使用GMM在聚类上画土豆,因为你想在关于人类的图像中对身体部位进行聚类。大多数身体部位都是而不是%set infile to what your image file will be infile='sil10001.pbm'; %read file I=im2double(imread(infile)); %determine 'white' pixels, size of image can be [M N], [M N 3] or [M N 4] Idims=size(I); whites=true(Idims(1),Idims(2)); %we add up the various color channels for colori=1:Idims(3) whites=whites & I(:,:,colori)>0.5; end %choose indices of 'white' pixels as coordinates of data [datax datay]=find(whites); %cluster data into 10 clumps K = 10; % number of mixtures/clusters cInd = kmeans([datax datay], K, 'EmptyAction','singleton',... 'maxiter',1000,'start','cluster'); %get clusterwise means meanx=zeros(K,1); meany=zeros(K,1); for i=1:K meanx(i)=mean(datax(cInd==i)); meany(i)=mean(datay(cInd==i)); end figure; gscatter(datay,-datax,cInd); %funky coordinates for plotting according to image axis equal; hold on; scatter(meany,-meanx,20,'ko'); %same funky coordinates
%read file

I=im2double(imread('sil10340.pbm'));
%choose indices of 'white' pixels as coordinates of data
[datax datay]=find(I);
%cluster data into 10 clumps
 K = 10;               % number of mixtures/clusters
[cInd, c] = kmeans([datax datay], K, 'EmptyAction','singleton',...
'maxiter',1000,'start','cluster');
 figure;
gscatter(datay,-datax,cInd); %funky coordinates for plotting according to    
image
axis equal;
hold on;
 scatter(c(:,2),-c(:,1),20,'ko'); %same funky coordinates