对于halcon中的例子set_shape_model_timeout,基于形状的模板匹配简要分析
* This example demonstrates the usage of the shape based matching timeout
* mechanism integrated in HALCON.
* To enforce varying runtimes, artificial clutter is added to
* the image containing the pattern.
*
* ********************** ATTENTION! *********************
* The timeout mechanism throws exceptions!
* That implies, that HDevelop will open an error dialog,
* unless 'Suppress error message dialog' is checked within
* Edit -> Preferences -> General Options -> Experienced User.
* To get meaningful runtime measurements,
* make sure that the error messages are suppressed.
* *********************************************************
*
/* 这个例子演示了在HALCON中集成的基于形状的匹配超时机制的用法
为了执行不同的运行时,在包含模式的图像中添加了人工杂波
********************ATTENTION!**********************
超时机制抛出异常!
这意味着,HDevelop将打开一个错误对话框,除非在Edit -> Preferences -> General Options ->资深用户中选中“禁止错误消息对话框”。
要获得有意义的运行时度量,请确保抑制了错误消息。*/
dev_update_off ()
// 此过程将'dev_update_pc, dev_update_var和 dev_update_window' 设置为'off '
dev_close_window ()
// 关闭当前活动图形窗口
* Suppress the opening of error message dialogs
// 禁止打开错误的消息对话框
dev_get_preferences ('suppress_handled_exceptions_dlg', SuppressExceptionSetting)
/* 获取Hdevelop系统的参数
PreferenceNames:输入参数名称
'suppress_handled_exceptions_dlg':禁止外部对话框
PreferenceValues:输入参考值
SuppressExceptionSetting:抑制Excoption的设置 */
dev_set_preferences ('suppress_handled_exceptions_dlg', 'true')
/* 设置HDevelope系统的参数
'suppress_handled_exceptions_dlg'
算子输入窗口或全文编辑中按下回车仿真的行为,默认' false',控制是否应该抑制错误对话框,该对话框默认是开启的,在程序运行期间某一程序行发生错误并且由try-catch块封闭,有此可以由异常处理程序处理,对话框也会默认打开,该选项永久存储HDevelop.ini文件中,也可以通过Preference dialog/ General Option/ Experience Users进行配置,如果一个没有被try-catch 包围的程序行引发异常,则总是打开一个错误消息的对话框 */
* Define that the shape model to be found may not
* partially lie outside the image
// 定义要找到的形状模型不能部分位于图像之外
get_system ('border_shape_models', BorderShapeModels)
set_system ('border_shape_models', 'false')
/* get 根据HALCON系统参数获取关于当前的信息
Query 输入需要质疑的参数
'border_shape_models' 边界形状模型
Information 输出系统参数的对应值
set HALCON系统参数的设置 */
* Read the model image
// 读取模板图像
read_image (Image, 'coins/20cent_italian.png')
/* 读取一个图像文件
Image 输出一个被读取的图像
Filename 输入图像的文件名称 */
dev_open_window_fit_image (Image, 0, 0, [600,800], [400,600], WindowHandle)
// 在坐标为(0,0)处打开一个图像窗口,其宽和高分别是【600,800】,【400,600】,窗口的句柄为WindowHandle
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
// 设置窗口中显示的字体颜色,大小,加粗,倾斜
dev_set_line_width (2)
// 设置一个region,图形或轮廓的线宽,可在菜单--可视化--线宽中选择自己所需要的线宽
// LIneWidth:设置线宽,1-50不等
* Display warning
Warning := 'ATTENTION!'
Warning[1] := 'This examples throws exceptions to demonstrate'
Warning[2] := 'timeouts!'
Warning[3] := 'To get meaningful runtime measurements make sure that'
Warning[4] := 'the error messages are suppressed in'
Warning[5] := 'Edit / Preferences / General Options / Experienced User'
// 显示警告
disp_message (WindowHandle, Warning, 'window', 12, 12, 'red', 'true')
// 显示消息
disp_continue_message (WindowHandle, 'black', 'true')
// 显示继续消息,等待用户按F5继续执行
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
stop ()
* Create the shape model
// 创建形状模板
gen_circle (ModelRegion, 245.5, 353.5, 145)
/* 创建一个圆周区域
Circle 输出一个圆形区域
Row 输入该圆中心点的y值坐标,1.0<Row<1024.0
Column 输入该圆中心点的x值坐标,1.0<Column<1024.0
Redius 输入该圆的半径,1.0<Redius <1024.0 */
reduce_domain (Image, ModelRegion, TemplateImage)
/* 缩小一个图像的区域,新图像的尺寸大小并未发生变化,可查看变量了解、
Image 输入图像
Region 输入区域
ImageReduced 输出区域图像 */
create_shape_model (TemplateImage, 4, rad(0), rad(360), 'auto', ['point_reduction_high','no_pregeneration'], 'ignore_local_polarity', [21,34,7], 6, ModelId)
/* 创建一个不可缩放的区域匹配模型
Temple 输入的图像模板名称,用来产生模型的图像
NumLevels 最大的金字塔(分析法)层数,用来产生模型的图像
AngleStart 输入匹配的起始角度
AngleExtent 输入匹配时的角度范围
AngleStep 输入匹配旋转角度的步长,0<该值<(PI/16)
Optimization 设置模板优化和模板创建的方法
Metric 设置匹配方法
Contrast 设置对比度
MinContrast 输入图像中匹配物体的最小差别(对比度)
ModelID 输出模型 */
get_shape_model_contours (ModelContours, ModelId, 1)
/* 提取轮廓匹配模型的外形轮廓
Model Contours 输出外形轮廓
ModelID 输入匹配模型
Level 输入复制外形轮廓的金字塔层数,默认为1(原始大小) */
* Measure the runtime of find_shape_model with the
* model image. The first run is made to fill the caches.
//使用模型映像测量 find_shape_model 的运行时间,第一次运行是为了填充缓存
find_shape_model (Image, ModelId, rad(0), rad(360), 0.7, 1, 0.5, 'least_squares', [4,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
/* 搜索图像中不可缩放的单个外形匹配模型
Image 输入匹配模型的图像
ModeID 输入模型
AngleStart 输入匹配时的起始角度
AngleExtent 输入匹配时的角度范围
MinScore 输入最小的匹配值(匹配质量),考虑到模板的一半被遮挡,默认为0.5,0<= 该值 <=1
NumMatches 在图像上找到模板的最大个数,默认一个
MaxOverlap 定义了找到的两个目标区域最多重叠的系数,如果 = 0,找到目标区域不能存在重叠,如果 = 1,所有找到的目标区域都要返回,参考值0.5,0<= 该值 <=1
SubPixel 确定找到的目标是否使用亚像素精度提取
NumLevels 搜索时使用的金字塔层数,默认值为0
Greediness 搜索时的“ 贪婪程度 ” (0:safe but slow 慢而安全;1:fast but matches may be missed 快而可能匹配失败)
Row 输出目标外形轮廓的y值坐标(轮廓中心位置)
Column 输出目标外形轮廓的x值坐标(轮廓中心位置)
Angle 输出目标物体模型旋转后对应的角度
ScaleR 输出y值方向的比例大小
ScaleC 输出x值方向的比例大小
score 输出目标物体的匹配值 */
Loops := 10
count_seconds (S1)
for Index := 1 to Loops by 1
find_shape_model (Image, ModelId, rad(0), rad(360), 0.7, 1, 0.5, 'least_squares', [4,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
endfor
count_seconds (S2)
Runtime := (S2 - S1) * 1000.0 / Loops
/* 循环十次
记录起始时间点S1,终止时间点S2
*1000 转换单位,秒转换为毫秒 */
* Set the maximum runtime used to find the shape model. It
* should not exceed the measured runtime by more than 50 percent
//设置用于查找形状模型的最大运行时间,它不应该超过测量运行时间的50%
Timeout := floor(Runtime * 1.5)
dev_display (Image)
disp_message (WindowHandle, 'Model image', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Timeout set to ' + Timeout + ' ms', 'window', 40, 12, 'lime green', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
// 在当前图形窗口显示图形目标并显示消息
* Set the timeout of the shape model and the mode for
* the time measurement
// 设置形状模型和时间测量模式的超时
set_shape_model_param (ModelId, 'timeout', Timeout)
set_system ('timer_mode', 'performance_counter')
/* 设置轮廓匹配模型的参数
ModelID 输入匹配模型
GenParamNames 输入参数名称
timeout 工作时间 min_contrast 最小的差别
GenParamValues 输入参数值 */
* Find the shape model, measure the runtime and display the
* error message in case of timeout
// 查找形状模型,测量运行时间,如果发生超时显示错误信息
for Index := 1 to Loops by 1
*
* Add artificial clutter to enforce varying runtimes
add_clutter (Image, ImageClutter)
dev_display (ImageClutter)
*
* Prepare error handling in case of timeout
try
*
* Find the shape model in the cluttered image and
* measure the runtime
count_seconds (S1)
find_shape_model (ImageClutter, ModelId, rad(0), rad(360), 0.7, 1, 0.5, 'least_squares', [4,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
count_seconds (S2)
Time := (S2 - S1) * 1000.0
/* 添加人工杂波以强制执行不同的运行时间
准备错误处理以防超时
在杂乱的图像中找到形状模型并测量运行时间
try启动一个程序块,在其中检测并捕获异常 */
* Display the found match and the runtime
// 显示找到的匹配项和运行时间
dev_display_shape_matching_results (ModelId, 'lime green', ModelRow, ModelColumn, ModelAngle, 1, 1, ModelId)
// 显示形状匹配结果
disp_message (WindowHandle, 'Search image ' + Index + ' of ' + Loops, 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Runtime of \'find_shape_model\': ' + Time$'.4' + ' ms', 'window', 40, 12, 'lime green', 'true')
// 显示消息
* In case of timeout display the error message
// 在超时情况下显示错误消息
catch (Exception)
count_seconds (S2)
Time := (S2 - S1) * 1000.0
/*捕获前一个try块中抛出的异常
其中 catch 和 try 一般放在一起使用
当前面的 try成功查找到模板则直接进入第一个S2 ,若查找模板失败则会被catch 捕获,并进入之后的error处理 */
* Get the error code. If the timeout error code
* is returned display the corresponding message
//获取错误代码。如果返回超时错误代码,则显示相应的消息
dev_get_exception_data (Exception, 'error_code', Error)
// 访问一个异常数组元素
if (Error == 9400)
disp_message (WindowHandle, 'Search image ' + Index + ' of ' + Loops, 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Timeout occured after ' + Time$'.4' + ' ms', 'window', 40, 12, 'red', 'true')
Message := ['Error message:\n',Exception]
disp_message (WindowHandle, Message, 'window', 60, 12, 'red', 'true')
else
throw (Exception)
endif
endtry
*
* Display the continue message
if (Index < Loops)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
// 如果查找不到模板,则显示错误信息,然后返回到添加人工杂波处,直至Loop到10
* Reset the values of the parameters
set_system ('border_shape_models', BorderShapeModels)
set_shape_model_param (ModelId, 'timeout', 'false')
dev_set_preferences ('suppress_handled_exceptions_dlg', SuppressExceptionSetting)
*
* Clear the model
clear_shape_model (ModelId)
// 重置参数值并清除模板