文章目錄
- 1.概述
- 2.仿射變換類型
- 3.仿射變換流程
- 4.根據特征點、角度計算仿射變換矩陣
- 4.1 從空變換矩陣創建仿射變換矩陣
- 4.2 把旋轉角度添加到仿射變換矩陣
- 4.3 把縮放添加到仿射變換矩陣
- 4.4 把平移添加到防射變換矩陣
- 4.5 把斜切添加到仿射變換矩陣
- 4.6 根據點和角度計算剛性仿射變換矩陣
- 4.7 實戰Demo(把車牌號碼轉正)
- 5.根據多個以上特征點計算仿射變換矩陣
- 5.1 根據兩個以上特征點計算仿射變換矩陣
- 5.2 根據三個以上特征點獲取仿射變換矩陣
- 5.3 計算仿射變換參數
- 6.對圖像/Region/Xld進行仿射變換
- 6.1 對XLD進行仿射變換
- 6.2 對image進行仿射變換
- 6.3 對region進行仿射變換
- 6.4 對polygon_xld進行仿射變換
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,yPx (輸入參數):變換的固定點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)