基于CIELab空间色彩K-means聚类彩色图像分割

%可用于车牌检测,基于色彩的图像分割

%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));    %取出lab空间的a分量和b分量
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);  %重复聚类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

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值