基于LAB颜色空间的彩色图像分割

基于LAB颜色空间的彩色图像分割

首先加载原始图像

fabric = imread('fabric.png');
figure(1), imshow(fabric), title('fabric');

在这里插入图片描述
上述彩图包含背景黑色,红色,绿色,紫色,紫红色以及边缘的黄色。LAB颜色空间是基于色彩进行图像分割的最优空间。 Lab颜色模型由三个要素组成,一个要素是亮度(L),a 和b是两个颜色通道。a包括的颜色是从深绿色(低亮度值)到灰色(中亮度值)再到亮粉红色(高亮度值);b是从亮蓝色(低亮度值)到灰色(中亮度值)再到黄色(高亮度值)。

对于每一种颜色需选取相对应的一个小样本区域作为颜色标记区域。简化起见,这里直接导入MAT文件:

load regioncoordinates;

nColors = 6;
sample_regions = false([size(fabric,1) size(fabric,2) nColors]);

for count = 1:nColors
  sample_regions(:,:,count) = roipoly(fabric,region_coordinates(:,1,count),...
                                      region_coordinates(:,2,count));
end

imshow(sample_regions(:,:,2)),title('sample region for red');

在这里插入图片描述
上图显示了红色标记区域,显然,将原图与上图相乘即可得到红色区域的具体a,b通道数值平均值。求出的平均值可作为颜色标记点,最后可通过衡量原图中各个像素与标记点的距离做像素级分类,即为分割结果。

下面将原始图片从RGB通道转入LAB

lab_fabric = rgb2lab(fabric);

对于每种颜色,计算其相应的样本区域在a,b通道的均值作为颜色标记点:

a = lab_fabric(:,:,2);
b = lab_fabric(:,:,3);
color_markers = zeros([nColors, 2]);

for count = 1:nColors
  color_markers(count,1) = mean2(a(sample_regions(:,:,count)));
  color_markers(count,2) = mean2(b(sample_regions(:,:,count)));
end

得到的color_markers矩阵如下:
在这里插入图片描述
接下来使用近邻法对每个像素进行分类。对于每一个颜色标记点均有a,b值,可计算原始图像每个像素的a,b通道的数值与各个 颜色标记点的a,b值之间的欧式距离。最后选取距离最近的标记点作为该像素的颜色标记。

接下来创建一个color_labels 矩阵,其中:0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow。

color_labels = 0:nColors-1;

初始化近邻法所需参数:

a = double(a);
b = double(b);
distance = zeros([size(a), nColors]);

执行近邻法进行分类:


for count = 1:nColors
  distance(:,:,count) = ( (a - color_markers(count,1)).^2 + ...
                      (b - color_markers(count,2)).^2 ).^0.5;
end

[~, label] = min(distance,[],3);
label = color_labels(label);
clear distance;

所得到的label 矩阵包含了原图中每个像素属于那种颜色的标记。如:label(1,1)=0 表示原图中第一行第一列像素属于黑色。

接下来为了显示分割效果,创建一个segmented_images矩阵。以显示所分割出的红色区域为例:将原图中label(1,1)=1位置处的像素显示出来,而将label(1,1)~=1处的像素不显示即可。

rgb_label = repmat(label,[1 1 3]);
segmented_images = zeros([size(fabric), nColors],'uint8');

for count = 1:nColors
  color = fabric;
  color(rgb_label ~= color_labels(count)) = 0;
  segmented_images(:,:,:,count) = color;
end

imshow(segmented_images(:,:,:,2)), title('red objects');

得出的分类效果如下:
redgreenpmy
最后可显示各个像素在a,b颜色模型下二维空间内的分布情况:

purple = [119/255 73/255 152/255];
plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};

figure
for count = 1:nColors
  plot(a(label==count-1),b(label==count-1),'.','MarkerEdgeColor', ...
       plot_labels{count}, 'MarkerFaceColor', plot_labels{count});
  hold on;
end

title('Scatterplot of the segmented pixels in ''a*b*'' space');
xlabel('''a*'' values');
ylabel('''b*'' values');

在这里插入图片描述

  • 14
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值