这段代码是一个使用Halcon语言编写的脚本,它演示了如何使用 distance_contours_xld 和 apply_distance_transform_xld 这两个操作符来计算两个轮廓之间的点对点距离。

dev_close_window ()
dev_open_window_fit_size (0, 0, 1280, 1024, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
*

  • Show introduction
    Message := ‘This example shows the speed difference between the operators’
    Message[1] := ‘distance_contours_xld and apply_distance_transform_xld.’
    Message[2] := ‘Both operators measure the pointwise distance between’
    Message[3] := ‘two contours.’
    Message[4] := ’ ’
    Message[5] := ‘While distance_contours_xld can always be used,’
    Message[6] := ‘apply_distance_transform_xld is used if one of the contours’
    Message[7] := ‘is reused many times as an unchanging reference contour.’
    Message[10] := ‘In that case some time-consuming calculations can be done’
    Message[11] := ‘offline in advance so that apply_distance_transform_xld is’
    Message[12] := ‘much faster.’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘white’, ‘false’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • Choose one of two modes.
  • ‘point_to_point’ is faster,
  • ‘point_to_segment’ is more accurate.
    Mode := ‘point_to_point’
  • Mode := ‘point_to_segment’
    Tolerance := 7
    Loops := 4
    Legend := [‘Legend:’,‘- template contour’,‘- random test contour within tolerance’,‘- random test contour outside of tolerance’]
  • PART I: distance_contours_xld
  • distance_contours_xld calculates
  • the pointwise distance between two contours

for I := 1 to Loops by 1
* Generate the two contours to be compared
gen_test_contours (EdgeOriginal, EdgeWarped)
* Calculate pointwise distance.
* The distances from points of the first to the points of the second contour
* are stored in the ‘distance’ attribute of the output contour.
* Parts of the contour can be easily segmented based on the attributes.
count_seconds (S1)
distance_contours_xld (EdgeWarped, EdgeOriginal, EdgeWarpedWithDistances, Mode)
count_seconds (S2)
get_contour_attrib_xld (EdgeWarpedWithDistances, ‘distance’, Distance)
segment_contour_attrib_xld (EdgeWarpedWithDistances, ContourPart, ‘distance’, ‘and’, Tolerance, 99999)
*
display_result (EdgeOriginal, EdgeWarped, ContourPart)
Message := ‘Part 1: Calculate distances with distance_contours_xld’
Message[1] := ‘Execution time: ’ + ((S2 - S1) * 1000)$’.2f’ + ’ ms’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
disp_message (WindowHandle, Legend, ‘image’, 800, 12, [‘grey’,‘white’,‘green’,‘red’], ‘false’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endfor
*

  • PART II: apply_distance_transform_xld
  • apply_distance_transform_xld is used to calculate the
  • pointwise distance to changing contours to a not
  • changing reference contour.
  • To accelerate the calculation, some information is
  • pre-calculated and stored in a distance transform handle.

dev_clear_window ()
disp_message (WindowHandle, ‘Please wait while pre-calculating distance transform…’, ‘window’, 42, 12, ‘white’, ‘false’)
*

  • Pre-calculate distance transform for the template contour
  • (this may take a while, depending on the size of the contour
  • and MaxDistance)
  • The parameter MaxDistance is used to restrict the pre-calculated
  • distance transform to the specified value. Distances larger than
  • MaxDistance will not be calculated. Instead, the distances are
  • clipped.
    MaxDistance := 8
    create_distance_transform_xld (EdgeOriginal, Mode, MaxDistance, DistanceTransformID)
    for I := 1 to Loops by 1
    gen_test_contours (NotUsedOriginal, TestContour)
    count_seconds (S1)
    apply_distance_transform_xld (TestContour, TestContourOut, DistanceTransformID)
    count_seconds (S2)
    • Note, that the calculated distances are clipped at MaxDistance
      get_contour_attrib_xld (TestContourOut, ‘distance’, DistanceClipped)
      segment_contour_attrib_xld (TestContourOut, ContourPart, ‘distance’, ‘and’, Tolerance, MaxDistance)
    display_result (EdgeOriginal, TestContour, ContourPart)
    Message := ‘Part 2: Calculate distances with apply_distance_transform_xld’
    Message[1] := ‘Execution time: ’ + ((S2 - S1) * 1000)$’.2f’ + ’ ms’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
    disp_message (WindowHandle, Legend, ‘image’, 800, 12, [‘grey’,‘white’,‘green’,‘red’], ‘false’)
    if (I < Loops)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
    endif
    endfor
    stop ()
    clear_distance_transform_xld (DistanceTransformID)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以下是代码的主要功能和步骤:

窗口初始化:使用 dev_open_window_fit_size 打开一个窗口,并设置字体和颜色。

显示介绍信息:使用 disp_message 显示一段介绍信息,解释了 distance_contours_xld 和 apply_distance_transform_xld 两个操作符的区别和用途。

模式选择:可以选择两种模式之一:point_to_point(速度更快)或 point_to_segment(更准确)。

容差和循环次数设置:设置容差 Tolerance 和循环次数 Loops。

生成测试轮廓:使用 gen_test_contours 函数生成要比较的两个轮廓。

PART I: 使用 distance_contours_xld:

使用 distance_contours_xld 计算两个轮廓之间的点对点距离。
将距离存储在输出轮廓的 distance 属性中。
使用 segment_contour_attrib_xld 根据距离属性对轮廓进行分段。
显示结果:使用 display_result 函数显示原始轮廓、变形轮廓和分段轮廓。

PART II: 使用 apply_distance_transform_xld:

首先,使用 create_distance_transform_xld 为模板轮廓预计算距离变换。
然后,使用 apply_distance_transform_xld 计算变化轮廓到不变参考轮廓的点对点距离。
注意,计算的距离在 MaxDistance 处被截断。
显示结果:同样使用 display_result 函数显示结果。

清理资源:使用 clear_distance_transform_xld 清理预计算的距离变换资源。

这段代码的目的是展示两种不同方法计算轮廓之间距离的速度差异,以及如何根据需要选择使用哪种方法。通过比较执行时间,用户可以了解在不同情况下哪种方法更适合他们的需求。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐