算子sort_contours_xld算子有5種排序方式,即:
'upper_left':
The position is determined by the upper left corner of the surrounding rectangle.
'upper_right':
The position is determined by the upper right corner of the surrounding rectangle.
'lower_left':
The position is determined by the lower left corner of the surrounding rectangle.
'lower_right':
The position is determined by the lower right corner of the surrounding rectangle.
'character':
The position is determined by the upper left corner of the surrounding rectangle. In contrast to 'upper_left', the contours are also sorted according to the remaining coordinate, if they overlap in the direction of the coordinate which is specified by the parameter RowOrCol.
其中'character'排序方式不太好理解,不過從英文說明可知,它跟'upper_left'排序方式很像,我想一般來說,前面的四種排序方式一般夠用了。
以'upper_left'排序方式為例,它的英文說明的翻譯是:位置由環繞矩形的左上角決定。
於是我猜測它的意思是這樣的:對於一組xld來說,畫出每一個xld的最小平行矩形(gen_rectangle1),然后根據這些矩形的左上角坐標來對這些xld進行排序。
先看算子簽名:
sort_contours_xld(Contours : SortedContours : SortMode, Order, RowOrCol : )
Order可以取'true'或者'false','true'是升序排列,'false'是降序排列。
RowOrCol 可以取'row'或者'column'。
下面以如下方式排列試試:(根據最小外接矩形的左上角的行坐標來升序排列)
sort_contours_xld (SelectedXLD, SortedContours, 'upper_left', 'true', 'row')
1 read_image (Image, 'C:/Users/happy xia/Desktop/dynPic.png') 2 threshold_sub_pix (Image, Border, 90) 3 select_shape_xld (Border, SelectedXLD, 'contlength', 'and', 100, 99999) 4 sort_contours_xld (SelectedXLD, SortedContours, 'upper_left', 'true', 'row') 5 dev_set_draw ('margin') 6 dev_display (Image) 7 count_obj (SortedContours, Number) 8 gen_empty_obj (EmptyObject) 9 *注釋:這里Number = 29 10 for Index := 1 to Number by 1 11 select_obj (SortedContours, ObjectSelected, Index) 12 smallest_rectangle1_xld (ObjectSelected, Row1, Column1, Row2, Column2) 13 gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) 14 concat_obj (EmptyObject, ObjectSelected, EmptyObject) 15 endfor 16 17 dev_display (Image) 18 select_obj (SortedContours, ObjectSelected1, 11) 19 sort_contours_xld (SelectedXLD, SortedContours2, 'upper_left', 'false', 'row') 20 select_obj (SortedContours2, ObjectSelected2, Number - 11 + 1)
程序中變量ObjectSelected出現的順序依次如下:(可以觀察ObjectSelected的外接矩形的row坐標,看是否符合之前總結的規律)
對於排序后的xld元組SortedContours,我們可以發現它的升序排列第11項和降序排列第(29-11+1)項是同一個對象,都是字母W。
有一個問題值得討論一下:'upper_right', 'true', 'row'和'upper_left', 'true', 'row'有何區別?
我反復試了一下,結論是:沒有區別。因為根據行(row)排列時,矩形的左上角和右上角的row值是一樣的。
如果是根據row升序排列,如果row一樣,那么再根據column升序排列;如果是根據row降序排列,如果row一樣,那么再根據column降序排列。
至於其他幾種排序方式,規律是一樣的,無需贅言。
不知道看到這里的網友們懂了沒有,反正我自己是搞懂了,要想徹底搞懂,很簡單,用我提供的圖和程序,反復變換排序的方式,跑一跑程序便知。