value changes when running each time

x=imread('im9.bmp');
HSV = rgb2hsv(x);
H = HSV(:,:,1); H = H(:);
S = HSV(:,:,2); S = S(:);
V = HSV(:,:,3); V = V(:);
idx = kmeans([H S V], 3,'distance','sqEuclidean');
tabulate(idx)
the percentage for each idx gets changed every time i run the program,whay i am getting like this,kindly assist

3 件のコメント

Jan
Jan 2013 年 3 月 5 日
編集済み: Jan 2013 年 3 月 5 日
BTW., The creation of your [H S V] can be abbreviated, such that you do not need H, S and V explicitly:
idx = kmeans(reshape(HSV, [], 3), 3,'distance','sqEuclidean');
This behaviour of kmeans is explained in the dcoumentation. It is strongly recommended to read it, especially in cases of problems: doc kmeans
kash
kash 2013 年 3 月 22 日
I used
idx = kmeans(reshape(HSV, [], 3), 3,'distance','sqEuclidean');
even then i get same error each cluster changes and different color is applied each time
Walter Roberson
Walter Roberson 2013 年 3 月 22 日
kmeans still uses random initialization when you use reshape(), unless you provide the starting centroids like I showed below.

サインインしてコメントする。

回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 3 月 5 日
編集済み: Youssef Khmou 2013 年 3 月 5 日

1 投票

hi kash,
i quote , from Wikipedia : " Commonly used initialization methods are Forgy and Random Partition" so each time you run the code : Random partition is the cause of that small change , so here is what you can do :
you run the code 10 times , each time you store that variable, it is then a R.V calculate its mean and STD , and conclude .

9 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 5 日
Right. kmeans uses random initialization unless you provide initial values.
kash
kash 2013 年 3 月 5 日
walter can oyu tell how to set initial values plz
Walter Roberson
Walter Roberson 2013 年 3 月 5 日
kmeans([H S V], 3, 'distance', 'sqEuclidean', 'start', MatrixOfCentroids)
MatrixOfCentroids would have to be 3 (number of clusters) by 3 (number of columns of [H S V])
kash
kash 2013 年 3 月 22 日
Walter i tried yours but i get error
Undefined function or variable 'MatrixOfCentroids'.
what should it be
Walter Roberson
Walter Roberson 2013 年 3 月 22 日
What does the documentation for kmeans() say should be passed in after the word 'start' ?
kash
kash 2013 年 3 月 22 日
i passed [],got error ,passed 'uniform' stll same result,colour changes but dont which matrix to pass for
k-by-p matrix of centroid
kash
kash 2013 年 3 月 22 日
I ALSO TRIED
C=HSV(102:104,102:104); for k by p matrix,but still colour keeps changing
Teja Muppirala
Teja Muppirala 2013 年 3 月 22 日
Are you sure you have typed everything correctly?
x = imread('peppers.png');
HSV = rgb2hsv(x);
C=HSV(102:104,102:104);
idx = kmeans(reshape(HSV, [], 3),3,'distance','sqEuclidean','Start',C);
tabulate(idx)
For me, no matter how many times I run this, I always get the same result:
Value Count Percent
1 74013 37.64%
2 99481 50.60%
3 23114 11.76%
kash
kash 2013 年 3 月 22 日
sorry the mistake i made was placed C inside single quotes,thanks

サインインしてコメントする。

タグ

質問済み:

2013 年 3 月 5 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by