Halcon選擇一堆region中面積第N大的region的算法實現

如下圖爲例:算法

好比我想把面積第2小的那個「小正方形」選擇出來,算法代碼以下:app

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意思的理解。spa

 

代碼中:code

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

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

 

tuple_sort_index (Area, Indices)的意思是:先將Area中各元素按升序排序,而後將排序後的每個Area分別在原Area元組中的索引放在元組Indices 中。索引

 

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

58是Area 中的3號元素(元組索引從0開始)io

420是Area 中的0號元素;class

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)

相關文章
相關標籤/搜索