基于k-means的图像分割MATLAB程序 15
1个回答
展开全部
close all;
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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询