【ML实验6】K-means(图像压缩)

实验代码获取 github repo
山东大学机器学习课程资源索引


实验目的

在这里插入图片描述

实验内容

【ML实验6】K-means(图像压缩)_第1张图片
code

[m, n, color] = size(A);
k = 16;     % 簇数
center = zeros(k, 3);

% 随机采样图像中16个pixel作为初始中心点
for i = 1 : k
    rp = randperm(m, 2);
    center(i, :) = A(rp(1), rp(2), :);
end

% 迭代
maxiter = 60;
label = zeros(m, n);
% K-means loss function
Loss = zeros(1, maxiter);
for i = 1 : maxiter
    for x = 1 : m
        for y = 1 : n   % 遍历所有pixel
            min_dis = 1e10;
            for j = 1 : k   % calculate its nearest mean
                dis = [A(x, y, 1) - center(j, 1), A(x, y, 2) - center(j, 2), A(x, y, 3) - center(j, 3)];
                % A(x, y, :): 1 * 1 * 3
                % center(k, :): 1 * 3
                dis = dis * dis';
                if min_dis > dis
                    min_dis = dis;
                    label(x, y) = j;
                end
            end
            Loss(i) = Loss(i) + min_dis;
        end
    end
    
    % Update the values of the means
    cluster_size = zeros(k, 1);
    new_center = zeros(k, 3);
    for x = 1 : m
        for y = 1 : n
            lb = label(x, y);
            cluster_size(lb) = cluster_size(lb) + 1;
            new_center(lb, :) = new_center(lb, :) + [A(x, y, 1), A(x, y, 2), A(x, y, 3)];
        end
    end
    for j = 1 : k
        if cluster_size(k) ~= 0
            center(k, :) = new_center(k, :) / cluster_size(k);
        end
    end
end

聚类对象是颜色,由(r,g,b)表达,共 25 6 3 256^3 2563种,距离是颜色在颜色空间中的距离,设颜色 c 1 = [ r 1 , g 1 , b 1 ] c_1=[r_1,g_1,b_1] c1=[r1,g1,b1] c 2 = [ r 2 , g 2 , b 2 ] c_2=[r_2,g_2,b_2] c2=[r2,g2,b2],两种颜色的距离为 ∣ ∣ c 1 − c 2 ∣ ∣ ||c_1-c_2|| ∣∣c1c2∣∣.

【ML实验6】K-means(图像压缩)_第2张图片
【ML实验6】K-means(图像压缩)_第3张图片
这里是在 Small Image 上运行 k-means 算法得到16个颜色中心,将其作为压缩图像的颜色(就是压缩后的图像仅包含这16种颜色),压缩 Small Image 以及 Large Image.
code

% Reassigning Colors to The Large Image
B = double(imread('bird_large.tiff'));
[M, N, ~] = size(B);
for x = 1 : M
    for y = 1 : N
        min_dis = 1e18; lb = -1;
        for j = 1 : k
            dis = [B(x, y, 1) - center(j, 1), B(x, y, 2) - center(j, 2), B(x, y, 3) - center(j, 3)];
            dis = dis * dis';
            if min_dis > dis
                min_dis = dis;
                lb = j;
            end
        end
        B(x, y, :) = [center(lb, 1), center(lb, 2), center(lb, 3)];
    end
end

【ML实验6】K-means(图像压缩)_第4张图片
【ML实验6】K-means(图像压缩)_第5张图片

你可能感兴趣的:(ML,kmeans,人工智能)