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

发布于:2025-05-27 ⋅ 阅读:(47) ⋅ 点赞:(0)

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)