私はKを使って画像を分割するコードを見つけましたが、以下のコードでは最小値、最大値を見つけるために計算を使用しています。K平均私はこのコードを理解できませんでした。誰かが説明してください。Kはmatlabでのクラスタリングを意味します
function [Centroid,new_cluster]=kmeans_algorithm(input_image,k)
% k = 4;
input_image=double(input_image);
new_image=input_image;
input_image=input_image(:);
min_val=min(input_image);
input_image=round(input_image-min_val+1);
length_input_image=length(input_image);
max_val=max(input_image)+1;
hist_gram=zeros(1,max_val);
hist_gram_count=zeros(1,max_val);
for i=1:length_input_image
if(input_image(i)>0)
hist_gram(input_image(i))=hist_gram(input_image(i))+1;
end;
end
IDX=find(hist_gram);
hist_length=length(IDX);
Centroid=(1:k)*max_val/(k+1);
while(true)
old_Centroid=Centroid;
for i=1:hist_length
new_val=abs(IDX(i)-Centroid);
hist_val=find(new_val==min(new_val));
hist_gram_count(IDX(i))=hist_val(1);
end
for i=1:k,
loop_count=find(hist_gram_count==i);
Centroid(i)=sum(loop_count.*hist_gram(loop_count))/sum(hist_gram(loop_count));
end
if(Centroid==old_Centroid) break;end;
end
length_input_image=size(new_image);
new_cluster=zeros(length_input_image);
for i=1:length_input_image(1),
for j=1:length_input_image(2),
new_val=abs(new_image(i,j)-Centroid);
loop_count=find(new_val==min(new_val));
new_cluster(i,j)=loop_count(1);
end
end
Centroid=Centroid+min_val-1;
特に上記のコードではinput_image(:)
の目的は何ですか。グーグルでは行列のように言っていましたが、行列や配列であっても混乱しています。
[this](http://stackoverflow.com/questions/26726257/color-quantization-of-an-image-using-k-means-clustering-using-rgb-features/26726929#)をご覧ください。 26726929)。 –
@Paragありがとうございました – temp