一、概述
模板匹配常用于定位和查找,有很多的方式,halcon 中就有灰度匹配 、形状匹配、变形匹配、缩放匹配等,其实最常用的还是两种第一个就是灰度匹配、还有就是形状匹配
二、金字塔概述
网上有很多关于金字塔的解释,我这里直接上图看看金字塔层数的不同影响
第一层:
第二层
第三层:
第四层:
。
。
。
第8层
从其中可以看出:
金字塔层数越高,模板图像越小所以金字塔越高,匹配越快
三、匹配
read_image (Image, './scale.tif')
gen_rectangle1 (ROI_0, 420.708, 55.4103, 2606.9, 1005.36)
area_center (ROI_0, Area2, Row3, Column3)
reduce_domain (Image, ROI_0, ImageReduced1)
crop_domain (ImageReduced1, ImagePart)
get_image_size (ImagePart, Width2, Height2)
create_shape_model (ImagePart, 'auto', -0.39, 0.79, 'auto', 'auto', 'use_polarity', 10, 5, ModelID)
get_shape_model_contours (ModelContours1, ModelID, 1)
vector_angle_to_rigid (0, 0, 0,Row3, Column3, 0, HomMat2D2)
dev_display (Image)
affine_trans_contour_xld (ModelContours1, ContoursAffineTrans1, HomMat2D2)
*Row2 找到匹配的全图的行
*Column2 找到匹配的全图的行
find_shape_model (Image, ModelID, -0.39, 0.79, 0.3, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle, Score)
* 模板是单个图像,所以这个起始位置和角度都是000
vector_angle_to_rigid (0, 0, 0, Row2, Column2, Angle, HomMat2D3)
dev_display (Image)
gen_cross_contour_xld (Cross1, Row2, Column2, 116, 0.78)
affine_trans_contour_xld (ModelContours1, ContoursAffineTrans, HomMat2D3)
这个是简单的使用,opencv 哪里要学习一些大概的实现
补充一个paint_region算子: 将选中的区域变成自己想要的灰度值
* *把上面的图的7芯片 个变成白色
read_image(Image, 'printer_chip/printer_chip_01')
threshold (Image, Regions, 128, 255)
connection(Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions1, 'area', 'and', 25360, 46760.5)
get_image_size (Image, Width, Height)
paint_region (SelectedRegions1, Image, imgR, 255, 'fill')
四、3D模板匹配
halcon 案例:
create_shape_model_3d_lowest_model_level.dev
* This example program shows how to use HALCON's shape-based
* 3D matching to find the 3D pose of tile spacers. Because
* the object appears large in the images, the 3D model
* needs many internal representations of different viewing
* angles. This leads to a very high memory consumption of
* the model. To reduce the needed memory, the parameter
* 'lowest_model_level' is used. By setting 'lowest_model_level'
* to a higher level (1 is the default) the memory consumption
* can be reduced approximately by factor 3 per level, while
* the time for the creation of the model is reduced by factor 2.
* In exchange, the robustness of the matching might decrease and
* the search time increases slightly (by 5-10%).
*
* Here, 'lowest_model_level' is set to 3, which results in
* a reduction by factor 6 for the memory needed for the model
* and factor 4 for the time needed to create the model.
*
dev_update_off ()
*
* Set the camera parameters (can be obtained by calibrate_cameras)
* 相机的内参 焦距 相机畸变 x方向的像素当量 y方向的像素当量 图像坐标在平面坐标的 位置x y 图片的宽 图片的高
gen_cam_par_area_scan_division (0.0269462, -354.842, 1.27964e-005, 1.28e-005, 254.24, 201.977, 512, 384, CamParam)
* 从内参中获取 高和宽
get_cam_par_data (CamParam, 'image_width', IWidth)
get_cam_par_data (CamParam, 'image_height', IHeight)
*
read_image (Image, 'tile_spacers/tile_spacers_color_01')
dev_close_window ()
dev_open_window (0, 0, 512 * 1.5, 384 * 1.5, 'white', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
*
*
dev_clear_window ()
disp_message (WindowHandle, 'Reading the 3D shape model file from disk ...', 'window', 12, 12, 'black', 'false')
* Create the 3D shape model if it is not available on disk
try
read_shape_model_3d ('tile_spacer.sm3', ShapeModel3DID)
catch (Exception)
*
* 读取3D模型作为 目的是
read_object_model_3d ('tile_spacer.dxf', 0.0001, [], [], ObjectModel3DID, DXFStatus)
* 将3D模型投影到2D的图像坐标系中
prepare_object_model_3d (ObjectModel3DID, 'shape_based_matching_3d', 'true', [], [])
*
disp_message (WindowHandle, 'Reading the 3D shape model file from disk ... not found!', 'window', 12, 12, 'red', 'false')
disp_message (WindowHandle, 'Creating the 3D shape model (may take a few seconds) ...', 'window', 42, 12, 'black', 'false')
count_seconds (S1)
*
* Model creation
*
* Change the value of the 'lowest_model_level' parameter to
* compare the training times and memory consumption on your
* computer.
* 用投影后的模组作为3D模板 其结果放到ShapeModel3DID 当中
create_shape_model_3d (ObjectModel3DID, CamParam, 0, 0, 0, 'gba', -rad(60), rad(60), -rad(60), rad(60), 0, rad(360), 0.26, 0.27, 10, 'lowest_model_level', 3, ShapeModel3DID)
count_seconds (S2)
T := S2 - S1
*
disp_message (WindowHandle, 'Creation time: ' + T$'.3' + ' s', 'window', 72, 12, 'black', 'false')
try
disp_message (WindowHandle, 'Writing model to disk ...', 'window', 102, 12, 'black', 'false')
write_shape_model_3d (ShapeModel3DID, 'tile_spacer.sm3')
catch (Exception)
disp_message (WindowHandle, 'Writing model to disk ... failed!', 'window', 102, 12, 'red', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endtry
endtry
disp_lowest_model_level_info (WindowHandle)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Matching
Times := []
NumImages := 12
for I := 1 to NumImages by 1
read_image (Image, 'tile_spacers/tile_spacers_color_' + I$'02')
dev_display (Image)
*
* Find up to three instances of the 3D shape model
* (set 'border_model' to 'true' because the object may
* touch the image border)
count_seconds (Seconds1)
find_shape_model_3d (Image, ShapeModel3DID, 0.7, 0.85, 0, ['num_matches','max_overlap','border_model'], [3,0.75,'true'], Pose, CovPose, Score)
count_seconds (Seconds2)
Time := Seconds2 - Seconds1
Times := [Times,Time]
* Visualize the found matches in the image by projecting
* the 3D shape model into it using the pose of the match
for J := 0 to |Score| - 1 by 1
* Display contours
PoseTmp := Pose[J * 7:J * 7 + 6]
project_shape_model_3d (ModelContours, ShapeModel3DID, CamParam, PoseTmp, 'true', rad(30))
dev_set_color ('yellow')
dev_display (ModelContours)
* Display the coordinate system of the 3D object model
dev_set_colored (3)
disp_3d_coord_system (WindowHandle, CamParam, PoseTmp, 0.015)
endfor
for K := 0 to |Score| - 1 by 1
* Display the parameters of the found pose
PoseTmp := Pose[K * 7:K * 7 + 6]
display_match_pose (ShapeModel3DID, PoseTmp, WindowHandle)
endfor
disp_message (WindowHandle, |Score| + ' Match(es) found in ' + Time$'4.2f' + ' s', 'window', 12, 12, 'dark green', ['white','false'])
if (I < NumImages)
disp_continue_message (WindowHandle, 'black', ['white','false'])
stop ()
endif
endfor
*
disp_end_of_program_message (WindowHandle, 'black', ['white','false'])