5.25 Halcon仿射变换原理分析

几何定位和仿射变换是联合在一起的
仿射变换的意思就是跟随,目的就是实现跟随的效果

  • Matching 01: ***********************************************

  • Matching 01: BEGIN of generated code for model initialization

  • Matching 01: ***********************************************
    *设置系统参数
    set_system ('border_shape_models', 'false')

  • Matching 01: Obtain the model image
    read_image (Image, 'clip')
    *生成一个圆

  • Matching 01: Build the ROI from basic regions
    gen_circle (ModelRegion, 233.558, 483.037, 35.9314)
    *使用这个图像,通过这个区域,生成另外一个图像 结果是个图像

  • Matching 01: Reduce the model template
    reduce_domain (Image, ModelRegion, TemplateImage)

  • Matching 01: Create the shape model
    *create_shape_model (TemplateImage, 3, rad(0), rad(360), rad(3.4478), ['r']
    *创建模板TemplateImage 目标图像 3金字塔等级 rad(0)起始角度 rad(360)角度范围
    *['none','no_pregeneration'] 可优化的选项 'use_polarity' 是否使用极性[10,11,4]对比度 4表示最小对比度 ModelID生成一个目标ID
    create_shape_model (TemplateImage, 3, rad(0),rad(360), rad(3.4478), ['none','no_pregeneration'], 'use_polarity', [10,11,4], 4, ModelID)
    *获取模板的边缘

  • Matching 01: Get the model contour for transforming it later into th
    get_shape_model_contours (ModelContours, ModelID, 1)
    *圆的区域和中心点

  • Matching 01: Get the reference position
    area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
    *生成 HomMat2D 这就是一个仿射变换矩阵 原始点X 原始点Y 原始点A 目标点X 目标点Y 目标点A 支持平移和旋转
    vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
    *对XId进行仿射变换
    affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)

  • Matching 01: Display the model contours
    dev_display (Image)
    dev_set_color ('green')
    dev_set_draw ('margin')
    dev_display (ModelRegion)
    dev_display (TransContours)
    stop ()

  • Matching 01: END of generated code for model initialization

  • Matching 01: ******************************

  • Matching 01: BEGIN of generated code for model application

  • Matching 01: Loop over all specified test images
    TestImages := ['clip']

for T := 0 to 0 by 1
*
* Matching 01: Obtain the test image
read_image (Image, TestImages[T])
*
* Matching 01: Find the model
*find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', [3,1], 0.7
*查找模板
find_shape_model (Image, ModelID, -0.39, 0.79, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
* Matching 01: Transform the model contours into the detected positions
dev_display (Image)
for I := 0 to |Score| - 1 by 1

*生成一个单元矩阵

*hom_mat2d_identity (HomMat2D)
*旋转
*hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
*平移
*hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
*对XId 仿射变换
*affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
*这是2种仿射矩阵 上面的仿射矩阵 更底层一点儿
*生成 HomMat2D 这就是一个仿射变换矩阵 原始点X 原始点Y 原始点A 目标点X 目标点Y 目标点A 支持平移和旋转
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)

dev_set_color ('green')
dev_display (TransContours)
stop ()
endfor
endfor

*输出仿射变换矩阵 至少2个点 刚性仿射变换矩阵,支持旋转和平移
vector_to_rigid (Column, Row, Column, Row, HomMat2D1)

*柔性的仿射变换矩阵,支持旋转和平移 和缩放
vector_to_similarity (Column, Row, Column, Row, HomMat2D2)

*输出仿射变换矩阵 至少3个点 支持旋转、平移、缩放、斜切
vector_to_hom_mat2d (Column, Row, Column, Row, HomMat2D3)

*仿射变换流程
*1、获取特征点 坐标 角度
*2、调用算子生成仿射变换矩阵
*3、对图像 区域 XId 进行仿射变换

posted @ 2026-09-10 07:58  浮云1  阅读(4)  评论(0)    收藏  举报