Matlab:Centroidsを描きます
-
27-09-2019 - |
質問
私の主な質問には、機能の重心が与えられていますが、どうすればmatlabで描くことができますか?
さらに詳しくは、私は持っています NxNx3
私が取る画像(RGB画像) 4x4
ブロックして計算a 6
- 各ブロックの次元機能ベクトル。これらの機能ベクトルをANに保存します Mx6
私が実行するマトリックス kmeans
機能し、aの重心を取得します kx6
マトリックス、ここで k
クラスターの数と 6
各ブロックの機能の数です。
アルゴリズムが実行されたい方法で実行されているかどうかを視覚化するために、これらの中心クラスターを自分の画像に描画するにはどうすればよいですか?または、誰かが自分の画像の重心をどのように視覚化できるかについて他の方法/提案を持っているなら、私はそれを大いに感謝します。
解決
クラスターを視覚化できる1つの方法は次のとおりです。
説明したように、最初にブロックを抽出し、それぞれの機能ベクトルを計算し、この機能マトリックスをクラスター化します。
次に、各ブロックに割り当てられたクラスターを視覚化できます。 4x4ブロックが明確であると想定していることに注意してください。これは、元の画像にブロックをそれらの場所にマッピングできるように重要です。
最後に、画像にクラスター重心を表示するために、各クラスターに最も近いブロックを見つけて、そのクラスターの代表として表示するだけです。
上記のアイデアを示すための完全な例を次に示します(あなたの場合、あなた自身の実装によって各ブロックの機能を計算する関数を置き換えることをお勧めします。私は単にMIN/MAX/平均/中央値/Q1/Q3を取得しています4x4ブロックごとに私の機能ベクトル):
%# 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')
他のヒント
重心は、画像内の座標に対応するのではなく、特徴空間の調整に対応します。 Kmeansがどれだけうまく実行されたかをテストする方法は2つあります。どちらの方法でも、ポイントを最も近いクラスターに拳を関連付けたいと思うようにします。 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
ブロック、あなたは書きます
%# 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)