【问题标题】:Group numbers in a certain way in MATLAB在 MATLAB 中以某种方式对数字进行分组
【发布时间】:2017-11-19 00:48:26
【问题描述】:
我在未使用 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 的标签。
【问题讨论】:
标签:
image
matlab
image-processing
computer-vision
connected-components
【解决方案1】:
您似乎想要计算图形的连通分量。这可以在 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