通过l * a * b进行颜色分割 [英] Colour Segmentation by l*a*b

查看:486
本文介绍了通过l * a * b进行颜色分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MatLab网站上的代码,使用L a b *颜色空间进行基于颜色的分割:
http://www.mathworks.com/help/images/ examples / color-based-segmentation-using-the-lab-color-space.html

I'm using the code on the MatLab website, "Color-Based Segmentation Using the Lab* Color Space": http://www.mathworks.com/help/images/examples/color-based-segmentation-using-the-l-a-b-color-space.html

所以我试图自己选择一些区域而不是使用load region_coordinates,使用roipoly(fabric),但我卡住了。如何保存刚绘制的多边形的坐标?我实际上是在解决方案II页面底部遵循lennon310的建议:
关于使用L * a * b * 进行颜色分割的几个问题

So I'm trying to select some areas myself instead of using the "load region_coordinates", using roipoly(fabric), but i get stuck. How do I save coordinates of the polygon I just drew? I'm actually following advice from lennon310 at Solution II, bottom of page: A few questions about color segmentation with L*a*b*

我不确定何时保存 region_coordinates 并执行尺寸(region_coordinates,1)

I'm not sure when to save region_coordinates and do size(region_coordinates,1)

我制作以下更改(第1步)

1)已移除load region_coordinates

1) Removed "load region_coordinates"

2)添加了region_coordinates = roipoly(fabric);

2) Added "region_coordinates = roipoly(fabric);"

以下是代码:

`
%%第1步

` %% Step 1

fabric = imread(file);

figure(1);                                                                   %Create figure window. "If h is not the handle and is not the Number property value of an existing figure, but is an integer, then figure(h) creates a figure object and assigns its Number property the value h."
imshow(fabric)
title('fabric')



%load regioncoordinates; % 6 marices(?) labelled val(:,:,1-6), 5x2 (row x column)
region_coordinates = roipoly(fabric);

nColors = 6;
sample_regions = false([size(fabric,1) size(fabric,2) nColors]); %Initializing an Image Dimension, 3x3 (:,:,:) to zero? Zeros() for arrays only I guess.
                        %Size one is column, size two is row?
for count = 1:nColors
  sample_regions(:,:,count) = roipoly(fabric,region_coordinates(:,1,count),region_coordinates(:,2,count));

end

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

%%第2步

% Convert your fabric RGB image into an L*a*b* image using rgb2lab .

lab_fabric = rgb2lab(fabric);


%Calculate the mean a* and b* value for each area that you extracted with roipoly. These values serve as your color markers in a*b* space.

a = lab_fabric(:,:,2);
b = lab_fabric(:,:,3);
color_markers = zeros([nColors, 2]);%... I think this is initializing a 6x2 blank(0) array for colour storage. 6 for colours, 2 for a&b colourspace.

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

%For example, the average color of the red sample region in a*b* space is

fprintf('[%0.3f,%0.3f] \n',color_markers(2,1),color_markers(2,2));

%%步骤3:使用最近邻规则对每个像素进行分类

%% Step 3: Classify Each Pixel Using the Nearest Neighbor Rule %

color_labels = 0:nColors-1;

% Initialize matrices to be used in the nearest neighbor classification.

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


%Perform classification, Elucidean Distance.

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;

%%步骤4:显示最近邻分类的结果

%标签矩阵包含织物图像中每个像素的颜色标签。
%使用标签矩阵按颜色分隔原始织物图像中的对象。

%% Step 4: Display Results of Nearest Neighbor Classification % % The label matrix contains a color label for each pixel in the fabric image. % Use the label matrix to separate objects in the original fabric image by color.

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

%figure, imshow(segmented_images(:,:,:,1)), title('Background of Fabric');
%Looks different somehow.
figure, imshow(segmented_images(:,:,:,2)), title('red objects');

figure, imshow(segmented_images(:,:,:,3)), title('green objects');

figure, imshow(segmented_images(:,:,:,4)), title('purple objects');

figure, imshow(segmented_images(:,:,:,5)), title('magenta objects');

figure, imshow(segmented_images(:,:,:,6)), title('yellow objects');



`


推荐答案

您可以在 roipoly 的调用中使用输出参数检索多边形的坐标。然后,您可以获得多边形的二进制蒙版,以及顶点坐标(如果需要)。

You can retrieve the coordinates of the polygon using output arguments in the call to roipoly. You can then get a binary mask of the polygon, as well as vertices coordinates if you want.

演示的简单示例:

clear
clc
close all

A = imread('cameraman.tif');

figure;
imshow(A)

%// The vertices of the polygon are stored in xi and yi;
%// PolyMask is a binary image where pixels == 1 are white.
[polyMask, xi, yi] = roipoly(A);

这看起来像这样:

< img src =https://i.stack.imgur.com/LQa1d.jpgalt =在此输入图像说明>

如果你想看到带有二元掩码的顶点:

And if you wish to see the vertices with the binary mask:

%// display polymask
imshow(polyMask)
hold on

%// Highlight vertices in red
scatter(xi,yi,60,'r')
hold off

其中包含以下内容:

总结一下:

1)多边形的顶点存储在xi和yi中。

1) The polygon's vertices are stored in xi and yi.

2)您可以使用 imshow(polyMask)绘制多边形的binaryMask

3)如果您需要白色像素的坐标,可以使用以下内容:

3) If you need the coordinates of the white pixels, you can use something like this:

[row_white,col_white] = find(polyMask == 1);

你很高兴。希望有所帮助!

You're then good to go. Hope that helps!

这篇关于通过l * a * b进行颜色分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆