2016-12-13 172 views
1

skin cancer image 我想申請K爲灰度圖像的聚類,代碼如下如何在灰度圖像應用k均值聚類在MATLAB

im = imread('SSM1_2_orig.jpg'); 
im = rgb2gray(im); 
[idx centroids]=kmeans(double(im(:)),3,'distance','sqEuclidean','Replicates',3); 
%imseg = zeros(size(im,1),size(im,2)); 
%{for i=1:max(idx) 
    %imseg(idx==i)=i; 
    %end} 
segmented_images = cell(1,3); 
for k = 1:3 
    color = im; 
    color(im ~= k) = 0; 
    segmented_images{k} = color; 
end 

figure(),imshow(segmented_images{1}); 
figure(),imshow(segmented_images{2}); 
figure(),imshow(segmented_images{3}); 

,但它給了我黑色的輸出僅

+0

你試過'imshow(segmented_images {1},[]);' –

+0

是的,但它給人的黑色輸出僅 –

+0

那麼能否請您添加原始圖像,所以我們可以嘗試一下呢? –

回答

1

這是工作代碼。備註:

您從不使用聚類結果,而是將原始像素值與k比較,而不是聚類像素值idx。如果你的圖像不是[0-1]或[0-255],請記住使用imshow(____, [])

im = imread('https://i.stack.imgur.com/ZYp7r.jpg'); 
im = rgb2gray(im); 
[idx, centroids]=kmeans(double(im(:)),3,'distance','sqEuclidean','Replicates',3); 

segmented_images = cell(1,3); 
for k = 1:3 
    color = zeros(size(im)); 
    color(idx==k) = im(idx==k); 
    segmented_images{k} = color; 
end 

figure(),imshow(segmented_images{1},[]); 
figure(),imshow(segmented_images{2},[]); 
figure(),imshow(segmented_images{3},[]);