基于FCM的灰度图像的分割, matlab免费源码

clear; close all; clc;

%%读入图像,

I = imread('figure');

figure, imshow(I,[])

[m, n, p] = size(I);
data = zeros(m*n,1);
I1 = reshape(double(I), m*n, p); %将数据转换为一列数据
data(1:m*n,1) = I1;

cluster_n = 2;    %聚类数目,自定义
options = [2, 500, 1e-5, 0];   %设定聚类参数
[center, U, obj_fcn] = fcm(data, cluster_n, options); %调用fcm实现聚类, matlab自带
cluster = cell(1, cluster_n);
cluster_pic = cell(1, cluster_n);
pic = cell(1, cluster_n);

%%按聚类结果分割图像

for i = 1:cluster_n
    cluster{i} = find( U(i,:)==max(U));
    cluster{i} = cluster{i}';
    cluster_pic{i} = data(1:m*n,1);
    temp = data(1:m*n,1);
    temp(cluster{i},:) = 0; 
    cluster_pic{i} = cluster_pic{i}-temp;
    pic{i} = reshape(cluster_pic{i},m,n,p);
    figure;
         imshow(uint16(pic{i}));
end

 

 

 

你可能感兴趣的:(图像处理)