Plot Gaussian Mixture Model with MATLAB

49 Views Asked by At

I have a Gaussian Mixture Model (GMM) from image data

ans =

                I: 1
                K: 5
                D: 2
             Rmin: 2.5000e-04
          cluster: [1x5 struct]
          numPnts: 165032
         numSPnts: 464305
    loglikelilood: -4.2638e+06
          epsilon: 0.8245
               Lc: 6

This is cluster: cluster description

How can I plot it? This is the plotGMM function:

function plotGMM(mixture)

    % 提取GMM参数
    mus = cell2mat({mixture.cluster.mu})';
    sigmas = {mixture.cluster.R};
    pis = cell2mat({mixture.cluster.pi})';
    
    % 生成坐标网格
    [X, Y] = meshgrid(-5:0.1:5, -5:0.1:5);
    [m,n] = size(X);
    
    % 计算GMM概率分布
    K = length(mus);
    Z = zeros(size(X));
    
    for k = 1:K
        MU = mus(k, :)';
        sigma_k = sigmas{k}(1:n, 1:n);
        gauss_k = mvnpdf(X, MU, sigma_k);
        Z = Z + pis(k) * gauss_k;
    end
    
    % 绘制等高线
    contour(X, Y, Z, 'ShowText', 'on');
    
    % 设置图像参数
    colormap('jet');
    xlabel('x'); ylabel('y');
    title('GMM Contours');
    
    % 设置轴范围
    axis square;
    xlim([-5 5]);
    ylim([-5 5]);

end

Run it in MATLAB, X and MU SIGMA is not correct.

0

There are 0 best solutions below