1. 程式人生 > >Halcon中循環的相關算子

Halcon中循環的相關算子

條件 rep str lse regions start operator center else

條件<condition> ,<condition> 內為計算成an integer or boolean value的表達式。技術分享圖片

表達式的值1則條件為真,否則為假。

1.if(<condition>)。。。 endif:條件為真時,執行條件後的內容,否則轉到endif.

2.if (<condition>)...else...endif:條件為真,執行if...else部分,否則執行else...endif,典型的選擇條件語句。

3.elseif相當於算子2的else部分,但是它允許測試一個附件條件。

例如:if (<condition1>)...elseif (<condition2>)...endif 當條件1為假條件2為真時,執行條件2後面的部分。在語法結構上等價於:if (<condition1>)
...
else
if (<condition2>)
...
endif
endif

循環控制語句

1.while (<condition>)... endwhile:循環控制結構,當條件為真時,執行循環體。可以利用operator continuebreak操作來重新開始或立刻終止循環。

2.repeat...until (<condition>):循環體至少被執行一次,直到滿足條件時退出。

3.for <index> := <start> to <end> by <step>...endfor

halcon最重要的循環結構,呵呵不細說了~同樣,可以利用operator continuebreak操作來重新開始或立刻終止循環。

例如:

old_x := 0
old_y := 0
dev_set_color (‘red‘)
dev_set_part(0, 0, 511, 511)
for x := 1 to 511 by 1
y := sin(x / 511.0 * 2 * 3.1416 * 3) * 255
disp_line (WindowID, -old_y+256, old_x, -y+256, x)
old_x := x
old_y := y
endfor

4.continue:強制執行下一個循環。

例如:i := |Images|
while (i)
Image := Images[i]
count_channels (Image, Channels)
if (Channels # 3)
continue
endif
* extensive processing of color image follows
endwhile

5.break:強制退出循環。

例1.

Number := |Regions|
AllRegionsValid := 1
* check whether all regions have an area <= 30
for i := 1 to Number by 1
ObjectSelected := Regions[i]
area_center (ObjectSelected, Area, Row, Column)
if (Area > 30)
AllRegionsValid := 0
break ()
endif
endfor

例2.

while (1)
grab_image (Image, FGHandle)
dev_error_var (Error, 1)
dev_set_check (‘~give_error‘)
get_mposition (WindowHandle, R, C, Button)
dev_error_var (Error, 0)
dev_set_check (‘give_error‘)
if ((Error = H_MSG_TRUE) and (Button # 0))
break ()
endif
endwhile

其他

1.stop:終止後面的循環,點擊Step Over or Run button繼續。

2.exit:終止Hdevelop程序段。

3.return:從當前的程序返回到正在呼叫的算子。

4.try ... catch ... endtry:異常算子處理句柄

5.throw:允許處理用戶定義的意外情況。

Halcon中循環的相關算子