文章目录
- 基于相关性
- 使用匹配助手
基于相关性
适用场景
模板匹配:当你需要在图像中找到一个已知的模板时,例如在工业生产线上检测产品的特定标识或零件的特征时,相关性匹配是一种简单而有效的方法。 实时应用:相关性匹配通常具有较快的计算速度,因此适合用于实时应用,比如机器人视觉、自动驾驶中的目标检测等场景。 光照变化较小:相关性匹配对光照变化不敏感,因此在光照变化相对较小的场景下,它的匹配效果会更好。 少量变形:当物体存在轻微的旋转、缩放或畸变时,相关性匹配通常能够保持较好的匹配性能。
创建匹配模板
+ create_ncc_model( Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Metric : ModelID )
Template(in):单通道图像,它的区域可被创建为模板
NumLevels(in):金字塔的最大层级
AngleStart(in):模板的最小旋转
AngleExtent(in):旋转角度范围
AngleStep(in):角度步长
Metric(in):匹配标准
ModelID(out):模板句柄
查找模板
+ find_ncc_model( Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels : Row, Column, Angle, Score )
Image(in):单通道图像,它的区域可被创建为模板
ModelID(in):模板句柄
AngleStart(in):模板的最小旋转
AngleExtent(in):旋转角度范围
MinScore(in):被找到的模板最小分数
NumMatches(in):被找到的模板个数
MaxOverlap(in):被找到的模板实例最大重叠部分
SubPixel(in):亚像素级别标志,true,false
NumLevels(in):金字塔层级数
Row(out):被找到的模板实例行坐标
Column(out):被找到的模板实例列坐标
Angle(out):被找到的模板实例的旋转角度
Score(out):被找到的模板实例的分数
清除模板
+ clear_ncc_model( : : ModelID : ) 句柄
在不同的曝光下模板匹配
* This example program shows how to use HALCON's correlation-based
* matching. In particular it demonstrates the robustness of this method against
* linear illumination changes. The training is performed in an image with good
* illumination. The matching is applied in images where the exposure time varies
* extremely from very short to very long.
read_image (Image, 'cap_exposure/cap_exposure_03')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_update_off ()
*绘制圆
gen_circle (Circle, 246, 336, 150)
*获取区域圆点坐标
area_center (Circle, Area, RowRef, ColumnRef)
*裁剪图片
reduce_domain (Image, Circle, ImageReduced)
*创建匹配模板
create_ncc_model (ImageReduced, 'auto', 0, 0, 'auto', 'use_polarity', ModelID)
dev_set_draw ('margin')
dev_display (Image)
dev_set_color ('yellow')
dev_display (Circle)
disp_message (WindowHandle, 'Trained NCC model', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
Rows := []
Cols := []
for J := 1 to 10 by 1
read_image (Image, 'cap_exposure/cap_exposure_' + J$'02')
find_ncc_model (Image, ModelID, 0, 0, 0.5, 1, 0.5, 'true', 0, Row, Column, Angle, Score)
Rows := [Rows,Row]
Cols := [Cols,Column]
dev_display (Image)
*显示匹配结果
dev_display_ncc_matching_results (ModelID, 'green', Row, Column, Angle, 0)
disp_message (WindowHandle, 'Found NCC model', 'window', 12, 12, 'black', 'true')
if (J < 10)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
* Compute the standard deviation of the found positions. If the individual
* positions in Rows and Cols are examined, it can be seen that the standard
* deviation is caused mainly by the last four images, which are severely
* overexposed.
StdDevRows := deviation(Rows)
StdDevCols := deviation(Cols)
获取模板参数
+ determine_ncc_model_params(Template : : NumLevels, AngleStart, AngleExtent, Metric, Parameters : ParameterName, ParameterValue)
NumLevels:金字塔的最大层级,层数越多搜索的越快,可设为“auto”或0—10的整数。设置“auto”或0时create_ncc_model自动
AngleStart:模板旋转的起始角度(弧度)
AngleExtent:模板旋转角度范围 >=0
Metric:匹配标准 ‘use_polarity’ 使用全局,如果图像对比度不好,可用忽略全局’ignore_global_polarity’。'use_polarity'目标和背景的灰度关系保持模板和要匹配的区域灰度关系相同。 ‘ ignore_global_polarity ’对比度完全逆转也就是物体比背景颜色深也可以找到他Parameters:'all'/'num_levels'/'angle_step' 确定参数
控制输出参数:
ParameterName:Parameters中选择的参数
ParameterValue:Parameters中选择参数的具体值
* This example program shows how to use HALCON's
* operator determine_ncc_model_params to determine
* the parameters for a correlation-based matching.
*
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
for Index := 1 to 3 by 1
if (Index == 1)
read_image (Image, 'smd/smd_on_chip_05')
gen_rectangle1 (Rectangle, 180, 180, 430, 450)
elseif (Index == 2)
read_image (Image, 'fabrik')
gen_rectangle1 (Rectangle, 335, 275, 420, 355)
elseif (Index == 3)
read_image (Image, 'mreut')
gen_rectangle1 (Rectangle, 110, 195, 185, 275)
endif
dev_resize_window_fit_image (Image, 0, 0, -1, -1)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
dev_set_draw ('margin')
dev_set_line_width (2)
dev_set_color ('green')
dev_display (Rectangle)
reduce_domain (Image, Rectangle, ImageReduced)
determine_ncc_model_params (ImageReduced, 'auto', -0.39, 0.79, 'use_polarity', 'all', ParameterName, ParameterValue)
Message := 'NCC model parameters:'
for I := 0 to |ParameterName| - 1 by 1
Message[I + 1] := ParameterName[I] + ': ' + ParameterValue[I]
endfor
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
if (Index < 3)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
dev_update_on ()
使用匹配助手
选中助手
打开Halcon 自带文件C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/barcode/gs1databar_limited/gs1databar_limited_01.png
选中要匹配的区域
选中基于互相关创建好模板
参数对应create算子
应用对应find算子
加载另一张图片
> 查找模板
点击插入代码
*
* Matching 03: ************************************************
* Matching 03: BEGIN of generated code for model initialization
* Matching 03: ************************************************
*
* Matching 03: Obtain the model image
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/boxes/cardboard_boxes_01.png')
*
* Matching 03: Build the ROI from basic regions
gen_rectangle2 (ModelRegion, 382.938, 520.5, rad(128.186), 236.162, 193.603)
*
* Matching 03: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
*
* Matching 03: Create the correlation model
create_ncc_model (TemplateImage, 'auto', rad(0), rad(360), 'auto', 'use_polarity', ModelID)
*
* Matching 03: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
*
* Matching 03: Display the model region
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
gen_cross_contour_xld (TransContours, RefRow, RefColumn, 20, 0.0)
dev_display (TransContours)
stop ()
*
* Matching 03: END of generated code for model initialization
* Matching 03: * * * * * * * * * * * * * * * * * * * * * * *
* Matching 03: BEGIN of generated code for model application
*
* Matching 03: Loop over all specified test images
TestImages := ['C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/boxes/cardboard_boxes_03.png']
for T := 0 to 0 by 1
*
* Matching 03: Obtain the test image
read_image (Image, TestImages[T])
*
* Matching 03: Find the model
find_ncc_model (Image, ModelID, rad(0), rad(360), 0.8, 0, 0.5, 'true', 0, Row, Column, Angle, Score)
*
* Matching 03: Display the centers of the matches in the detected positions
dev_display (Image)
for I := 0 to |Score| - 1 by 1
* Matching 03: Display the center of the match
gen_cross_contour_xld (TransContours, Row[I], Column[I], 20, Angle)
dev_set_color ('green')
dev_display (TransContours)
hom_mat2d_identity (AlignmentHomMat2D)
hom_mat2d_translate (AlignmentHomMat2D, -RefRow, -RefColumn, AlignmentHomMat2D)
hom_mat2d_rotate (AlignmentHomMat2D, Angle[I], 0, 0, AlignmentHomMat2D)
hom_mat2d_translate (AlignmentHomMat2D, Row[I], Column[I], AlignmentHomMat2D)
* Matching 03: Display the aligned model region
affine_trans_region (ModelRegion, RegionAffineTrans, AlignmentHomMat2D, 'nearest_neighbor')
dev_display (RegionAffineTrans)
* Matching 03: Insert your code using the alignment here, e.g. code generated by
* Matching 03: the measure assistant with the code generation option
* Matching 03: 'Alignment Method' set to 'Affine Transformation'.
stop ()
endfor
endfor
*
* Matching 03: *******************************************
* Matching 03: END of generated code for model application
* Matching 03: *******************************************
*
手写案例
手写案例