Halcon模板匹配
作者:互联网
模板匹配的目的:给定一个模板图片,给它标定一个目标区域。当下一张图片进来的时候,希望在新的图片中找到目标区域。
1.如图所示,读取模板图片,然后标定目标区域。
read_image(Image, 'C:/Users/HJ/Desktop/test_image/AB1.jpg')
draw_rectangle1(WindowHandle, Row1, Column1, Row2, Column2)
gen_rectangle1(Rectangle, Row1, Column1, Row2, Column2)
reduce_domain(Image, Rectangle, ImageReduced)
2.创建一个模板shape模型, 然后再获取shape模型的轮廓表示
create_shape_model(ImageReduced, 'auto', -0.39, 0.79, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours(ModelContours, ModelID, 2)
3.读取新的图片,然后寻找图像中shape模型的最佳匹配
read_image (Image2, 'C:/Users/HJ/Desktop/test_image/AB3.jpg')
find_shape_model(Image2, ModelID, -0.39, 0.79, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
4.当得分Score的个数大于0时, 进行仿射变换,然后 对xld轮廓应用2d仿射变换
if (|Score| > 0)
vector_angle_to_rigid(0, 0, 0, Row, Column, Angle, HomMat2D)
affine_trans_contour_xld(ModelContours, ContoursAffineTrans, HomMat2D)
代码总合:
read_image(Image, 'C:/Users/HJ/Desktop/test_image/AB1.jpg') get_image_size(Image, Width, Height) * dev_close_window() dev_open_window(0, 0, Width/2, Height/2, 'black', WindowHandle) dev_display(Image) draw_rectangle1(WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1(Rectangle, Row1, Column1, Row2, Column2) reduce_domain(Image, Rectangle, ImageReduced) *创建一个模板模型 *Template:输入的图像,它的区域被用于创造模型 *NumLevels: 控制级别,可选:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'auto' *AngleStart:最小的旋转弧度。建议值:-3.14, -1.57, -0.79, -0.39, -0.20, 0.0 *AngleExtent:旋转角度的范围, 建议值:6.29, 3.14, 1.57, 0.79, 0.39 *AngleStep :角度的步长, 建议值: 'auto', 0.0175, 0.0349, 0.0524, 0.0698, 0.0873 *Optimization :优化, 建议值:'auto', 'no_pregeneration', 'none', 'point_reduction_high', 'point_reduction_low', 'point_reduction_medium', 'pregeneration' *Metric :匹配的衡量标准, 建议值:'ignore_color_polarity', 'ignore_global_polarity', 'ignore_local_polarity', 'use_polarity' *Contrast :对比度。建议值:'auto', 'auto_contrast', 'auto_contrast_hyst', 'auto_min_size', 10, 20, 30, 40, 60, 80, 100, 120, 140, 160 *MinContrast :最小对比度。它的值要比Contrast小。 建议值:'auto', 1, 2, 3, 5, 7, 10, 20, 30, 40 *ModelID :返回的是ModelID ,也就是模型的句柄 create_shape_model(ImageReduced, 'auto', -0.39, 0.79, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID) *ModelContours:返回的是形状模型的轮廓表示 *ModelID :输入的模型句柄 *Level :返回的轮廓的金字塔级别, 建议值: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 get_shape_model_contours(ModelContours, ModelID, 2) draw_rectangle1(WindowHandle, Row11, Column11, Row21, Column21) gen_rectangle1(RectangleRoi, Row11, Column11, Row21, Column21) area_center(Rectangle, AreaRef, RowRef, ColumnRef) read_image (Image2, 'C:/Users/HJ/Desktop/test_image/AB3.jpg') *寻找图像中形状模型的最佳匹配 *Image:输入的图片 *ModelID :模型句柄 *AngleStart :最小的旋转弧度。建议值:-3.14, -1.57, -0.79, -0.39, -0.20, 0.0 *AngleStep :角度的步长, 建议值: 'auto', 0.0175, 0.0349, 0.0524, 0.0698, 0.0873 *MinScore:最低得分,在(0,1)这个区间。建议值:0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 *NumMatches:找到的模型的实例数,也就是找到的匹配的个数 *MaxOverlap:找到的形状的最大重叠度。建议值:0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 *SubPixel:亚像素精度 *NumLevels: 匹配过程中的级别,可选:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 *Greediness :搜索启发式的贪婪级别,值越大,搜索速度越快,但可能会漏失。建议值零到一之间 *返回的是找到的形状的行坐标Row和列坐标Column, 以及角度angle和对应的得分Score find_shape_model(Image2, ModelID, -0.39, 0.79, 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score) if (|Score| > 0) *仿射变换 vector_angle_to_rigid(0, 0, 0, Row, Column, Angle, HomMat2D) *对XLD轮廓应用任意仿射2D变换 affine_trans_contour_xld(ModelContours, ContoursAffineTrans, HomMat2D) *仿射变换,求第二个region与标定区域的仿射变换矩阵 vector_angle_to_rigid(RowRef, ColumnRef, 0, Row, Column, Angle, HomMat2D1) affine_trans_region(RectangleRoi, RegionAffineTrans, HomMat2D1, 'nearest_neighbor') reduce_domain(Image2, RegionAffineTrans, ImageReduced1) endif clear_shape_model(ModelID)
标签:匹配,0.39,auto,image,Halcon,shape,Image,模板,ModelID 来源: https://www.cnblogs.com/mr2504/p/16443420.html