繁体   English   中英

在MATLAB中以某种方式分组编号

[英]Group numbers in a certain way in MATLAB

我没有在Matlab中使用计算机视觉工具箱,而是在图像中找到连接的组件。

在给定图像的第一次扫描中,我为每个像素分配了标签。 我还创建了一个表,它为我提供了彼此连接的组件列表。 该表如下所示:

1   5
1   5
2   4
3   5
8   5
2   6

该表表示标记为1和5的所有像素都连接,2和4连接,3和5连接,依此类推。 此外,由于1和5连接,3和5也连接,这意味着1和3也连接。

我想找到一种方法将所有这些组件组合在一起。 有什么办法吗? 或者某种方式,每个连接的像素获得与具有最小值的标签等同的标签,即如果连接4,7和12个像素,则具有标签4,7和12的所有像素应该获得值为4的标签。

您似乎想要计算图表的连接组件。 这可以在MATLAB中相当容易地完成,无需任何额外的工具箱。

e = [1   5; ...
     1   5; ...
     2   4; ...
     3   5; ...
     8   5; ...
     2   6];
% MATLAB doesn't support repeated edges so remove them
e = unique(sort(e,2),'rows');
% create a graph
G = graph(e(:,1),e(:,2));
% plot the graph (optional)
plot(G);
% find connected components
bins = conncomp(G);
% get the indicies of nodes in each cluster
cluster = arrayfun(@(v)find(bins==v),1:max(bins),'UniformOutput',false);

结果

在此输入图像描述

>> cluster{1}
ans = 1     3     5     8
>> cluster{2}
ans = 2     4     6
>> cluster{3}
ans = 7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM