如何在matlab中基于颜色精确地排序聚类 [英] How to accurately order the clusters based on color in matlab
问题描述
我有一张叶子的图像,主要有三种颜色的黑色背景,绿色的叶子和棕色的病斑。
这是图像
当我集群它第一次,我得到棕色斑点在集群1,绿色部分在集群2,黑色区域在集群3(例如)。
,我得到群集1中的绿色部分,群集2中的褐色斑点,群集3中的黑色区域(例如)。 b
当我第三次集群时,集群的顺序会再次不同。
代码,使得棕色斑点出现在群集1,绿色部分群集2和黑色的群集3.群集的顺序应该是相同的,即使我群集多次。有人可以帮助我的代码吗?我使用Matlab2009a。
<$ c $ c> function segmented_img = leaf_segmentation(original_img,nclusters)
original_img = im2double(original_img);
G = fspecial('gaussian',[200 250],1);
smoothed_img = imfilter(original_img,G,'same');
conversionform = makecform('srgb2lab');
lab_img = applycform(smoothed_img,conversionform);
ab_img = double(lab_img(:,:,2:3));
nrows = size(ab_img,1);
ncols = size(ab_img,2);
ab_img = reshape(ab_img,nrows * ncols,2);
cluster_idx =
kmeans(ab_img,nclusters,'distance','sqEuclidean','Replicates',3);
cluster_img = reshape(cluster_idx,nrows,ncols);
segmented_img = cell(1,nclusters);
for k = 1:nclusters
segmented_img {k} = bsxfun(@times,original_img,cluster_img == k);
end
end
segmented = leaf_segmentation(imread('input image'),3);
figure,imshow(segmented {1}),title('Cluster 1');
figure,imshow(segmented {2}),title('Cluster 2');
figure,imshow(segmented {3}),title('Cluster 3')
只要在聚类后检查颜色!
这样做的一种方法是将质心颜色空间转换为HSV,并检查H和V的值.H会给你颜色(例如,它的绿色约为120.0度)和V将给你ligth,所以如果V是0,那么无论什么H,它是黑色的簇。
这对于程序来说应该是微不足道的,但不要犹豫要问任何问题。
I have an image of leaf that has mostly three colors black background, green leaf and brown diseased spots. Here is the image
When I cluster it first time, I get brown spots in cluster 1, green portion in cluster 2, black region in cluster 3(for example).
When I cluster it second time, I get green portion in cluster 1,brown spots in cluster 2, black region in cluster 3(for example). b
When I cluster it third time the order of clusters are different again.
I want to change the code such that the brown spots appear in cluster 1, green portion in cluster 2 and black in cluster 3. The order of clusters should be same even if I cluster many times.Could someone please help me with the code? I am using Matlab2009a. This question is about ordering clusters
Here is what is being done so far
function segmented_img = leaf_segmentation( original_img, nclusters )
original_img = im2double(original_img);
G=fspecial('gaussian',[200 250],1);
smoothed_img =imfilter(original_img,G,'same');
conversionform = makecform('srgb2lab');
lab_img = applycform(smoothed_img,conversionform);
ab_img = double(lab_img(:,:,2:3));
nrows = size(ab_img,1);
ncols = size(ab_img,2);
ab_img = reshape(ab_img,nrows*ncols,2);
cluster_idx =
kmeans(ab_img,nclusters,'distance','sqEuclidean','Replicates',3);
cluster_img = reshape(cluster_idx,nrows,ncols);
segmented_img = cell(1,nclusters);
for k = 1:nclusters
segmented_img{k} = bsxfun( @times, original_img, cluster_img == k );
end
end
segmented = leaf_segmentation( imread('input image'), 3 );
figure,imshow(segmented{1}), title('Cluster 1');
figure, imshow(segmented{2}), title('Cluster 2');
figure, imshow(segmented{3}), title('Cluster 3');
Just check the colors after the clustering!
One way of doing this is transforming the centroids color space to HSV and checking the values oh H and V. H will give you the color (for example, around 120.0 degrees its green) and V will give you the "ligth", so if V is 0 then whatever H is, it is the black cluster.
This should be trivial to program, but dont hesitate to ask any questions about it.
这篇关于如何在matlab中基于颜色精确地排序聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!