以下图为例:

比如我想把面积第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)

 

posted on 2017-02-19 23:23  xh6300  阅读(11941)  评论(0编辑  收藏  举报