当前位置: 首页 > news >正文

HALCON第四讲->几何变换

文章目录

      • 一、基础概念与核心原理
      • 一、刚体变换:保持形状不变(平移+旋转)
        • **核心算子**与原理
      • 二、仿射变换:线性变换(平移+旋转+缩放+错切)
        • **核心算子**与矩阵操作
        • **错切变换示例**
      • 三、投影变换:透视校正(非共面点映射)
        • **核心算子**与流程
      • 四、非刚体变换:弹性形变(局部扭曲)
        • **核心算子**与场景
      • 五、像素与图像变换
        • **1. 仿射变换矩阵构建**
        • **2. 投影变换实现**
      • 六、XLD轮廓变换
        • **1. 轮廓仿射变换**
        • **2. 轮廓形状转换**
      • 七、区域变换(二值形态学处理)
        • **1. 区域仿射变换**
        • **2. 区域形态学变换**
      • 八、综合案例:刹车盘多角度尺寸测量
        • **目标**:倾斜状态下测量内径+外径+圆度
      • 九、参数调优黄金法则
        • **3. 插值算法选型**
        • **4. 工业级问题解决方案**

以下针对HALCON几何变换技术进行系统梳理,结合工业场景需求、数学原理及参数调优策略,分类详解刚体变换、仿射变换、投影变换及非刚体变换的核心算子,并附综合案例。


一、基础概念与核心原理

几何变换本质:将图像中的坐标位置映射到新坐标位置,不改变像素值,仅重新排列像素空间关系。
齐次坐标系:用于简化变换计算,二维坐标扩展为三维向量(如像素坐标(x,y)表示为(x,y,1))。
变换类型

  • 刚体变换:平移+旋转(保持形状不变)
  • 仿射变换:平移+旋转+缩放+错切(保持平行性)
  • 投影变换:任意四边形映射(模拟透视效果)

一、刚体变换:保持形状不变(平移+旋转)

核心算子与原理
  1. vector_angle_to_rigid

    • 功能:计算刚体变换矩阵(平移+旋转)
    • 参数
      • Row1, Col1:参考点坐标
      • Angle:旋转角度(弧度)
      • Row2, Col2:目标点坐标
    • 数学原理
      [ cos ⁡ θ − sin ⁡ θ T x sin ⁡ θ cos ⁡ θ T y 0 0 1 ] \begin{bmatrix} \cos\theta & -\sin\theta & T_x \\ \sin\theta & \cos\theta & T_y \\ 0 & 0 & 1 \end{bmatrix} cosθsinθ0sinθcosθ0TxTy1
      其中 T x = Col2 − Col1 T_x = \text{Col2} - \text{Col1} Tx=Col2Col1, T y = Row2 − Row1 T_y = \text{Row2} - \text{Row1} Ty=Row2Row1
    • 场景:机械零件定位(平移旋转后姿态对齐)
      * 计算变换矩阵
      vector_angle_to_rigid(RefRow, RefCol, 0, TargetRow, TargetCol, Angle, HomMat2D)
      * 应用变换
      affine_trans_region(Region, RegionTrans, HomMat2D, 'nearest_neighbor')
      
  2. rigid_trans_image

    • 功能:直接对图像进行刚体变换
    • 参数Interpolation(插值方式)影响边缘质量
      • 'nearest_neighbor':速度快,锯齿明显
      • 'bilinear':平衡速度与质量(默认)
      • 'weighted':抗锯齿最佳,速度慢

二、仿射变换:线性变换(平移+旋转+缩放+错切)

核心算子与矩阵操作
  1. 矩阵构造算子

    算子功能参数示例
    hom_mat2d_identity创建单位矩阵-
    hom_mat2d_translate添加平移Tx, Ty(像素偏移)
    hom_mat2d_rotate添加旋转Angle(弧度), PivotX, PivotY(旋转中心)
    hom_mat2d_scale添加缩放Sx, Sy(缩放因子)
  2. 应用算子

    • affine_trans_image:图像仿射变换
      hom_mat2d_identity(HomMat2D)
      hom_mat2d_rotate(HomMat2D, rad(30), 256, 256, HomMatRotate)  // 绕中心旋转30°
      hom_mat2d_scale(HomMatRotate, 1.5, 1.5, 256, 256, HomMatScale)  // 缩放1.5倍
      affine_trans_image(Image, ImageTrans, HomMatScale, 'bilinear', 'false')
      
    • affine_trans_region:区域仿射变换
      • 注意:区域变换后需重采样,'nearest_neighbor'可保留二值特征
错切变换示例
hom_mat2d_slant(HomMat2D, rad(45), 'x', Height/2, Width/2, HomMatSlant)
affine_trans_image(Image, ShearedImage, HomMatSlant, 'constant', 'false')

效果:模拟摄像头倾斜拍摄(PCB板检测中校正元件位置)


三、投影变换:透视校正(非共面点映射)

核心算子与流程
  1. hom_vector_to_proj_hom_mat2d

    • 功能:根据4组点对应计算单应性矩阵
    • 参数
      • SourceX/Y:源图像四边形顶点坐标
      • DestX/Y:目标矩形顶点坐标
      • Method'normalized_dlt'(归一化直接线性变换)
    • 数学原理
      [ h 11 h 12 h 13 h 21 h 22 h 23 h 31 h 32 1 ] [ x y 1 ] = [ x ′ y ′ w ′ ] \begin{bmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} x' \\ y' \\ w' \end{bmatrix} h11h21h31h12h22h32h13h231xy1=xyw
      归一化坐标: x ′ ′ = x ′ / w ′ , y ′ ′ = y ′ / w ′ x'' = x'/w', y'' = y'/w' x=x/w,y=y/w
  2. projective_trans_image

    • 应用示例(文档透视校正):
      SourceX := [70, 270, 270, 70]  // 源图四边形顶点X
      SourceY := [100, 100, 300, 300] // 源图四边形顶点Y
      DestX := [0, 480, 480, 0]      // 目标矩形顶点X
      DestY := [0, 0, 300, 300]      // 目标矩形顶点Y
      hom_vector_to_proj_hom_mat2d(SourceX, SourceY, [1,1,1,1], DestX, DestY, [1,1,1,1], 'normalized_dlt', HomMat2D)
      projective_trans_image(Image, ImageRectified, HomMat2D, 'bilinear', 'false', 'false')
      
      效果:将倾斜拍摄的文档转换为正视角矩形

四、非刚体变换:弹性形变(局部扭曲)

核心算子与场景
  1. deform_region

    • 功能:基于位移场扭曲区域
    • 参数
      • DeformationField:位移场图像(双通道,存储XY方向偏移)
      • Interpolation:形变插值方式
    • 场景:医学影像对齐(如肺部呼吸运动补偿)
  2. gen_arbitrary_deform_model

    • 流程
      1. 生成位移场:gen_deform_model
      2. 训练变形模型:train_deform_model
      3. 应用变形:deform_image
    • 示例(液晶屏缺陷修复):
      gen_deform_model(Image, DeformModel, 'bilinear', [10,10], [0.1,0.1])
      train_deform_model(DeformModel, DefectRegion, TemplateRegion)
      deform_image(DeformModel, Image, ImageDeformed)
      
      效果:将扭曲的屏幕区域映射到标准模板

五、像素与图像变换

1. 仿射变换矩阵构建
变换类型矩阵形式HALCON算子
平移 [ 1 0 t x 0 1 t y 0 0 1 ] \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} 100010txty1hom_mat2d_translate(HomMat, Tx, Ty, HomMatTrans)
旋转 [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} cosθsinθ0sinθcosθ0001hom_mat2d_rotate(HomMat, Theta, Px, Py, HomMatRot)
缩放 [ s x 0 0 0 s y 0 0 0 1 ] \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} sx000sy0001hom_mat2d_scale(HomMat, Sx, Sy, Px, Py, HomMatScale)

示例:绕中心旋转30°并缩放0.8倍

hom_mat2d_identity(HomMat)
hom_mat2d_rotate(HomMat, rad(30), 256, 256, HomMatRot)
hom_mat2d_scale(HomMatRot, 0.8, 0.8, 256, 256, HomMatScale)
2. 投影变换实现

原理:单应性矩阵将四边形映射到任意四边形(需4对非共线点)。
算子

vector_to_proj_hom_mat2d(SourceX, SourceY, DestX, DestY, 'normalized_dlt', HomMat2D)
projective_trans_image(Image, ImageTrans, HomMat2D, 'bilinear', 'false')

场景:文档透视校正(倾斜文本→正视角矩形)。


六、XLD轮廓变换

1. 轮廓仿射变换

算子affine_trans_contour_xld(Contours, ContoursTrans, HomMat2D)
参数说明

  • HomMat2D:2×3仿射变换矩阵
  • Contours:输入轮廓(亚像素精度)

示例:轮廓平移+旋转

edges_sub_pix(Image, Edges, 'canny', 1.0, 20, 40)  // 提取轮廓
affine_trans_contour_xld(Edges, EdgesTrans, HomMatScale)  // 应用变换矩阵
2. 轮廓形状转换

算子shape_trans_xld(XLD, XLDTrans, Type)
Type选项

  • 'convex':计算凸包
  • 'ellipse':拟合等效椭圆
  • 'outer_circle':最小外接圆

工业应用:齿轮齿廓凸包检测(排除毛刺干扰)。


七、区域变换(二值形态学处理)

1. 区域仿射变换

算子affine_trans_region(Region, RegionTrans, HomMat2D, 'nearest_neighbor')
参数说明

  • 'nearest_neighbor':二值区域保形插值
  • 'constant':背景填充值

示例:芯片定位区域旋转

threshold(Image, Region, 120, 255)
affine_trans_region(Region, RegionRot, HomMatRot, 'nearest_neighbor')
2. 区域形态学变换

算子

  • dilation_circle(Region, RegionDilated, 5.0):圆形膨胀(填补孔洞)
  • transpose_region(Region, RegionTransposed):区域转置(行列互换)

八、综合案例:刹车盘多角度尺寸测量

目标:在任意摆放角度下测量刹车盘内径与外径

* 1. 图像采集与预处理
read_image(Image, 'brake_disk.jpg')
rgb1_to_gray(Image, GrayImage)
gauss_filter(GrayImage, Smoothed, 5)* 2. 基于形状匹配定位刹车盘
create_shape_model(Smoothed, 4, rad(-180), rad(360), 'auto', 'none', 'use_polarity', 40, ModelID)
find_shape_model(Smoothed, ModelID, rad(-5), rad(10), 0.8, 1, 0.5, 'least_squares', 0, 0.7, Row, Col, Angle, Score)* 3. 刚体变换对齐标准位置
vector_angle_to_rigid(Row, Col, Angle, 512, 512, 0, HomMat2D)
affine_trans_image(Smoothed, ImageAligned, HomMat2D, 'bicubic', 'false')* 4. 投影变换校正透视畸变(假设摄像头倾斜)
SourceX := [Col-200, Col+200, Col+200, Col-200]  // 源图四边形
SourceY := [Row-200, Row-200, Row+200, Row+200]
DestX := [256-200, 256+200, 256+200, 256-200]    // 目标正视角矩形
DestY := [256-200, 256-200, 256+200, 256+200]
hom_vector_to_proj_hom_mat2d(SourceX, SourceY, [1,1,1,1], DestX, DestY, [1,1,1,1], 'dlt', HomProj)
projective_trans_image(ImageAligned, ImageRectified, HomProj, 'bilinear', 'true')* 5. 尺寸测量
edges_sub_pix(ImageRectified, Edges, 'canny', 1.0, 20, 40)
fit_circle_contour_xld(Edges, 'algebraic', -1, 0, 0, 3, 2, InnerRow, InnerCol, InnerRadius)
fit_circle_contour_xld(Edges, 'min_circsc', -1, 0, 0, 3, 2, OuterRow, OuterCol, OuterRadius)

关键参数调整

  • 形状匹配:Angle范围从±180°缩小到±5°,提速300%
  • 投影变换:启用AdaptImageSize='true'自动扩展画布,避免裁剪有效区域

目标:倾斜状态下测量内径+外径+圆度
* 1. 图像采集与预处理
read_image(Image, 'brake_disk.jpg')
gauss_filter(Image, Smoothed, 3.0)  // 抑制铸造表面噪点* 2. 提取亚像素边缘
edges_sub_pix(Smoothed, Edges, 'canny', 1.0, 15, 30)* 3. 拟合初始圆定位中心
fit_circle_contour_xld(Edges, 'algebraic', -1, 0, 0, 3, 2, Row, Col, Radius)* 4. 投影变换校正倾斜(4点法)
SourceX := [Col-100, Col+100, Col+100, Col-100]  // 源四边形
SourceY := [Row-100, Row-100, Row+100, Row+100]
DestX := [256-100, 256+100, 256+100, 256-100]    // 目标矩形
DestY := [256-100, 256-100, 256+100, 256+100]
vector_to_proj_hom_mat2d(SourceX, SourceY, DestX, DestY, 'normalized_dlt', HomMatProj)
projective_trans_image(Smoothed, ImageRectified, HomMatProj, 'bicubic', 'true')* 5. 变换后精确测量
edges_sub_pix(ImageRectified, EdgesRect, 'canny', 1.0, 20, 40)
* 内径测量(最大内切圆)
inner_circle(EdgesRect, InnerRow, InnerCol, InnerRadius)
* 外径测量(最小外接圆)
smallest_circle_xld(EdgesRect, OuterRow, OuterCol, OuterRadius)
* 圆度评价(最小间隔法)
fit_circle_contour_xld(EdgesRect, 'min_sep', -1, 0, 0, 3, 2, _, _, _, RoundnessError)

关键调优

  • 边缘断裂:降低edges_sub_pixLow=10
  • 反光干扰:投影变换后增加直方图均衡化

九、参数调优黄金法则

  1. 精度与速度权衡

    • 高精度测量:用'least_square'亚像素插值 + 'geotukey'拟合
    • 实时检测:用'nearest_neighbor'插值 + 'greedy'匹配模式
  2. 变换失效解决方案

    问题原因解决策略
    边缘模糊多次插值累积误差链式矩阵乘法代替多次变换
    匹配偏移特征点选取不当改用Foerstner角点(points_foerstner
    投影畸变点对应顺序错误确保源点与目标点顺时针顺序一致
3. 插值算法选型
方法计算速度输出质量适用场景
最近邻插值★★★☆☆锯齿明显二值区域/实时系统
双线性插值★★☆☆☆边缘平滑灰度图像变换(默认)
双三次插值★☆☆☆☆细节保留最佳高精度测量(如医疗影像)
4. 工业级问题解决方案
问题现象原因解决策略
变换后图像边缘模糊多次插值累积误差链式矩阵乘法替代分步变换
XLD轮廓断裂边缘检测阈值过高动态调整edges_sub_pixLow参数
圆拟合发散圆弧角度不足(<90°)固定半径法:fit_circle_contour_xld(..., 'fixed_radius', r, ...)

精度对比数据:在汽车零件检测中,通过投影变换+双三次插值,尺寸测量误差从±1.2mm降至±0.05mm。

相关文章:

  • C++中的RAII技术:资源获取即初始化
  • 【C++】ImGui:不足半兆的桌面程序
  • DWS层新增指标处理方案
  • Vue3+TypeScript实现访问者模式
  • Lesson 27 A wet night
  • MySQL 和 PostgreSQL,到底选择哪个?
  • 基于llamafactory微调千问大模型(实战)
  • error report
  • 备忘录模式:状态管理的时光机器
  • Elasticsearch 的自动补全以及RestAPI的使用
  • vue3 导出表格,合并单元格,设置单元格宽度,文字居中,修改文字颜色
  • 一篇文章理解js闭包和作用于原理
  • 模板字符串使用点击事件【VUE3】
  • shell使用for循环批量统计文件的行数
  • spring boot项目整合mybatis实现多数据源的配置
  • Day13_C语言基础(C语言考试试卷)
  • 测试完成的标准是什么?
  • CoSchedule Headline Analyzer:分析标题情感强度与可读性
  • 深度学习-163-MCP技术之使用Cherry Studio调用本地自定义mcp-server
  • 【AIGC】Qwen3-Embedding:Embedding与Rerank模型新标杆
  • 网站建设平台哪个公司好/创建网站要钱吗
  • ipv6域名解析 做网站/百度在西安有分公司吗
  • 湖州建设企业网站/百度官网入口
  • 脚本语言在网站开发中/网站推广公司排名
  • 惠州网站建设公司排名/百度搜索引擎的网址是
  • 佛山免费网站制作/seo外包网络公司