Halcon 创建模版保存到本地并从文件夹读取模版进行匹配
在上一篇文章中提到了使用create_scaled_shape_model算子创建模版并进行模版匹配。这篇文章根据上一篇文章进行扩展,将创建的模版保存到本地并从本地读取模版进行匹配。
使用create_scaled_shape_model算子创建模版并进行模版匹配
下面是完整的代码,我们根据这个完整代码进行拆分来实现保存模版并实现模版匹配。
这部分的代码是全部能将文件夹中的所有图片匹配出来。
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('E:/ZhuW/Halcon/模版匹配/标签', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
read_image (Image, ImageFiles[0])//读取第一张图像
get_image_size (Image, Width, Height)//获取图像大小
gen_rectangle2 (ROI_0, 1894.92, 2884.33, rad(-53.132), 1208.64, 283.476)
reduce_domain (Image, ROI_0, ImageReduced)//抠图
scale_image (ImageReduced, ImageScaled, 2.38318, -269)//提高对比度
create_scaled_shape_model (ImageScaled, 'auto', rad(0), rad(360), 'auto', 0.5, 1.9, 'auto', 'auto', \
'use_polarity', [30,40,7], 10, ModelID)//创建模版
get_shape_model_contours (ModelContours, ModelID, 1)//获取轮廓模型
dev_open_window (0, 0, Width/8, Height/8, 'black', WindowHandle)//打开窗口
dev_set_color ('green')//设置颜色
for Index :=0 to |ImageFiles|-1 by 1
read_image (Image1, ImageFiles[Index])
parse_filename(ImageFiles[Index], BaseName, Extension, Directory) //分割文件名
find_scaled_shape_model (Image1, ModelID, rad(0), rad(360), 0.5, 1.9, 0.1, 1, 0.5, \
'least_squares', 0, 0.9, Row, Column, Angle, Scale, Score)//查找模版
if (|Row|>0)
vector_angle_to_rigid (0, 0, 0, Row, Column, Angle, HomMat2D)// 根据点的对应关系计算变换矩阵
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2D)//仿射变换
endif
dev_display (Image1)
dev_disp_text (BaseName,'image', 20, 20, 'black', [], [])
dev_display (ContoursAffineTrans)
wait_seconds (1)
endfor
保存模版
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('E:/ZhuW/Halcon/模版匹配/标签', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
read_image (Image, ImageFiles[0])//读取第一张图像
get_image_size (Image, Width, Height)//获取图像大小
gen_rectangle2 (ROI_0, 1894.92, 2884.33, rad(-53.132), 1208.64, 283.476)
reduce_domain (Image, ROI_0, ImageReduced)//抠图
scale_image (ImageReduced, ImageScaled, 2.38318, -269)//提高对比度
create_scaled_shape_model (ImageScaled, 'auto', rad(0), rad(360), 'auto', 0.5, 1.9, 'auto', 'auto', \
'use_polarity', [30,40,7], 10, ModelID)//创建模版
ModelFile := 'model.shm'
write_shape_model (ModelID, ModelFile)//保存模版
读取模版并进行模版匹配
注意点:我们通过get_shape_model_params 这个算子获得模型模版的参数,匹配模版的时候我们也会用到这些参数。
dev_set_line_width (3)
ModelFile := 'model.shm'
read_shape_model (ModelFile, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)//得到轮廓的模型
get_shape_model_origin (ModelID, Row1, Column1)//返回形状模型的原点
get_shape_model_params (ModelID, NumLevels, AngleStart, AngleExtent, AngleStep, \
ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)//返回形状模型的参数
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('E:/ZhuW/Halcon/模版匹配/标签', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
read_image (Image1, ImageFiles[0])
get_image_size (Image1, Width, Height)//获取图片的高和宽
dev_open_window (0, 0, Width/8, Height/8, 'black', WindowHandle)
dev_set_color ('green')
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
find_scaled_shape_model (Image, ModelID, AngleStart, AngleExtent, ScaleMin, ScaleMax, 0.1, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Scale, Score)
if (|Row|>0)
vector_angle_to_rigid (0, 0, 0, Row, Column, Angle, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2D)//仿射变换
endif
dev_display (Image)
dev_disp_text (BaseName,'image', 20, 20, 'black', [], [])
dev_display (ContoursAffineTrans)
wait_seconds (1)
endfor