我正在进行图像分割,测试许多不同的分割算法,以便进行比较研究。目前我已经实现了k-means算法。我想自动计算图像中分割的对象(每个图像上的粉红色和绿色)。我有专家手动计数,所以我想比较结果。
我的输入图片是:
我的结果图片是:
我有什么方法可以自动化这个过程吗?
你能帮帮我吗?
提前多多感谢。约翰
答案 0 :(得分:3)
这两个例子中缺少的部分是计算集群。获取簇数的关键是使用label函数的输出。标签功能将所有连接的像素组标记为1到NUMBER_OF_LABELS,因此使用max(labels(:))
与计算我们所有的簇相同
我处理了两个完全相同的集群,通常我不会保留所有中间变量,它们只是在这里,所以你可以在每次操作后看到输出
基本大纲
- 获取二元掩码(您可以自定义)
- 填写任何漏洞
- 删除小区域(您可以自定义)
- 标记所有区域(这基本上是连接组件分组)
- 计算区域
醇>
matlab代码
%% creates binary masks for each cluster
cluster1 = imread('cluster1.png');
cluster1 = rgb2gray(cluster1);
cluster2 = imread('cluster2.png');
cluster2 = rgb2gray(cluster2);
%im not sure how you found these clusters 1/2, but using that original mask is
%better than using this 'graythresh' Since I didn't have your original
%clustering output I tried to reconstruct it w
bw1 = im2bw(cluster1,graythresh(cluster1));
bw2 = im2bw(cluster2,graythresh(cluster2));
%alternate method for constructing binary images
%my_gray_thresh = 0
%bw1 = cluster1 > my_gray_thresh;
%bw2 = cluster2 > my_gray_thresh;
%% the above could be replaced with the output of your clustering alg
%tries to clean up the image a bit, filling in holes
fill_bw1 = imfill(bw1,'holes');
fill_bw2 = imfill(bw2,'holes');
%takes out regions with less than smallest_area_for_dots
smallest_area_for_dots = 5; %you can play with this parameter
large_bw1 = bwareaopen(fill_bw1, smallest_area_for_dots);
large_bw2 = bwareaopen(fill_bw2, smallest_area_for_dots);
%labels the regions, this is essentially connected component analysis
labels1 = bwlabel(large_bw1);
labels2 = bwlabel(large_bw2);
%plots the labeled image
figure(1)
subplot(2,3,[1 4]);imshow(cluster1);title('original image (grayscale)')
subplot(2,3,2);imshow(bw1);title('bw after threshold')
subplot(2,3,3);imshow(fill_bw1);title('after fill')
subplot(2,3,5);imshow(large_bw1);title('after removing small clusters')
subplot(2,3,6);imshow(label2rgb(labels1));title('labeled clusters')
figure(2)
subplot(2,3,[1 4]);imshow(cluster2);title('original image (grayscale)')
subplot(2,3,2);imshow(bw2);title('bw after threshold')
subplot(2,3,3);imshow(fill_bw2);title('after fill')
subplot(2,3,5);imshow(large_bw2);title('after removing small clusters')
subplot(2,3,6);imshow(label2rgb(labels2));title('labeled clusters')
%the number of figures is equal to the number of labels. since labels are
%numbered 1 to NUM_LABELS doing max will get us the number of dots
num_in_cluster1 = max(labels1(:))
num_in_cluster2 = max(labels2(:))
和matlab桌面输出
num_in_cluster1 = 243
num_in_cluster2 = 51
和结果图像