OI have used the following code to segment the attached image. But each iteration on the same image shows different result. How can i solve this by using rng('default'); ?

댓글 수: 2

Adam
Adam 2017년 3월 31일
You should just need to explicitly set the seed (either to 'default' I guess or to any seed of your choice) before each call to kmeans if you want the same result each time.
sam  CP
sam CP 2017년 3월 31일
편집: sam CP 2017년 3월 31일
%k-means clustering algorithm
imData = reshape(Y,[],1);
imData = double(imData);
[IDX nn] = kmeans(imData,'default');
imIDX = reshape(IDX,size(Y));
figure, imshow(imIDX,[]),title('Image after applying k-means Clustering Algorithm');
Where can i apply the rng('default'); ?

댓글을 달려면 로그인하십시오.

 채택된 답변

the cyclist
the cyclist 2017년 3월 31일
편집: the cyclist 2017년 3월 31일

2 개 추천

Looking at your code, you should be able to put the line
rng('default')
literally anywhere before the call to kmeans, because you do not call any other random number functions. But the safest bet might be to call it in the line just before the call to kmeans, in case you do something differently later.
But, also, I don't think you put 'default' in the actual kmeans call. So it should be like this ...
%k-means clustering algorithm
imData = reshape(Y,[],1);
imData = double(imData);
rng('default')
[IDX nn] = kmeans(imData);
imIDX = reshape(IDX,size(Y));
figure, imshow(imIDX,[]),title('Image after applying k-means Clustering Algorithm');

댓글 수: 10

sam  CP
sam CP 2017년 3월 31일
편집: sam CP 2017년 3월 31일
Error using kmeans (line 155) At least two input arguments required.
Error in Untitled2 (line 49) [IDX nn] = kmeans(imData);
Is there again need to specify the cluster number after applying rng('default') this line into my code.
Adam
Adam 2017년 3월 31일
편집: Adam 2017년 3월 31일
Your original (from your comment above)
[IDX nn] = kmeans(imData,'default');
is not valid syntax for calling kmeans as far as I can see. There is no function signature where 'default' makes sense as a 2nd argument.
The code you attached calls it totally differently so just use that exactly as you did before.
sam  CP
sam CP 2017년 3월 31일
Error using kmeans (line 265) K must be a positive integer value.
Error in Untitled2 (line 49) [IDX nn] = kmeans(imData,'default');
I;m really not understanding how your code relates in any way to the code you are saying creates an error. At a glance your code here looks fine, just add
rng('default')
on the line above
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',2);
I don't know where:
[IDX nn] = kmeans(imData, 'default');
is coming from at all.
sam  CP
sam CP 2017년 3월 31일
The result will not be changed in each iteration on the same image. Thank you. The code works.
sam  CP
sam CP 2017년 3월 31일
What changes i have to make in the code when i want to make 4 clusters?
Adam
Adam 2017년 3월 31일
Change nColours to 4.
sam  CP
sam CP 2017년 3월 31일
it works
Image Analyst
Image Analyst 2017년 3월 31일
Yeah, but let's put "works" in quotation marks because kmeans() is not a good method for finding brain tumors. Imagine what your algorithm would find for class 4 if there were no tumor present, or a very small one. Yeah, see what I mean?
sam  CP
sam CP 2017년 4월 3일
I have already found that the kmeans clustering method can't be detect the tumor when it is very small.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2017년 3월 31일

댓글:

2017년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by