少有人走的路

勇哥的工业自动化技术网站

Halcon选择一堆region中面积第N大的region的算法实现

以下图为例:

image.png

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

read_image (Yuan, 'C:/Users/happy xia/Desktop/yuan.png')
binary_threshold (Yuan, Region, 'max_separability', 'dark', UsedThreshold)
connection (Region, ConnectedRegions)
area_center (ConnectedRegions, Area, Row, Column)
tuple_sort_index (Area, Indices)
Num := |Indices|
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 是最终的索引。

 

结果图:

image.png

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

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

image.png

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2026年1月    »
1234
567891011
12131415161718
19202122232425
262728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864