gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:41:00 25 4
gpt4 key购买 nike

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

enter image description here

我希望 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');

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

enter image description here

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

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

谁能告诉我如何修复上面的代码以获得所需的结果?

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

编辑:这是我编写的用于迭代图像中每个像素的代码。请注意,我使用了红、黄、蓝、白 4 种颜色,而不是绿、白、黑,但想法是一样的。 rgb2name 是返回给定 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 均值通常是正确的策略。但是,您的示例图像主要仅包含三种颜色,每种颜色在颜色空间中都很好地分离,仅使用直方图即可轻松分割。请参阅下文了解使用阈值进行分段。

您可以通过对每个矩阵求和来轻松获得像素数。例如,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;

Image segmentation

关于matlab - 使用 K 均值聚类准确检测图像中的颜色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com