2

私の主な質問は特徴の重心が与えられていますが、MATLABでそれを描画するにはどうすればよいですか?

より詳細には、ブロックを取得し、各ブロックの次元特徴ベクトルを計算するNxNx3画像(RGB画像)があります。これらの特徴ベクトルを行列に格納し、その上で関数を実行して、行列の重心を取得します。ここで、はクラスターの数であり、は各ブロックの特徴の数です。4x46Mx6kmeanskx6k6

アルゴリズムが希望どおりに実行されているかどうかを視覚化するために、これらの中央のクラスターを画像に描画するにはどうすればよいですか?または、画像の重心を視覚化する方法について他の方法や提案がある場合は、大いに感謝します。

4

2 に答える 2

3

クラスターを視覚化する1つの方法は次のとおりです。

説明したように、最初にブロックを抽出し、それぞれの特徴ベクトルを計算して、この特徴行列をクラスター化します。

次に、各ブロックに割り当てられたクラスターを視覚化できます。4x4ブロックは別個のものであると想定していることに注意してください。これは、ブロックを元の画像の元の場所にマップできるようにするために重要です。

最後に、画像にクラスターの重心を表示するために、各クラスターに最も近いブロックを見つけて、そのクラスターの代表として表示します。

上記のアイデアを示す完全な例を次に示します(あなたの場合、各ブロックの特徴を計算する関数を独自の実装に置き換えたいと思います。私は単に最小/最大/平均/中央値/ 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')

クラスター 画像

于 2010-04-16T02:57:06.453 に答える
0

図心は、画像内の座標ではなく、フィーチャスペース内の座標に対応します。kmeansのパフォーマンスをテストする方法は2つあります。どちらの方法でも、ポイントを最も近いクラスターに最初に関連付けます。この情報は、kmeansの最初の出力から取得します。

(1)6次元空間を2次元または3次元空間に縮小し、分類の異なる座標を異なる色でプロットすることにより、クラスタリングの結果を視覚化できます。

特徴ベクトルがと呼ばれる配列に収集され、クラスターfeatureArrayを要求したとすると、mdscaleを使用して次のようにプロットし、データを3D空間に変換します。nClusters

%# 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)
于 2010-04-16T01:01:27.130 に答える