【发布时间】:2016-04-12 23:58:25
【问题描述】:
我想按颜色分割图片。我的示例图片是这样的:
这是我第一次使用 MATLAB。
首先我只想检测“红色”、“绿色”和“蓝色”。我不知何故让代码工作,它不仅检测红色,而且检测红色范围内的所有颜色。蓝色和绿色也是如此。但我只需要红色、蓝色和绿色。
也许这段代码对我的目的来说太多了。可能有更好的解决方案。
其次,是否可以设置颜色样本并将此样本用于不同的图片。我不想一次又一次地为每张图片设置样本。
clear all
close all
fabric = imread('sam3.JPG');
figure(1), imshow(fabric), title('fabric');
%% Step 2: Calculate Sample Colors in L*a*b* Color Space for Each Region
这里我不知道如何获取坐标。我尝试使用此线程Colour Segmentation by l*a*b 中提到的方法。但不知何故,它不起作用。我画了一个多边形,然后右键单击我可以复制它看起来像这样的位置:
[1082.5 730.5;1266.5 802.5;1186.5 962.5;1018.5 886.5] %section of red
所以我把它变成了红色
region_coordinates(:,1,1)=[1082.5 1266.5 1186.5 1018.5];
region_coordinates(:,2,1)=[730.5 802.5 962.5 886.5]; %red
region_coordinates(:,1,2)=[1602.5 1722.5 1622.5 1494.5];
region_coordinates(:,2,2)=[494.5 626.5 734.5 594.5]; %blue
region_coordinates(:,1,3)=[1794.5 1994.5 2014.5 1826.5];
region_coordinates(:,2,3)=[730.5 690.5 882.5 906.5]; %green
region_coordinates(:,1,4)=[2878.5 3078.5 3098.5 2910.5];
region_coordinates(:,2,4)=[1338.5 1298.5 1490.5 1514.5]; %black
其余代码来自 LabColorSegementationExample
nColors = 4;
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');
%%
% 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]);
for count = 1:nColors
color_markers(count,1) = mean2(a(sample_regions(:,:,count)));
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));
%% Step 3: Classify Each Pixel Using the Nearest Neighbor Rule
% Create an array that contains your color labels,
% i.e., 0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow.
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
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;
%% 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
imshow(segmented_images(:,:,:,2)), title('red objects');
%%
imshow(segmented_images(:,:,:,3)), title('green objects');
%%
imshow(segmented_images(:,:,:,8)), title('purple objects');
%%
%imshow(segmented_images(:,:,:,5)), title('magenta objects');
%%
%imshow(segmented_images(:,:,:,6)), title('yellow objects');
%% Step 5: Display 'a*' and 'b*' Values of the Labeled Colors.
% You can see how well the nearest neighbor classification separated the
% different color populations by plotting the 'a*' and 'b*' values of pixels that
% were classified into separate colors. For display purposes, label each
% point with its color label.
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');
displayEndOfDemoMessage(mfilename)
【问题讨论】:
-
我建议使用 HSV 进行颜色分割。请参阅或示例stackoverflow.com/questions/32795660/…
-
非常感谢 Ander Biguri,这是一个非常好的方法。结果比以前的代码好得多,而且速度更快。但是检测到一些颜色部分,我不想检测到。就像在这个例子中:link 编辑:好的,我把它转换为二进制图像并使用
imfill和strel。再次,非常感谢 Ander Biguri!到目前为止我很满意 :D PS.: 我能以某种方式将二进制图像“转换”回彩色图像并且只显示红色部分吗? -
是的,你可以!将图像的所有通道与蒙版相乘。这将为您提供仅具有该颜色的图像
标签: matlab colors image-segmentation