%可用于车牌检测,基于色彩的图像分割
%matlab中CIElab空间的三个分量,L,a,b的范围为(0~255),(0~255),(0~255)。
%而不是标准的(0~100),(-120,,120)。
close all;
clear;
I_rgb = imread('car.jpg');
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));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 5;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',100);
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
end
for i=1:nColors
figure(2),subplot(1,nColors,i);imshow(segmented_images{i}), title('分割结果');
end