如何合并连接的组件标签中的标签 [英] How do I merge the labels in connected component labeling

查看:213
本文介绍了如何合并连接的组件标签中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我必须找出在图像中有多少物体。

Hi I have to find out how many objects I have in an image.

http://en.wikipedia.org/wiki/Connected-component_labeling

我需要帮助存储邻居之间的等价和第二遍。这个传递给我173个左右的对象,是第一次通过。我想存储等价(当它们发生时),然后在第二遍,以便能够用最低的等价值替换相应的等价。

I need help with storing the equivalence between neighbours and also the second pass. This pass gives me 173 or so objects and is the first pass. I would like to store equivalences (when they occur) and then in the second pass to be able to just replace the corresponding equivalences with the lowest equivalent value.

推荐答案

等价表可以使用 HashMap 来实现。每次找到等价于另一个标签的标签时,只需将该关系添加到散列图。

The equivalence table can be implemented using a HashMap. Each time you find a label that is equivalent to another label, just add that relation to the hash map.

final Map<Integer, Integer> equivalenceTable = new HashMap<>();

因此,只要找到两个相等的标签,只需将它们放在等价表中即可。

So whenever you find two labels that are equal, just put them in the equivalence table.

private void storeEquivalence(final Integer label0, final Integer label1, final Map<Integer, Integer> table) {
  if (table.keySet().contains(label0)) {
    table.put(label1, table.get(label0));
  }
  else if (table.keySet().contains(label1)) {
    table.put(label0, table.get(label1));
  }
  else {
    table.put(label0, label1);
  }
}

区域[x-1] [y] 和 region [x] [y-1] (区域[x-1] [y]),并更新等价表(如果是,则通过调用

So when you identify that region[x-1][y] and region[x][y-1] are equal, you should check if the labels are different (they should be) and update the equivalence table if the are by calling

 storeEquivalence(region[x-1][y], region[x][y-1], equivalenceTable);

然后在第二遍中,只需替换在等价表中有值的每个标签。 p>

Then in the second pass you simply replace each label that has a value in the equivalence table.

for (int x = 1; x < imageTwo.getWidth(); x++) {
  for (int y =1; y < imageTwo.getHeight(); y++) {
    if (equivalenceTable.keySet().contains(region[x][y])) {
      region[x][y] = equivalenceTable.get(region[x][y]);
    }  
  }
}

这篇关于如何合并连接的组件标签中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆