使用K均值聚类(使用RGB特征)对图像进行颜色量化 [英] Color quantization of an image using K-means clustering (using RGB features)

查看:205
本文介绍了使用K均值聚类(使用RGB特征)对图像进行颜色量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用matlab对图像的 RGB +空间要素进行聚类?



注意:我想使用 kmeans 进行群集。



事实上,基本上我想做一件事,我想得到这个图像





来自此



解决方案

我想你正在寻找颜色量化。

  [imgQ,map] = rgb2ind(img,4,'nodither'); %将此4改为量化图像中所需颜色的数量

imshow(imgQ,map);

结果:





使用 kmeans

 %img是原始图片

imgVec = [reshape(img( :,:,1),[],1)重塑(img(:,,2),[],1)重塑(img(:,:3),[],1)];
[imgVecQ,imgVecC] = kmeans(double(imgVec),4); %4种颜色
imgVecQK = pdist2(imgVec,imgVecC); %选择每个像素最近的质心,
[〜,indMin] = min(imgVecQK,[],2); %避免双循环

imgVecNewQ = imgVecC(indMin,:); %量化
imgNewQ = img;
imgNewQ(:,:,1)=重塑(imgVecNewQ(:,1),size(img(:,:,1))); %安排回图像
imgNewQ(:,:,2)=重塑(imgVecNewQ(:,2),size(img(:,:,1)));
imgNewQ(:,:,3)=重塑(imgVecNewQ(:,3),size(img(:,:,1)));

imshow(img)
figure,imshow(imgNewQ,[]);

结果 kmeans



<的量化图像/ p>

如果要将距离约束添加到 kmeans ,代码会略有不同。基本上,您还需要连接相应像素值的像素坐标。但请记住,在为每个像素指定最近的质心时,只分配颜色,即前3个维度,而不是最后2个维度。显然,这没有意义。代码与之前的代码非常相似,请注意更改并理解它们。

  [col,row] = meshgrid(1 :尺寸(IMG,2),1:尺寸(IMG,1)); 
imgVec = [reshape(img(:,:,1),[],1)重塑(img(:,:,2),[],1)重塑(img(:,:,3), [],1)row(:) col(:)];
[imgVecQ,imgVecC] = kmeans(double(imgVec),4); %4种颜色
imgVecQK = pdist2(imgVec(:,1:3),imgVecC(:,1:3));

[〜,indMin] = min(imgVecQK,[],2);
imgVecNewQ = imgVecC(indMin,1:3); %量化
imgNewQ = img;
imgNewQ(:,:,1)=重塑(imgVecNewQ(:,1),size(img(:,:,1))); %安排回图像
imgNewQ(:,:,2)=重塑(imgVecNewQ(:,2),size(img(:,:,1)));
imgNewQ(:,:,3)=重塑(imgVecNewQ(:,3),size(img(:,:,1)));

imshow(img)
figure,imshow(imgNewQ,[]);

带距离限制的 kmeans 的结果:




Is it possible to clustering for RGB + spatial features of images with matlab?

NOTE: I want to use kmeans for clustering.

In fact basicly i want to do one thing, i want to get this image

from this

解决方案

I think you are looking for color quantization.

[imgQ,map]= rgb2ind(img,4,'nodither'); %change this 4 to the number of desired colors
                                       %in quantized image
imshow(imgQ,map);

Result:

Using kmeans :

%img is the original image

imgVec=[reshape(img(:,:,1),[],1) reshape(img(:,:,2),[],1) reshape(img(:,:,3),[],1)];
[imgVecQ,imgVecC]=kmeans(double(imgVec),4); %4 colors
imgVecQK=pdist2(imgVec,imgVecC); %choosing the closest centroid to each pixel, 
[~,indMin]=min(imgVecQK,[],2);   %avoiding double for loop

imgVecNewQ=imgVecC(indMin,:);  %quantizing
imgNewQ=img;
imgNewQ(:,:,1)=reshape(imgVecNewQ(:,1),size(img(:,:,1))); %arranging back into image
imgNewQ(:,:,2)=reshape(imgVecNewQ(:,2),size(img(:,:,1)));
imgNewQ(:,:,3)=reshape(imgVecNewQ(:,3),size(img(:,:,1)));

imshow(img)
figure,imshow(imgNewQ,[]);

Result of kmeans :

If you want to add distance constraint to kmeans, the code will be slightly different. Basically, you need to concatenate pixel coordinates of corresponding pixel vales too. But remember, while assigning nearest centroid to each pixel, assign only the color i.e. the first 3 dimensions, not the last 2. That doesn't make sense, obviously. The code is very similar to the previous, please note the changes and understand them.

[col,row]=meshgrid(1:size(img,2),1:size(img,1));
imgVec=[reshape(img(:,:,1),[],1) reshape(img(:,:,2),[],1) reshape(img(:,:,3),[],1) row(:)   col(:)];
[imgVecQ,imgVecC]=kmeans(double(imgVec),4); %4 colors
imgVecQK=pdist2(imgVec(:,1:3),imgVecC(:,1:3));

[~,indMin]=min(imgVecQK,[],2);
imgVecNewQ=imgVecC(indMin,1:3);  %quantizing
imgNewQ=img;
imgNewQ(:,:,1)=reshape(imgVecNewQ(:,1),size(img(:,:,1))); %arranging back into image
imgNewQ(:,:,2)=reshape(imgVecNewQ(:,2),size(img(:,:,1)));
imgNewQ(:,:,3)=reshape(imgVecNewQ(:,3),size(img(:,:,1)));

imshow(img)
figure,imshow(imgNewQ,[]);

Result of kmeans with distance constraint:

这篇关于使用K均值聚类(使用RGB特征)对图像进行颜色量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆