2

我的主要問題給出一個特徵質心,我怎樣才能把它收回去MATLAB?MATLAB:平局重心

更詳細地說,我有一個NxNx3圖像(RGB圖像),其中我爲每個塊取4x4塊並計算一個6-維特徵向量。我將這些特徵向量存儲在一個Mx6矩陣中,我在該矩陣上運行kmeans函數並獲取kx6矩陣中的質心,其中k是羣集數量,6是每個塊的要素數量。

我如何才能,如果算法執行我希望它執行的方式來可視化繪製自己的形象,這些中心集羣?或者如果任何人有任何其他方式/建議如何我可以想象我的形象上的質心,我會非常感激。

回答

3

這裏有一種方法可以可視化的簇:

如你所描述的,第一I提取的塊,計算每個特徵向量和羣集這個功能矩陣。

接下來,我們可以可視化分配給每個塊的簇。請注意,我假設4x4塊是不同的,這很重要,這樣我們就可以將塊映射回原始圖像中的位置。

最後,爲了顯示的圖像上的聚類中心,我簡單找到最接近塊到每個羣集並顯示它作爲集羣的代表。下面是一個完整的例子來展示上面的想法(在你的情況下,你會想用你自己的實現來替換計算每個塊的特徵的函數;我只是簡單地把最小/最大/平均值/中值/ Q1/Q3爲每個4×4塊我的特徵向量):

%# params 
NUM_CLUSTERS = 3; 
BLOCK_SIZE = 4; 
featureFunc = @(X) [min(X); max(X); mean(X); prctile(X, [25 50 75])]; 

%# read image 
I = imread('peppers.png'); 
I = double(rgb2gray(I)); 

%# extract blocks as column 
J = im2col(I, [BLOCK_SIZE BLOCK_SIZE], 'distinct'); %# 16-by-NumBlocks 

%# compute features for each block 
JJ = featureFunc(J)';        %'# NumBlocks-by-6 

%# cluster blocks according to the features extracted 
[clustIDX, ~, ~, Dist] = kmeans(JJ, NUM_CLUSTERS); 

%# display the cluster index assigned for each block as an image 
cc = reshape(clustIDX, ceil(size(I)/BLOCK_SIZE)); 
RGB = label2rgb(cc); 
imshow(RGB), hold on 

%# find and display the closest block to each cluster 
[~,idx] = min(Dist); 
[r c] = ind2sub(ceil(size(I)/BLOCK_SIZE), idx); 
for i=1:NUM_CLUSTERS 
    text(c(i)+2, r(i), num2str(i), 'fontsize',20) 
end 
plot(c, r, 'k.', 'markersize',30) 
legend('Centroids') 

clusters image

0

的質心不對應的圖像中的座標,但在功能空間座標。有兩種方法可以測試kmeans的表現。對於這兩種方式,你都希望將這些觀點與他們最接近的羣集聯繫起來。你從kmeans的第一個輸出中獲得這些信息。

(1)可以通過減少6維空間2或3維空間,然後在繪製不同顏色的不同分類的可視化座標的聚類結果。

假設特徵向量被收集在一個名爲featureArray一個數組,你問nClusters集羣,你會做的情節使用mdscale轉換數據,比方說如下,3D空間:

%# kmeans clustering 
[idx,centroids6D] = kmeans(featureArray,nClusters); 
%# find the dissimilarity between features in the array for mdscale. 
%# Add the cluster centroids to the points, so that they get transformed by mdscale as well. 
%# I assume that you use Euclidean distance. 
dissimilarities = pdist([featureArray;centroids6D]); 
%# transform onto 3D space 
transformedCoords = mdscale(dissimilarities,3); 
%# create colormap with nClusters colors 
cmap = hsv(nClusters); 
%# loop to plot 
figure 
hold on, 
for c = 1:nClusters 
    %# plot the coordinates 
    currentIdx = find(idx==c); 
    plot3(transformedCoords(currentIdx,1),transformedCoords(currentIdx,2),... 
     transformedCoords(currentIdx,3),'.','Color',cmap(c,:)); 
    %# plot the cluster centroid with a black-edged square 
    plot3(transformedCoords(1:end-nClusters+c,1),transformedCoords(1:end-nClusters+c,2),... 
     transformedCoords(1:end-nClusters+c,3),'s','MarkerFaceColor',cmap(c,:),... 
     MarkerEdgeColor','k'); 
end 

(2)你可以,或者創​​建一個僞彩色圖像,顯示你,你寫這集羣

假設你有nRows通過nCols

什麼形象的一部分,屬於3210
%# kmeans clustering 
[idx,centroids6D] = kmeans(featureArray,nClusters); 
%# create image 
img = reshape(idx,nRows,nCols); 
%# create colormap 
cmap = hsv(nClusters); 

%# show the image and color according to clusters 
figure 
imshow(img,[]) 
colormap(cmap)