使用 K-Means 在 LAB 颜色空间中按颜色对图像进行聚类

问题描述 投票:0回答:1

我尝试过以下代码。

he = imread('hestain.png');
imshow(he), title('H&E image');
cform = makecform('srgb2lab');
lab_he = applycform(he,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1); %n rows
ncols = size(ab,2); %p columns
ab = reshape(ab,nrows*ncols,2);

nColors = 3;

[cluster_idx, cluster_center] = kmeans(ab,nColors); 

它给了我错误

reshape 无法从 n*1 数组创建 n*p matirx。

这是有道理的,但它在here起作用。

我在八度中尝试了相同类型的代码

ed=edge(de,"canny");
imshow(ed);
ed=double(ed);
nrows=size(ed,1);
ncols=size(ed,2);
ed=reshape(ed,nrows*ncols,2)
[cluster_idx, cluster_center]=kmeans(ed,3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
imshow(pixel_labels,[]), title('image labeled by cluster index');

其中 de 是一些图像。 当我运行时出现此错误。

错误:重塑:无法将 181x181 数组重塑为 32761x2 数组

感谢帮助

matlab machine-learning image-processing cluster-analysis k-means
1个回答
0
投票

您提供的链接:MathWorks - 使用 K 均值聚类进行基于颜色的分割 使用的是

imsegkmeans()
,这与您使用的常规
kmeans()
函数不同。

然而,使用常规 K 均值来做同样的事情并不难:

numColors = 3;

mI      = im2double(imread('https://i.postimg.cc/Kz5YZFWy/Lena512-Color.png')); %<! Lena Image 512 Color
numRows = size(mI, 1);
numCols = size(mI, 2);
numPx   = numRows * numCols;
mLab    = rgb2lab(mI);
mAB     = mLab(:, :, 2:3);

mData = reshape(permute(mAB, [3, 1, 2]), numPx, 2);
[vC, mC] = kmeans(mData, numColors); 
© www.soinside.com 2019 - 2024. All rights reserved.