www.pudn.com > demo.zip > fenge.m, change:2012-04-13,size:1235b


function segmented_images=fenge(); 
 
I_rgb=imread('1.jpg'); 
figure(); 
imshow(I_rgb); 
title('原始图像'); 
 
% sigma = 25;  
% if (length(size(I_rgb))>2) 
%     I_rgb = rgb2gray(I_rgb); 
% end 
% if (max(I_rgb(:))<2) 
%   I_rgb = I_rgb*255; 
% end 
% % I_rgb=im2double(I_rgb); 
%  
% h=sigma*randn(size(I_rgb)); 
% h=uint8(h); 
% I_rgb=I_rgb+h; 
% % PSNRIn = 20*log10(255/sqrt(mean((IMin(:)-I_rgb(:)).^2))); 
% % I_rgb=uint8(I_rgb); 
% I_rgb=imnoise(I_rgb,'salt & pepper',0.01);  
% figure(); 
% imshow(I_rgb); 
C=makecform('srgb2lab'); 
I_lab=applycform(I_rgb,C); 
ab=double(I_lab(:,:,2:3)); 
nrows=size(ab,1); 
ncols=size(ab,2); 
ab=reshape(ab,nrows*ncols,2); 
nColors=3; 
[cluster_idx cluster_center]=kmeans(ab,nColors,'distance','sqEuclidean','Replicates',4); 
pixel_labels=reshape(cluster_idx,nrows,ncols); 
figure(); 
imshow(pixel_labels,[]),title('聚类结果'); 
segmented_images=cell(1,3); 
rgb_lable=repmat(pixel_labels,[1 1 3]); 
for k=1:nColors 
    color=I_rgb; 
    color(rgb_lable~=k)=0; 
    segmented_images{k}=color; 
end 
% figure(),imshow(segmented_images{1}),title('分割结果-区域1'); 
% figure(),imshow(segmented_images{2}),title('分割结果-区域2'); 
% figure(),imshow(segmented_images{3}),title('分割结果-区域3');