基于形状的模板匹配(西动科技Halcon学习笔记)
主要设计流程:将彩色图像转为灰度图像,进行阈值分割,通过面积特征选择ROI区域,进行形态学处理后创建模板,创建好的模板显示在窗口左上角,需要将模板显示在原位置。再读取另一张图像进行模板匹配并显示结果。
Halcon代码
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
* Image Acquisition 01: Code generated by Image Acquisition 01
read_image (Image, 'D:/机器视觉学习资料/Halcon学习/案例/模板匹配/黄绿红蓝.png')
rgb1_to_gray (Image, GrayImage)
*阈值分割
threshold (GrayImage, Regions, 87, 89)
*调用connection分割成不同的区域
connection (Regions, ConnectedRegions)
*根据面积选择特征区域
select_shape (Regions, SelectedRegions, 'area', 'and', 13963.3, 14256.9)
*填充
fill_up (SelectedRegions, RegionFillUp)
*膨胀
dilation_circle (RegionFillUp, RegionDilation, 5.5)
*reduce_domain获取特定区域region的图像,缩小图像定义域并不能缩小实际尺寸
reduce_domain (GrayImage, RegionDilation, ImageReduced)
*创建模板轮廓
create_scaled_shape_model (ImageReduced, 'auto', rad(-45), rad(90), 'auto', 0.9, 1.1, 'auto', 'auto', 'ignore_local_polarity', 'auto', 'auto', ModelID)
*获取图像轮廓
get_shape_model_contours (ModelContours, ModelID, 1)
*求中心点坐标
area_center (RegionDilation, Area, Row, Column)
*获得平移旋转的矩阵、从(0,0)点移动到求得中心点位置
vector_angle_to_rigid (0, 0, 0, Row, Column, 0, HomMat2D)
*模板放到原位置
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2D)
dev_display (Image)
dev_display (ContoursAffineTrans)
* Image Acquisition 02: Code generated by Image Acquisition 02
read_image (Image, 'D:/机器视觉学习资料/Halcon学习/案例/模板匹配/多种图形.png')
rgb1_to_gray (Image, GrayImage1)
find_scaled_shape_model (GrayImage1, ModelID, rad(-45), rad(90), 0.9, 1.1, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Scale, Score)
for i:=0 to |Score|-1 by 1
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row1[i], Column1[i], HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, Angle[i], Row1[i], Column1[i], HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, Scale[i], Scale[i], Row1[i], Column1[i], HomMat2DScale)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans1, HomMat2DScale)
dev_display (ContoursAffineTrans1)
endfor
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)