使用 K 均值聚类准确检测图像中的颜色区域

Accurately detect color regions in an image using K-means clustering

我在基于颜色的图像分割中使用 K 均值聚类。我有一个二维图像,它有 3 种颜色,黑色、白色和绿色。这是图片,

我希望 K-means 产生 3 个聚类,一个代表绿色区域,第二个代表白色区域,最后一个代表黑色区域。

这是我使用的代码,

%Clustering color regions in an image. 

%Step 1: read the image using imread, and show it using imshow. 

img =  (imread('img.jpg'));

figure, imshow(img), title('X axis rock cut'); %figure is for creating a figure window.
text(size(img,2),size(img,1)+15,...
     'Unconventional shale x axis cut', ...
     'FontSize',7,'HorizontalAlignment','right');

 %Step 2: Convert Image from RGB Color Space to L*a*b* Color Space
 conversionform = makecform('srgb2lab'); %the form of the conversion is defined as from rgb to l a b
 lab_img = applycform(img,conversionform); %converting the rgb image to l a b image using the conversion form defined above.

 %Step 3: Classify the Colors in 'a*b*' Space Using K-Means Clustering
 ab = double(lab_img(:,:,2:3));
 nrows = size(ab,1);
 ncols = size(ab,2);
 ab = reshape(ab,nrows*ncols,2);

 nColors = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
                                      'Replicates',3);
%Step 4: Label Every Pixel in the Image Using the Results from KMEANS

%For every object in your input, kmeans returns an index corresponding to a cluster. The cluster_center output from kmeans will be used later in the example. Label every pixel in the image with its cluster_index.

pixel_labels = reshape(cluster_idx,nrows,ncols);
figure, imshow(pixel_labels,[]), title('image labeled by cluster index');

segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);

for k = 1:nColors
    color = img;
    color(rgb_label ~= k) = 0;
    segmented_images{k} = color;
end

figure, imshow(segmented_images{1}), title('objects in cluster 1');
figure, imshow(segmented_images{2}), title('objects in cluster 2');
figure, imshow(segmented_images{3}), title('objects in cluster 3');

但我没有得到所需的结果。我得到一个带有绿色区域的集群,一个带有绿色区域边界的集群,以及一个带有灰色、黑色和白色的集群。这是生成的集群。

这样做的目的是在得到正确的聚类结果后,我想用连通分量的概念统计每个区域的像素个数

所以,我的目标是知道每个颜色区域有多少像素。我尝试了另一种更简单的方法,即获取 2D 图像的矩阵并尝试计算出每种颜色的像素数。但是,我在矩阵中发现了 3 种以上的 RGB 颜色,可能是因为相同颜色的像素具有略微不同的颜色级别。这就是我去图像分割的原因。

任何人都可以告诉我如何修正上面的代码以获得所需的结果吗?

如果您能提示我如何以更简单的方式执行此操作(如果有的话),我将不胜感激。

编辑:这是我编写的代码,用于遍历图像中的每个像素。请注意,我使用了红、黄、蓝、白 4 种颜色,而不是绿、白、黑,但思路是一样的。 rgb2name是函数returns给定RGB颜色的颜色名称。

im= imread ('img.jpg'); 

[a b c] = size (im); 
%disp ([a b]);
yellow=0; 
blue=0; 
white=0; 
red=0; 


for i=1:a
    for j=1:b
        x= impixel(im, i, j)/255 ;
        color= rgb2name (x);
        if (~isempty (strfind (color, 'yellow')))
            yellow= yellow+1; 
        elseif (~isempty (strfind(color, 'red')))
            red= red+1; 
        elseif (~isempty (strfind (color, 'blue')))
            blue= blue+1; 
        elseif (~isempty (strfind (color, 'white')))
           white= white+1; 
        else 
            %disp ('warning'); break; 
        end            
        disp (color);
        disp (i);
    end
end
disp (yellow)
disp (red)
disp (blue)
disp (white)

谢谢。

我觉得这个问题很有意思,如果回答的有点过分,提前致歉。简而言之,对于要将图像分割成离散颜色的问题,k-means 通常是正确的策略 space。但是,您的示例图像主要仅包含三种颜色,每种颜色在颜色 space 上都很好地分离,仅使用直方图即可轻松分割。请参阅下文以使用阈值进行分段。

您可以通过对每个矩阵求和来轻松获得像素数。例如,bCount = sum(blackPixels(:))

filename = '379NJ.png';
x = imread(filename); 
x = double(x); % cast to floating point
x = x/max(max(max(x))); % normalize

% take histogram of green dimension
g = x(:, :, 2);
c = hist(g(:), 2^8);

% smooth the hist count 
c = [zeros(1, 10), c, zeros(1, 10)];
N = 4;
for i = N+1:length(c) - N; 
   d(i - N) = mean(c(i -N:i)); 
end
d = circshift(d, [1, N/2]);

% as seen in histogram, the three colors fall nicely into 3 peaks
figure, plot(c, '.-');
[~, clusterCenters] = findpeaks(d, 'MinPeakHeight', 1e3);

% set the threshold halfway between peaks 
boundaries = [floor((clusterCenters(2) - clusterCenters(1))/2), ...
                 clusterCenters(2) + floor((clusterCenters(3) - clusterCenters(2))/2)];
thresh1 = boundaries(1)*ones(size(g))/255;
thresh2 = boundaries(2)*ones(size(g))/255;

% categorize based on threshold
blackPixels = g < thresh1;
greenPixels = g >= thresh1 & g < thresh2;
whitePixels = g >= thresh2;

这是我计算每个区域像素数的方法。鉴于此(如评论中所讨论):

  • 值 (RGB) 和颜色数 (K) 是先验已知的
  • 压缩伪像和抗锯齿生成了额外的颜色,必须将其视为 K 种已知颜色中的最近邻。

由于您先验地知道颜色,因此不需要 k-means。正如您的问题,它实际上可能会导致不良结果。 @crowdedComputeeer 的方法解决了这方面的问题。

您可以直接在像素值上使用 pdist2 计算最近邻。无需使用查找颜色名称的非常​​慢的函数。

这是代码。您只需修改变量 colors 即可更改颜色的数量和值。这将计算每种颜色的像素数,并输出蒙版。

img =  (imread('path_to_image'));

colors = [  0 0 0;    % black
            0 1 0;    % green
            1 1 1];   % white


% % You can change the colors        
% colors = [  0 0 1;    % red
%             1 1 0;    % yellow
%             1 0 0;    % blue
%             1 1 1];   % white


% Find nearest neighbour color
list = double(reshape(img, [], 3)) / 255;
[~, IDX] = pdist2(colors, list, 'euclidean', 'Smallest', 1);
% IDX contains the indices to the nearest element


N = zeros(size(colors, 1), 1);
for i = 1 : size(colors, 1)
    % Count the number of pixels for each color
    N(i) = sum( IDX == i );
end

% This will display the number of pixels for each color
disp(N);



% Eventually build the masks
indices = reshape(IDX, [size(img,1), size(img,2)]);

figure();
szc = size(colors,1);
for i = 1 : szc
    subplot(1,szc,i);
    imagesc(indices == i);
end

结果计数:

97554     % black
16894     % green
31852     % white

生成的掩码:

也许this project能帮上忙,请试试看。