如何在matlab中对灰度图像应用kmeans聚类

时间:2016-12-13 10:28:44

标签: matlab k-means image-segmentation

skin cancer image 我想在灰度图像上应用K表示聚类,代码如下

    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});

但它只给我黑色输出

1 个答案:

答案 0 :(得分:1)

这是工作代码。注意:

您永远不会使用聚类结果,而是将原始像素值与apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.1" defaultConfig { applicationId "ba.application.travel" minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "-std=c++11" } } dexOptions { javaMaxHeapSize "4g" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "CMakeLists.txt" } } sourceSets { main { assets.srcDirs = ['src/main/assets'] } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1' compile project(':buttonstyle') compile 'com.android.support:design:25.0.1' compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.android.support:percent:25.0.1' compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' compile 'com.android.support:support-v4:25.0.1' compile 'com.android.support:gridlayout-v7:25.0.1' compile 'com.android.support:cardview-v7:25.0.1' compile 'com.android.support:recyclerview-v7:25.0.1' compile 'com.akexorcist:RoundCornerProgressBar:2.0.3' compile 'com.github.aakira:expandable-layout:1.4.2@aar' compile 'com.github.grennis:ExpandableRecyclerView:0.9.3' compile 'com.github.markushi:circlebutton:1.1' testCompile 'junit:junit:4.12' } 进行比较,而不是聚集像素值k

另外,如果您的图片不是[0-1]或[0-255],请记得使用idx

imshow(____, [])