在halcon代码的基础上进行修改,定位匹配图形的中心点,并且显示中心点位置;
*
* ***********************************************************
* This example shows how to use the 'high_noise_sample' *
* parameter from the set_generic_shape_model_object *
* operator to improve the matching rate for images *
* containing very high noise. The operator estimates the *
* lowest pyramid level automatically based on a provided *
* sample search image. *
* ***********************************************************
*
* ***********************************************************
* Initialization.
* ***********************************************************
dev_update_off ()
* 关闭自动更新以提高性能
dev_close_window ()
* 关闭所有现有窗口
* ***********************************************************
* Initialization of the display window using automatic size.
* ***********************************************************
read_image (Image, 'crosses/crosses_01')
* 读取一张图像用于初始化窗口
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
* 创建一个适配图像大小的窗口
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* 设置字体样式
dev_set_color ('lime green')
* 设置绘图颜色为亮绿色
dev_set_line_width (3)
* 设置线条宽度为3像素
* ***********************************************************
* Visualization: 显示描述信息和样本图像
* ***********************************************************
dev_display_description_text (WindowHandle)
* 显示帮助说明文字
stop ()
* 暂停程序,等待用户操作(F5继续)
dev_display_sample_image (Image, WindowHandle)
stop ()
* ***********************************************************
* Create the shape model (offline step)
* ***********************************************************
create_generic_shape_model (ModelID)
* 创建通用形状模型并返回句柄
* ***********************************************************
* Define the model template (offline step)
* ***********************************************************
* The quality of the model contour is crucial.
read_object (ModelContour, 'model_contour')
* 从文件中读取轮廓作为模板
* ***********************************************************
* Set the model parameters (offline step)
* ***********************************************************
set_generic_shape_model_param (ModelID, 'iso_scale_min', 0.75)
* 设置最小缩放比例
set_generic_shape_model_param (ModelID, 'iso_scale_max', 1.25)
* 设置最大缩放比例
* ***********************************************************
* Train the shape model (offline step)
* ***********************************************************
train_generic_shape_model (ModelContour, ModelID)
* 使用轮廓训练模型
* ***********************************************************
* Set the metric (offline step)
* ***********************************************************
* Create the polarity image
gen_image_const (PolarityImage, 'byte', 512, 512)
* 创建一个空白图像
paint_region (PolarityImage, PolarityImage, PolarityImage, 255, 'fill')
* 填充白色背景
paint_xld (ModelContour, PolarityImage, PolarityImage, 0)
* 在上面绘制黑色轮廓
* Determine the associated homography and set the metric.
find_generic_shape_model (PolarityImage, ModelID, PolarityMatchResultID, NumMatchResult)
get_generic_shape_model_result (PolarityMatchResultID, 0, 'hom_mat_2d', HomMat2D)
set_shape_model_metric (PolarityImage, ModelID, HomMat2D, 'use_polarity')
* Visualization.
dev_display_metric (PolarityImage, PolarityMatchResultID, WindowHandle)
stop ()
* ***********************************************************
* Estimate the lowest pyramid level (offline step)
* ***********************************************************
* Estimate the lowest pyramid level based on a sample search image.
gen_rectangle1 (Rectangle, 97, 355, 222, 455)
* 定义一个矩形区域
reduce_domain (Image, Rectangle, ImageReduced)
* 提取该区域图像
set_generic_shape_model_object (ImageReduced, ModelID, 'high_noise_sample')
* 设置高噪声样本
get_generic_shape_model_param (ModelID, 'pyramid_level_lowest', PyramidLevelLowest)
* Visualization.
dev_display_high_noise_sample (ImageReduced, PyramidLevelLowest, WindowHandle)
stop ()
* ***********************************************************
* Set the search parameters
* ***********************************************************
set_generic_shape_model_param (ModelID, 'angle_start', rad(-50))
* 设置起始角度
set_generic_shape_model_param (ModelID, 'angle_end', rad(50))
* 设置结束角度
set_generic_shape_model_param (ModelID, 'num_matches', 5)
* 最多匹配5个对象
set_generic_shape_model_param (ModelID, 'max_overlap', 0.0)
* 不允许重叠
set_generic_shape_model_param (ModelID, 'greediness', 0.0)
* 匹配贪婪度设为最低
* ***********************************************************
* Find the model (online step)
* ***********************************************************
dev_set_color ('lime green')
* 设置匹配结果的显示颜色
NumImages := 10
* 图像序列数量
for Index := 1 to NumImages by 1
read_image (Image, 'crosses/crosses_' + Index$'02')
* 读取当前图像
find_generic_shape_model (Image, ModelID, MatchResultID, NumMatchResult)
* 执行匹配
get_generic_shape_model_result_object (Contours, MatchResultID, 'all', 'contours')
* 获取所有匹配轮廓
dev_display (Image)
* 显示原图
dev_display (Contours)
* 显示匹配轮廓
Message := 'Found ' + NumMatchResult + ' matches'
* 构造提示信息
dev_disp_text (Message, 'window', 'top', 'left', 'black', 'box', 'true')
* 显示匹配数
* 遍历每一个匹配结果,计算中心点并标注
for i := 0 to NumMatchResult - 1 by 1
get_generic_shape_model_result (MatchResultID, i, 'hom_mat_2d', HomMat2D)
* 获取仿射变换矩阵
affine_trans_point_2d (HomMat2D, 0, 0, Row, Column)
* 将模板中心映射到图像坐标
disp_cross (WindowHandle, Row, Column, 20, 0)
*绘制十字标记中心点
dev_disp_text ('#' + (i + 1), 'image', Row - 15, Column + 15, 'red', 'box', 'true')
* 标注编号
* 显示坐标文本(关键部分,含异常处理)
try
set_tposition (WindowHandle, Row, Column)
* 设置文本位置
set_color (WindowHandle, 'yellow')
* 设置文本颜色
write_string (WindowHandle, 'Center: (' + Row$'.2f' + ', ' + Column$'.2f' + ')')
* 显示坐标
catch (Exception)
* 如果出现异常(如窗口关闭),则重新创建窗口
dev_close_window ()
dev_open_window (0, 0, 768, 576, 'black', WindowHandle)
dev_set_window (WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
continue
* 跳过当前图像
endtry
endfor
* 显示附加信息
dev_disp_text ('Detection results in search image.', 'window', 'top', 'left', 'black', 'box', 'true')
dev_disp_text ('Image ' + Index + '/' + 10, 'window', 'top', 'right', 'black', 'box', 'true')
wait_seconds (0.75)
* 每张图像停留0.75秒
endfor
stop ()
* 暂停以便查看最后结果
* ***********************************************************
* Clean up
* ***********************************************************
dev_clear_window ()
* 清空窗口内容
dev_disp_text ('End of program', 'window', 'bottom', 'right', 'black', [], [])
* 显示结束提示
dev_update_on ()
* 开启窗口自动更新