matlab - 按 l*a*b 进行颜色分割

标签 matlab image-processing colors segment

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

所以我尝试自己选择一些区域,而不是使用“加载区域坐标”,使用 roipoly(fabric),但我陷入困境。如何保存刚刚绘制的多边形的坐标?我实际上遵循解决方案 II 页面底部 lennon310 的建议: A few questions about color segmentation with L*a*b*

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

我进行了以下更改(第 1 步):

1) 删除了“加载区域坐标”

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

代码如下:

` %% 步骤 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:使用最近邻规则对每个像素进行分类 %

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:显示最近邻分类的结果 % % 标签矩阵包含织物图像中每个像素的颜色标签。 % 使用标签矩阵按颜色分离原始织物图像中的对象。

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中的输出参数来检索多边形的坐标。然后,您可以获得多边形的二进制掩码,以及顶点坐标(如果需要)。

简单的例子演示:

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);

看起来像这样:

enter image description here

如果您希望使用二进制掩码查看顶点:

%// display polymask
imshow(polyMask)
hold on

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

给出以下结果:

enter image description here

总结一下:

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

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

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

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

那么你就可以开始了。希望有帮助!

关于matlab - 按 l*a*b 进行颜色分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923636/

相关文章:

matlab - 使用 matlab 从 URL 获取所有 .nc 文件以获取数据

python - 使用 scipy ndimage.rotate(值高于 1.0)旋转后出现奇怪的伪像

image-processing - 用于数据增强的 Keras 最佳图像数据生成器参数

python - 一种不必每次都在 colorama 中重置颜色/样式的方法

java - 矩形不更新也不显示颜色

启动 MATLAB 时出现 java 错误消息

.net - .NET/Oracle 架构的数据处理逻辑的最佳位置

matlab - 在 Matlab/Simulink Stateflow 中设置事件

c# - 面经应用

c# - 更改 Bing map 图钉的颜色