机器视觉 for算子

Posted 沧海一笑-dj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器视觉 for算子相关的知识,希望对你有一定的参考价值。

00. 目录

01. 概述

for — 启动一个循环块,通常执行固定次数的迭代。

02. 签名

for( : : Start, End, Step : Index)

03. 描述

在HDevelop中的语法:for Index := Start to End by Step。

for语句启动一个通常是运行固定次数的迭代的循环分段。 for分段结束于相应的endfor语句。

迭代次数由Start值,End值和Step值共同决定。 所有这些参数都不一定是常量值,可以用表达式或变量初始化替代。 请注意,这些循环参数只被计算一次,即在for循环输入之前。 它们在循环之后不被重新计算,即在循环内对这些变量的任何修改都不会影响迭代次数。

传递的循环参数必须是整型或实型。 如果所有输入参数都是整数类型的,那么Index变量也是整型。 在所有其他情况下,Index变量将是real类型的。

在每次迭代开始时,将循环变量Index与End参数进行比较。 如果Step为正,则只要Index变量小于或等于End参数,就会运行for循环。 如果Step为负,则只要Index变量大于或等于End参数,就会执行for循环。

注意:如果将Step设置为real类型的值,则在Index变量预期与上一个周期中的End值完全匹配的情况下,可能会因为四舍五入误差而忽略最后一个循环。 因此,在一些系统中,下面的循环没有按照预期的那样运行了四次(Index变量设置为1.3,1.4,1.5和1.6),只运行了三次,因为在三次加法之后,索引变量由于四舍五入的误差而略大于1.6。

  I:=[]
  for Index := 1.3 to 1.6 by 0.1
    I := [I,Index]
  endfor

在循环内容的运行之后,即在到达相应的endfor语句或者continue语句之后,循环计数器Index的当前值会加上Step(在for循环的开始处被初始化)。 然后,如上所述重新判断循环条件。 根据结果,循环要么被重新运行,要么运行完毕。运行完毕时,将会继续运行相应的endfor语句之后的第一个语句。

循环中的存在break语句—没有被更多的内部分段所覆盖(that is not covered by a more internal block)—立即离开循环,并在相应的endfor语句之后继续运行。 与之相反的是,continue语句用于忽略当前循环中其余的循环内容,并继续执行索引变量并重新判断循环条件。

注意:在HALCON 11中,for循环相对于Index变量的行为发生了变化:在以前的版本中,循环体内的Index变量的变化被for语句忽略。 在判断循环条件之前,Index变量将被重置为一个内部计数器,计数器的计算公式如下:

Index := Start + count * Step

count是已经运行的循环次数。

使用HALCON 11之前的HALCON版本保存的程序和程序(program and procedure)在加载时通过分析它们的for循环保持不变地运行:如果在循环体内修改了这样的for循环的Index变量,则循环被设置为兼容模式并会进行相应的标注。所有以这种方式标记的循环都是使用旧的语义执行的,即在每个循环周期之前将Index变量重置为内部计数器以保持旧的行为。在程序列表中,for语句由一个警告三角形和行尾的标签__use_internal_index__标记。另外,在算子窗口中也会显示警告。check box允许取消所选for循环的兼容模式,并使用新的语义执行。在全文编辑器中删除标签__use_internal_index__具有相同的效果。但是,为了保持程序行为,必须通过将循环体开头的Index变量复制到局部变量来稍微调整for循环体。这个局部变量可以在循环体内随意使用和修改。

在任何情况下,建议避免在其内部修改for循环的Index变量,因为代码变得难以调试,代码将不兼容HALCON 11之前的HALCON版本。

如果for循环停止,例如通过停止语句或按下停止按钮,并且如果用户手动放置PC(程序计数器),则只要PC还在for循环内,for循环在当前迭代中继续或者跳到endfor语句。 如果PC放置在在for语句中(或之前)并再次执行,则循环将重新初始化并在开始时重新启动。

原文描述

Syntax in HDevelop: for Index := Start to End by Step

The for statement starts a loop block that is usually executed for a fixed number of iterations. The for block ends at the corresponding endfor statement.

The number of iterations is defined by the Start value, the End value, and the increment value Step. All of these parameters can be initialized with expressions or variables instead of constant values. Please note that these loop parameters are evaluated only once, namely, immediately before the for loop is entered. They are not re-evaluated after the loop cycles, i.e., any modifications of these variables within the loop body will have no influence on the number of iterations.

The passed loop parameters must be either of type integer or real. If all input parameters are of type integer, the Index variable will also be of type integer. In all other cases the Index variable will be of type real.

At the beginning of each iteration the loop variable Index is compared to the End parameter. If the increment value Step is positive, the for loop is executed as long as the Index variable is less than or equal to the End parameter. If the increment value Step is negative, the for loop is executed as long as the Index variable is greater than or equal to the End parameter.

Attention: If the increment value Step is set to a value of type real, it may happen that the last loop cycle is omitted owing to rounding errors in case the Index variable is expected to match the End value exactly in the last cycle. Hence, on some systems the following loop is not executed—as expected—for four times (with the Index variable set to 1.3, 1.4, 1.5, and 1.6), but only three times because after three additions the index variable is slightly greater than 1.6 due to rounding errors.

I:=[]
for Index := 1.3 to 1.6 by 0.1
I := [I,Index]
endfor
After the execution of the loop body, i.e., upon reaching the corresponding endfor statement or a continue statement, the increment value (as initialized at the beginning of the for loop) is added to the current value of the loop counter Index. Then, the loop condition is re-evaluated as described above. Depending on the result the loop is either executed again or finished in which case execution continues with the first statement after the corresponding endfor statement.

A break statement within the loop—that is not covered by a more internal block—leaves the loop immediately and execution continues after the corresponding endfor statement. In contrast, the continue statement is used to ignore the rest of the loop body in the current cycle and continue execution with adapting the Index variable and re-evaluating the loop condition.

Attention: The behavior of the for loop with respect to the Index variable has changed in HALCON 11: In previous versions changes to the Index variable within the loop body were ignored by the for statement. Before evaluating the loop condition the Index variable would be reset to an internal counter that was calculated by:

Index := Start + count * Step
where count was the number of already executed cycles.

Programs and procedures that were saved with a HALCON version prior to HALCON 11 are kept running unmodified by analyzing their for loops while loading: If the Index variable of such a for loop is modified within the loop body, the loop is set into a compatibility mode and marked accordingly. All for loops that are marked in such a way are executed using the old semantics, i.e., the Index variable is reset to the internal counter before each loop cycle to keep the old behavior. In the listing the for statement is marked by a warning triangle and the label use_internal_index at the end of the line. In addition, in the operator window a warning is displayed. A check box allows to cancel the compatibility mode for the selected for loop and perform it with the new semantics. Deleting the label use_internal_index in the full text editor has the same effect. However, to keep the program behavior, the body of the for loop must slightly be adapted by copying the Index variable at the begin of the loop body to a local variable. This local variable can be used and modified within the loop body at will.

In any case it is recommended to avoid modifying the Index variable of the for loop within its body because the code becomes harder to debug and the code will not be compatible to HALCON versions prior to HALCON 11.

If the for loop is stopped, e.g., by a stop statement or by pressing the Stop button, and if the PC is placed manually by the user, the for loop is continued at the current iteration as long as the PC remains within the for body or is set to the endfor statement. If the PC is set on the for statement (or before it) and executed again, the loop is reinitialized and restarts at the beginning.

04. 注意

05. 参数

Start (input_control)   number → (integer / real)
循环变量的起始值。
默认值: 1

End (input_control)    number → (integer / real)
循环变量的结束值。
默认值: 5

Step (input_control)    number → (integer / real)
循环变量的增量值。
默认值: 1

Index (output_control)    number → (integer / real)
循环变量

06. 结果

如果指定参数的值是正确的,for(作为算子)返回2(H_MSG_TRUE)。 否则,会引发异常并返回错误代码。

HDevelop例程

match_function_trans.hdev Calculate transformation parameters between two functions
fuzzy_measure_pin.hdev Measure pins of an IC using fuzzy measuring
for.hdev Use a for loop to iterate over extracted blobs
assign.hdev Assign values to variables and tuple elements

程序示例

read_image (Image, 'fabrik')
threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
area_center (SelectedRegions, Area, Row, Column)
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)
dev_display (SelectedRegions)
dev_set_color ('white')
for Index := 0 to |Area| - 1 by 1
  set_tposition (WindowHandle, Row[Index], Column[Index])
  write_string (WindowHandle, 'Area=' + Area[Index])
endfor

07. 附录

7.1 机器视觉博客汇总
网址:https://dengjin.blog.csdn.net/article/details/116837497

以上是关于机器视觉 for算子的主要内容,如果未能解决你的问题,请参考以下文章

机器视觉 exit算子

机器视觉 switch算子

机器视觉 export_def算子

机器视觉 insert算子(已废弃)

机器视觉 stop算子

机器视觉 endwhile算子