Halcon 仿射变换例子

 

*读取张图片

read_image (Image, 'C:/Users/Administrator/Desktop/2.png')

*设置模板区域
draw_rectangle1 (3600, ModelRow1, ModelColumn1, ModelRow2, ModelColumn2)
gen_rectangle1 (ModelRect, ModelRow1, ModelColumn1, ModelRow2, ModelColumn2)

*设置测试区域
draw_rectangle2 (3600, TRow, TColumn, TPhi, TLength1, TLength2)

*设置gen_rectangle2 函数,参数是矩形的中心点,长半径,宽半径。
gen_rectangle2 (Rectangle, TRow, TColumn, TPhi, TLength1, TLength2)

*
reduce_domain (Image, ModelRect, ImageReduced)

*查找模板矩形中心点
area_center (ModelRect, Area, CRow, CColumn)

*模板中心点和测试中心点的距离
CTRow:=TRow-CRow
CTColumn:=TColumn-CColumn

*建立模板

create_shape_model (ImageReduced, 'auto', rad(0), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
dev_clear_window ()
get_shape_model_contours (ImageReduced, ModelID, 1)
dev_display (ImageReduced)

*读取图片2
read_image (Image1, 'C:/Users/Administrator/Desktop/4.png')

*查找模板
find_shape_model (Image1, ModelID, rad(0), rad(360), 0.4, 0, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)

*初始化仿射矩阵
hom_mat2d_identity (HomMat2DIdentity)

*仿射矩阵偏移
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, Angle, Row, Column, HomMat2DRotate)

*仿射xld
affine_trans_contour_xld (ImageReduced, ShapeModelTrans, HomMat2DRotate)
dev_display (ShapeModelTrans)

*仿射测试点
affine_trans_pixel (HomMat2DRotate, CTRow, CTColumn, RowTrans, ColTrans)

*Angle1为原来的角度
gen_rectangle2 (Rectangle1, RowTrans, ColTrans, Angle1+Angle, TLength1, TLength2)
dev_display (Rectangle1)

//********************************************************************//

*另外一种仿射变换

*仿射矩阵偏移
hom_mat2d_translate (HomMat2DIdentity, Row-CRow, Column-CColumn, HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, Angle, Row, Column, HomMat2DRotate)

*仿射xld
affine_trans_contour_xld (ImageReduced, ShapeModelTrans, HomMat2DRotate)
dev_display (ShapeModelTrans)

*仿射测试点
affine_trans_pixel (HomMat2DRotate, TRow, TColumn, RowTrans, ColTrans)

*整体region仿射变换

affine_trans_region (Rectangle, RegionAffineTrans2, HomMat2DRotate, 'nearest_neighbor')

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页