如何在图像中聚集相同的颜色

时间:2013-08-28 08:38:28

标签: java image colors grouping

我有一个灰色的水平图像,想要找到特定颜色(例如黑色)更密集的区域。由于这个问题是一个更大项目的一部分,我需要用java编程,但我找不到合适的算法。

2 个答案:

答案 0 :(得分:1)

您应该使用K-Means在图像中查找群集。例如,你的数据集应该是 位置X,Y,Z和灰色值。

然后运行多个k值的算法。并使用BIC分数找到最佳的群集配置。

答案 1 :(得分:1)

我建议您使用 Matlab ,以便在图像处理方面迈出第一步。我写了一个生成伪随机图像的简单函数,然后我只考虑值超过阈值的像素。其余像素将设置为零。这是代码:

image=randi([0 255],10); %Used to generate a 10x10 image with pixel values between [0-255]

[row,col]=size(image); % to calculate the size of the image;
image_output=zeros([row,col]); %We make an empty image with the same size than the origial

TH_value=100; %threshold value 

index=find(image>TH_value); %searching for all the pixels with value>TH_value
n_idx=length(index); %number of pixels over TH_value

%no we have to replace in our empty new matrix only those pixels that have a value over the TH_value
for i=1:n_idx
    [row, col, ~ ] = ind2sub(size(image), index(i)); %geting the coordinates for all values over TH_value
    image_output(row,col)=image(row,col); %copying to the output matrix only the pixels over TH_value
end
image_output %in order to visualize the output image

将上面的代码粘贴到Matlab中,看看它是如何工作的。它很简单,但它可以作为一个开始。您应该使用“imread”函数导入自己的图像,而不是生成随机图像(请参阅代码的第一行)。