clear
I_rgb = imread('color-cam4-f0.bmp') %读取文件数据
figure(1)
subplot(1,2,1)
imshow(I_rgb) %显示原图
title('原始图像')
%将彩耐行色图像从昌姿哗RGB转化到lab彩色空间
C = makecform('srgb2lab') %设置转册衡换格式
I_lab = applycform(I_rgb, C)
%进行K-mean聚类将图像分割成3个区域
ab = double(I_lab(:,:,2:3)) %取出lab空间的a分量和b分量
nrows = size(ab,1)
ncols = size(ab,2)
ab = reshape(ab,nrows*ncols,2)
nColors = 4 %分割的区域个数为
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',100) %重复聚类3次
pixel_labels = reshape(cluster_idx,nrows,ncols)
figure(1)
subplot(1,2,2)
imshow(pixel_labels,[]), title('聚类结果')
%显示分割后的各个区域
segmented_images = cell(1,nColors)
rgb_label = repmat(pixel_labels,[1 1 3])
for k = 1:nColors
color = I_rgb
color(rgb_label ~= k) = 0
segmented_images{k} = color
end
for i=1:nColors
figure(2),subplot(1,nColors,i)imshow(segmented_images{i}), title('分割结果')
end
function [mu,mask]=kmeans(ima,k)%k为指定类别数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% kmeans image segmentation
%
% Input:
% ima: grey color image灰度图像
% k: Number of classes指定的图像中类别数目
% Output:
% mu: vector of class means 每个类的均值
% mask: clasification image mask分类后的图像掩膜(mask)
%
% Author: Jose Vicente Manjon Herrera
%Email: jmanjon@fis.upv.es
% Date: 27-08-2005
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check image
ima=double(ima)
copy=ima% make a copy
ima=ima(:) % vectorize ima将图像向量化,即一维化。
mi=min(ima) % deal with negative
ima=ima-mi+1% and zero values
s=length(ima)%获得图像像素个数
% create image histogram%创建图像直方图
m=max(ima)+1%最大像素值加1
h=zeros(1,m)%直方图,有m个bin
hc=zeros(1,m)%标号矩阵,每个像素点的值为该点所隶属的类别号
for i=1:s%s是图像象素个数,即考查每个像素
if(ima(i)>0) h(ima(i))=h(ima(i))+1end%直方图中对应bin加1
end
ind=find(h)%找到直方图中不为零的那些bin的序号。
hl=length(ind)%直方图中非零bin的个数
% initiate centroids
mu=(1:k)*m/(k+1)%k为指定的类别数,mu为不同类的分割点,相当于坐标轴上的整点
% start process
while(true)
oldmu=mu
% current classification
for i=1:hl
c=abs(ind(i)-mu)%就是相当于考察ind(i)在坐标轴上离哪个整点最近!注意mu总共就k个
cc=find(c==min(c))%cc保留距离ind(i)最近整点的前槐序号,序号为基悔羡1、2、3...k
hc(ind(i))=cc(1)
end
%recalculation of means 下面的程序用于计算每一类的均值位置
for i=1:k,
a=find(hc==i)
mu(i)=sum(a.*h(a))/sum(h(a))%h为直搏拍方图
end
if(mu==oldmu) breakend%循环结束条件
end
% calculate mask
s=size(copy)
mask=zeros(s)
mask1=mask%增加一个显示矩阵
size(mask1)
for i=1:s(1),
for j=1:s(2),
c=abs(copy(i,j)-mu)
a=find(c==min(c))
mask(i,j)=a(1)
end
end
mu=mu+mi-1 % recover real range
for i = 1 : k
p=find(mask==i)
mask1(p)=1/k*i
end
figure,imshow(mask1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)