zoukankan      html  css  js  c++  java
  • Halcon选择一堆region中面积第N大的region的算法实现

    以下图为例:

    比如我想把面积第2小的那个“小正方形”选择出来,算法代码如下:

    1 read_image (Yuan, 'C:/Users/happy xia/Desktop/yuan.png')
    2 binary_threshold (Yuan, Region, 'max_separability', 'dark', UsedThreshold)
    3 connection (Region, ConnectedRegions)
    4 area_center (ConnectedRegions, Area, Row, Column)
    5 tuple_sort_index (Area, Indices)
    6 Num := |Indices|
    7 select_obj (ConnectedRegions, ObjectSelected, Indices[1] + 1)

    该实现算法的关键是对算子tuple_sort_index意思的理解。

    代码中:

    Area := [420, 12922, 38019, 58, 2033]

    Indices := [3, 0, 4, 1, 2]

    tuple_sort_index (Area, Indices)的意思是:先将Area中各元素按升序排序,然后将排序后的每一个Area分别在原Area元组中的索引放在元组Indices 中。

    [3, 0, 4, 1, 2]的意思是:

    58是Area 中的3号元素(元组索引从0开始)

    420是Area 中的0号元素;

    2033是Area 中的4号元素;

    12922是Area 中的1号元素;

    38019是Area 中的2号元素。

    再看看上面tuple_sort_index算子的意思,可知Indices[1]面积第2小的元素在原Area元组中的索引,即索引是0。由于对于算子select_obj 来说,它的索引是从1开始的。所以Indices[1] + 1 是最终的索引。

    结果图:

    如果要找到面积第二大的,只需将最后一句改为:

    select_obj (ConnectedRegions, ObjectSelected, Indices[Num -2] + 1)

  • 相关阅读:
    2020/11/4
    2020/11/10
    2020/11/12
    2020/11/5
    20201113 千锤百炼软工人
    20201112 千锤百炼软工人
    Java_swing_打开文件目录
    千锤百炼软工周报二
    9.29 课下动手动脑
    开学小测感想
  • 原文地址:https://www.cnblogs.com/xh6300/p/6417801.html
Copyright © 2011-2022 走看看