Halcon仿射变换---个人笔记

1.概述

​ 仿射变换,即在二维平面内,对象进行平移(Translation)、缩放(Scale)、翻转(Flip)、旋转(Rotation)和斜切(Shear)等操作。

​仿射变换可以保持原来的线共点、点共线的关系不变;
保持原来相互平行的线仍然平行;
保持原来的中点仍然是中点;
保持原来在一直线上几段线段之间的比例关系不变;
但不能保持原来的线段长度和夹角角度不变。
在这里插入图片描述

2.仿射变换类型

仿射变换有:平移、旋转、缩放、斜切(就是将斜体字导正)。

3.仿射变换流程

1.获取特征点坐标、角度
2.计算仿射变换矩阵
3.对图像、区域、轮廓进行仿射变换

4.根据特征点、角度计算仿射变换矩阵

4.1 从空变换矩阵创建仿射变换矩阵

hom_mat2d_identity( : : : HomMat2DIdentity)
功能:产生仿射变换矩阵(产生一个空的二维空变换矩阵)
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, 200, 200, HomMat2DTranslate)
affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')

4.2 把旋转角度添加到仿射变换矩阵

hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)

功能:把旋转角度添加到仿射变换矩阵

   HomMat2D :(输入参数)仿射变换矩阵
    Phi  :旋转角度(单位弧度)
   	Px   :变换的固定点行坐标。固定点是指以该点为支撑进行仿射变换  (这里是指围绕这点进行旋转)
   	Py :  变换的固定点列坐标   	
   	HomMat2DRotate:输出的旋转变换的二维矩阵
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, rad(90), 200, 200, HomMat2DRotate)
affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')

4.3 把缩放添加到仿射变换矩阵

hom_mat2d_scale( : : HomMat2D, Sx, Sy, Px, Py : HomMat2DScale)

功能:把缩放添加到仿射变换矩阵

 HomMat2D(输入参数):仿射变换矩阵   
  Sx(输入参数):x轴方向的缩放因子    
  Sy(输入参数):y轴方向的缩放因子   
  Px(输入参数):变换的固定点行坐标    
  Py(输入参数):  变换的固定点列坐标    
  HomMat2DScale(输出参数):输出缩放变换矩阵
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, 0.5, 0.5, 0, 0, HomMat2DScale)
affine_trans_image (Image, ImageAffineTrans, HomMat2DScale, 'constant', 'false')

4.4 把平移添加到防射变换矩阵

hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)

功能:把平移添加到防射变换矩阵

   HomMat2D:(输入参数)仿射变换矩阵
   Tx(输入参数):沿x轴方向平移的距离
   Ty:输入参数):沿y轴方向平移的距离
   HomMat2DTranslate(输出参数):输出变换矩阵
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, 200, 200, HomMat2DTranslate)
affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')

4.5 把斜切添加到仿射变换矩阵

hom_mat2d_slant( : : HomMat2D, Theta, Axis, Px, Py : HomMat2DSlant)

功能:把斜切添加到防射变换矩阵

  HomMat2D        (输入参数):仿射变换矩阵
   Theta           (输入参数):斜切角度(单位:弧度)
   Axis            (输入参数):斜切的坐标轴。取值列表:x,y
   Px               (输入参数):变换的固定点x坐标
   Py              (输入参数):变换的固定点y坐标
   HomMat2DSlant  (输出参数):输出斜切仿射变换矩阵
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_slant (HomMat2DIdentity, 0.52, 'y', 0, 0, HomMat2DSlant)
affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')

4.6 根据点和角度计算刚性仿射变换矩阵

vector_angle_to_rigid(::Row1,Column1,Angle1,Row2,Column2,Angle2:HomMat2D)

功能:根据点和角度计算刚性仿射变换矩阵,支持旋转和平移

 Row1(输入参数):原始点行坐标
  Column1(输入参数):原始点列坐标
  Angle1(输入参数):原始点角度
  Row2(输入参数):变换的目的点行坐标
  Column2(输入参数):变换的目的点列坐标
  Angle2(输入参数):变换的目的点角度
  HomMat2D(输出参数):输出仿射变换矩阵

dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, ‘C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png’)
area_center (Image, Area, Row, Column)
orientation_region (Image, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(-180), HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, ‘constant’, ‘false’)

4.7 实战Demo(把车牌号码转正)

dev_clear_window ()
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/5.png')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 105, 150)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column','width'], 'and', [0,112.1,191.31,6.57], [506.57,150.56,355.53,51.59])
opening_circle (SelectedRegions, RegionClosing, 1)
union1 (RegionClosing, RegionUnion)
shape_trans (RegionUnion, RegionTrans, 'rectangle2')
* 获取当前图片中心点及对应面积
area_center (RegionUnion, Area, Row, Column)
* 获取图片原始弧度
orientation_region (RegionUnion, Phi)
* 获取仿射变换矩阵
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
* 仿射变换图片
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')

5.根据多个以上特征点计算仿射变换矩阵

5.1 根据两个以上特征点计算仿射变换矩阵

 vector_to_rigid(::Px,Py,Qx,Qy:HomMat2D)
   功能:根据两个以上点对计算计算刚性仿射变换矩阵,支持旋转和平移
    Px:(输入参数)原始点组的x坐标
    Py:(输入参数)原始点组的y坐标
    Qx:(输入参数)变换的目的点组的x坐标
    Qy:输入参数)变换的目的点组的y坐标
    HomMat2D:(输出参数)输出仿射变换矩阵
* This program gives an example of how to use vector_to_rigid.
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 500, 500, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_part (0, 0, 499, 499)
* To generate a set of points from which we can construct a transformation,
* we will use a synthetic ellipse and read out its coordinates.
gen_ellipse_contour_xld (ContEllipse, 200, 200, rad(0), 150, 100, rad(0), rad(360), 'positive', 1)
get_contour_xld (ContEllipse, Rows1, Cols1)
* Now we will transform the ellipse by a known transformation and add some
* noise to the coordinates.
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, rad(30), 0, 0, HomMat2DRotate)
hom_mat2d_translate (HomMat2DRotate, 200, 100, HomMat2DTranslate)
affine_trans_point_2d (HomMat2DTranslate, Rows1, Cols1, Rows2, Cols2)
gen_contour_polygon_xld (Contour, Rows2, Cols2)
add_noise_white_contour_xld (Contour, NoisyContours, 5, 5)
* We will read out the coordinates of the noisy ellipse and use it to construct
* the transformation.  Note that by his approach we know exactly which points
* correspond to each other.  In a real application, the step of determining
* the correspondences is usually the hard part.
get_contour_xld (NoisyContours, Rows2, Cols2)
* Now we can determine the rigid transformation that best approximates the
* the point correspondences.
vector_to_rigid (Rows1, Cols1, Rows2, Cols2, HomMat2D)
* Finally, we transform the original contour by the calculated transformation
* to get an impression of how accurate the calculated transformation is
* (but before that, correct the coordinate system).
hom_mat2d_translate (HomMat2D, 0.5, 0.5, HomMat2DTmp)
hom_mat2d_translate_local (HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)
affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DAdapted)

5.2 根据三个以上特征点获取仿射变换矩阵

 vector_to_hom_mat2d(::Px,Py,Qx,Qy:HomMat2D)

功能:根据三个以上点对计算仿射变换矩阵,支持旋转、平移、缩放、斜切

Px:(输入参数)原始点组的x坐标
Py:(输入参数)原始点组的y坐标
Qx:(输入参数)变换的目的点组的x坐标
Qy:(输入参数)变换的目的点组的y坐标
HomMat2D:(输出参数)输出仿射变换矩阵
*图像坐标
r:=[431, 355, 507, 53, 507]
c:=[505, 543, 316, 127, 883]
*物理坐标(例如机器人坐标)
r1:=[0, 2.0, -2.0, 10, -2.0]
c1:=[0, 1.0, -5.0, -10, 10]

row:=670
col:=789
vector_to_hom_mat2d (r, c, r1,c1 , HomMat2D)
* 图像坐标仿射为物理坐标(例如机器人坐标)
affine_trans_point_2d (HomMat2D, row,col, Qx, Qy)

hom_mat2d_invert (HomMat2D, HomMat2DInvert)
* 物理坐标仿射为图像坐标
affine_trans_point_2d (HomMat2DInvert, Qx, Qy, Px, Py)

5.3 计算仿射变换参数

hom_mat2d_to_affine_par( : : HomMat2D : Sx, Sy, Phi, Theta, Tx, Ty)   

功能:根据仿射变换矩阵(齐次二维变换矩阵)计算仿射变换参数

   HomMat2D   (输入参数):仿射变换矩阵
    Sx          (输出参数):x方向的缩放因子(如果从图像空间变换到物理空间,就是x方向的像素单量)
    Sy          (输出参数):y方向的缩放因子(如果从图像空间变换到物理空间,就是y方向的像素单量)
    Phi         (输出参数):旋转角度
    Theta       (输出参数):斜切角度
    Tx          (输出参数):沿x方向平移的距离
    Ty          (输出参数):沿y方向平移的距离
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, 0.78, 10, 10, HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, 2, 2, 0, 0, HomMat2DScale)
hom_mat2d_to_affine_par (HomMat2DScale, Sx, Sy, Phi, Theta, Tx, Ty)

6.对图像/Region/Xld进行仿射变换

6.1 对XLD进行仿射变换

affine_trans_contour_xld(Contours : ContoursAffinTrans : HomMat2D : )

功能:对XLD轮廓进行二维仿射变换 (支持缩放,旋转,平移,斜切)

   Contours(输入参数):输入XLD轮廓
   ContoursAffinTrans(输出参数):输出变换的XLD轮廓
   HomMat2D(输入参数):仿射变换矩阵
read_image (Image, 'printer_chip/printer_chip_01')
threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 25000, 100000)
select_obj (SelectedRegions, ObjectSelected, 1)
gen_contour_region_xld (ObjectSelected, Contours, 'border')
*将区域移动到图像中心
area_center_xld (Contours, Area, Row, Column, PointOrder)
orientation_xld (Contours, Phi)
get_image_size (Image, Width, Height)
vector_angle_to_rigid (Row, Column, Phi, Height/2, Width/2, Phi, HomMat2D)
affine_trans_contour_xld (Contours, ContoursAffineTrans, HomMat2D)
dev_display (Image)
dev_display (ContoursAffineTrans)

6.2 对image进行仿射变换

affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : )

功能:对图像轮廓进行二维仿射变换 (支持缩放、旋转、平移,斜切)
Image (输入参数):输入图像

ImageAffinTrans (输出参数):变换后的图像
HomMat2D      (输入参数):仿射变换矩阵
Interpolation    (输入参数):插值算法。参数值列表 nearest_neighbor,bilinear,constant,weighted
AdaptImageSize  (输入参数):结果图像尺寸是否自适应。默认值:false
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 212, 255)
erosion_circle (Regions, RegionErosion, 1)
connection (RegionErosion, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column','area'], 'and', [0,173.61,320.7,0], [717.59,189.35,538.82,717.59])
union1 (SelectedRegions, RegionUnion)
shape_trans (RegionUnion, RegionTrans, 'rectangle2')
gen_contour_region_xld (RegionTrans, Contours, 'border')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
affine_trans_contour_xld (Contours, ContoursAffineTrans, HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')

6.3 对region进行仿射变换

affine_trans_region(Region : RegionAffineTrans : HomMat2D, Interpolate : ) 

功能:对区域进行任意二维仿射变换

Region              (输入参数):输入区域
RegionAffineTrans   (输出参数):变换的区域
HomMat2D            (输入参数):仿射变换矩阵
Interpolate          (输入参数):插值算法。默认值:nearest_neighbor。参数值列表:constant,nearest_neighbor
dev_clear_window ()
dev_get_window (WindowHandle)
read_image (Image, 'C:/Users/Gerry/Pictures/Saved Pictures/halcon/OCM识别.png')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 212, 255)
erosion_circle (Regions, RegionErosion, 1)
connection (RegionErosion, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column','area'], 'and', [0,173.61,320.7,0], [717.59,189.35,538.82,717.59])
union1 (SelectedRegions, RegionUnion)
shape_trans (RegionUnion, RegionTrans, 'rectangle2')
gen_contour_region_xld (RegionTrans, Contours, 'border')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
affine_trans_contour_xld (Contours, ContoursAffineTrans, HomMat2D)
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
*affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')

6.4 对polygon_xld进行仿射变换

affine_trans_polygon_xld(Polygon:PolygonsAffinTrans:HomMat2D:)

功能:对XLD多边形进行任意二维仿射变换

Polygon(输入参数):输入XLD多边形
PolygonsAffinTrans(输出参数):变换的XLD多边形
HomMat2D(输入参数):仿射变换矩阵
gen_region_polygon_filled(reg,[100,50,50,100,300,300,300,100],[50,100,200,400,400,200,50,50])
gen_contour_region_xld (reg, Contours, 'border')
area_center_xld (Contours, Area, Row, Column, PointOrder)
orientation_xld (Contours, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, Phi, HomMat2D)
hom_mat2d_scale (HomMat2D, 0.5, 0.5, Column, Row, HomMat2DScale)
affine_trans_contour_xld (Contours, ContoursAffineTrans, HomMat2DScale)
<think>好的,我现在需要帮助用户了解HALCON仿射变换的使用方法或示例代码。根据用户提供的引用资料,我需要整理出清晰的步骤和示例,同时确保符合格式要求,比如行内数学表达式用$...$,独立公式用$$...$$,并且生成相关问题。 首先,用户提到的引用[2]和[3]指出,HALCON仿射变换包括旋转、缩放、平移等,并且有两种使用方式:已知原图和变换矩阵生成结果图,或者通过原图和结果图求变换矩阵。引用[4]提到了缩放矩阵的生成函数hom_mat2d_scale,可能还有其他函数如hom_mat2d_rotate和hom_mat2d_translate。 接下来,我需要将这些信息结构化。步骤可能包括创建变换矩阵、应用变换、组合变换等。每个步骤需要对应的HALCON函数和示例代码。例如,旋转矩阵的生成可以用hom_mat2d_rotate,缩放用hom_mat2d_scale,然后使用affine_trans_image应用变换。 需要注意数学表达式的正确性。例如,旋转矩阵的公式应该是: $$ \begin{bmatrix} \cosθ & -\sinθ & 0 \\ \sinθ & \cosθ & 0 \\ 0 & 0 & 1 \end{bmatrix} $$ 而缩放矩阵是: $$ \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} $$ 这些需要用独立公式格式。 然后,示例代码部分需要展示如何生成变换矩阵并应用,可能需要组合多个变换,比如先旋转再缩放。同时,需要说明变换矩阵的参数,比如旋转角度、缩放因子、中心坐标等。 还需要检查是否有其他相关函数或注意事项,比如使用hom_mat2d_identity初始化单位矩阵,或者如何计算变换后的坐标。例如,$(x,y)$变换后的坐标计算为: $$ x' = h_{11}x + h_{12}y + h_{13} \\ y' = h_{21}x + h_{22}y + h_{23} $$ 这需要作为行内公式呈现。 最后,生成相关问题时要基于用户可能遇到的疑问,比如如何组合多个变换、如何处理不同变换顺序的影响、如何求逆变换等。 确保所有数学表达式格式正确,引用标注在适当的位置。例如,在解释仿射变换的应用时,引用[3]提到的两种使用方式需要标注。同时,示例代码中的函数调用如hom_mat2d_scale来自引用[4],需要正确标注。 现在,将这些整理成结构化的回答,分步骤说明使用方法,给出代码示例,并附上相关问题。</think>### HALCON仿射变换使用方法详解 HALCON中的仿射变换通过变换矩阵实现几何变换,支持旋转、缩放、平移等操作[^2][^3]。以下是具体使用步骤: #### 一、核心数学原理 仿射变换矩阵形式为: $$ H = \begin{bmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ 0 & 0 & 1 \end{bmatrix} $$ 任意$(x,y)$经过变换后坐标为: $$x' = h_{11}x + h_{12}y + h_{13},\quad y' = h_{21}x + h_{22}y + h_{23}$$ #### 二、操作步骤及示例代码 1. **创建单位矩阵** ```halcon hom_mat2d_identity(HomMat2DIdentity) //初始化单位矩阵 ``` 2. **生成旋转矩阵** 旋转角度$\theta=30°$,绕$(256,256)$旋转: ```halcon hom_mat2d_rotate(HomMat2DIdentity, rad(30), 256, 256, HomMat2DRotate) ``` 对应的旋转矩阵: $$ \begin{bmatrix} \cosθ & -\sinθ & (1-\cosθ)*cx + \sinθ*cy \\ \sinθ & \cosθ & -\sinθ*cx + (1-\cosθ)*cy \\ 0 & 0 & 1 \end{bmatrix} $$ 3. **生成缩放矩阵** 缩放因子$(s_x=1.5, s_y=1.5)$,中心$(256,256)$: ```halcon hom_mat2d_scale(HomMat2DRotate, 1.5, 1.5, 256, 256, HomMat2DScale) ``` 缩放矩阵: $$ \begin{bmatrix} s_x & 0 & (1-s_x)*cx \\ 0 & s_y & (1-s_y)*cy \\ 0 & 0 & 1 \end{bmatrix} ```[^4] 4. **应用变换矩阵** ```halcon affine_trans_image(Image, ImageAffinTrans, HomMat2DScale, 'constant', 'false') ``` #### 三、组合变换示例 实现先旋转30°再缩放1.5倍的复合变换: ```halcon hom_mat2d_identity(HomMat2D) hom_mat2d_rotate(HomMat2D, rad(30), 256, 256, HomMat2DRotate) hom_mat2d_scale(HomMat2DRotate, 1.5, 1.5, 256, 256, HomMat2DCompose) affine_trans_image(Image, ImageTransformed, HomMat2DCompose, 'constant', 'true') ``` #### 四、变换矩阵计算(反向求解) 已知变换前后的坐标对应,可求变换矩阵: ```halcon vector_to_hom_mat2d([X1,Y1,X2,Y2], [X1',Y1',X2',Y2'], HomMat2D) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱凤的小光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值