Halcon二維仿射變換實例探究

轉:https://www.cnblogs.com/xh6300/p/7442164.html

Halcon二維仿射變換實例探究

二維仿射變換,顧名思義就是在二維平面內,對對象進行平移、旋轉、縮放等變換的行爲(當然還有其他的變換,這裏僅論述這三種最常見的)。

 

Halcon中進行仿射變換的常見步驟如下:

① 通過hom_mat2d_identity算子創建一個初始化矩陣(即[1.0, 0.0, 0.0, 0.0, 1.0, 0.0]);

② 在初始化矩陣的基礎上,使用hom_mat2d_translate(平移)、hom_mat2d_rotate(旋轉)、hom_mat2d_scale(縮放)等生成仿射變換矩陣;(這幾個算子可以疊加或者重複使用

③ 根據生成的變換矩陣執行仿射變換,執行仿射變換的算子通常有:affine_trans_image、affine_trans_region、affine_trans_contour_xld,即不管對於圖像、區域、XLD都可以執行仿射變換。

 

下面用一個完整程序分別展示hom_mat2d_translate(平移)、hom_mat2d_rotate(旋轉)、hom_mat2d_scale(縮放)這三個算子的的具體功能。(特別要注意程序註釋部分)

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

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

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

 

程序所用圖片如下:

複製代碼

 1 read_image (Image, 'hogn-1.jpg')
 2 threshold (Image, Region, 0, 200)
 3 opening_circle (Region, Region, 1.5)
 4 connection (Region, ConnectedRegions)
 5 select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
 6 *得到變換的中心點
 7 area_center (SelectedRegions, Area, Row, Column)
 8 dev_set_draw ('margin')
 9 
10 *hom_mat2d_translate中的兩個參數的意思是:Tx和Ty分別代表Row方向和Column方向的平移量
11 dev_display (Image)
12 disp_cross (3600, Row, Column, 10, 40)
13 hom_mat2d_identity (HomMat2DIdentity)
14 hom_mat2d_translate (HomMat2DIdentity,30, 150, HomMat2DTranslate)
15 affine_trans_region (Region, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')
16 
17 *hom_mat2d_rotate中的三個參數的意思是:旋轉角度(逆時針爲正,弧度制),旋轉中心的row和column值
18 dev_display (Image)
19 disp_cross (3600, Row, Column, 10, 40)
20 hom_mat2d_rotate (HomMat2DIdentity, rad(20), Row, Column, HomMat2DRotate)
21 affine_trans_region (Region, RegionAffineTrans, HomMat2DRotate, 'nearest_neighbor')
22 
23 *hom_mat2d_scale中的四個參數的意思是:Sx和Sy分別代表Row方向和Column方向的縮放係數,縮放中心的row和column值
24 dev_display (Image)
25 disp_cross (3600, Row, Column, 10, 40)
26 hom_mat2d_scale (HomMat2DIdentity, 2.0, 1.05, Row, Column, HomMat2DScale)
27 affine_trans_region (Region, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')
1 read_image (Image, 'hogn-1.jpg')
 2 threshold (Image, Region, 0, 200)
 3 opening_circle (Region, Region, 1.5)
 4 connection (Region, ConnectedRegions)
 5 select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
 6 *得到變換的中心點
 7 area_center (SelectedRegions, Area, Row, Column)
 8 dev_set_draw ('margin')
 9 
10 *hom_mat2d_translate中的兩個參數的意思是:Tx和Ty分別代表Row方向和Column方向的平移量
11 dev_display (Image)
12 disp_cross (3600, Row, Column, 10, 40)
13 hom_mat2d_identity (HomMat2DIdentity)
14 hom_mat2d_translate (HomMat2DIdentity,30, 150, HomMat2DTranslate)
15 affine_trans_region (Region, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')
16 
17 *hom_mat2d_rotate中的三個參數的意思是:旋轉角度(逆時針爲正,弧度制),旋轉中心的row和column值
18 dev_display (Image)
19 disp_cross (3600, Row, Column, 10, 40)
20 hom_mat2d_rotate (HomMat2DIdentity, rad(20), Row, Column, HomMat2DRotate)
21 affine_trans_region (Region, RegionAffineTrans, HomMat2DRotate, 'nearest_neighbor')
22 
23 *hom_mat2d_scale中的四個參數的意思是:Sx和Sy分別代表Row方向和Column方向的縮放係數,縮放中心的row和column值
24 dev_display (Image)
25 disp_cross (3600, Row, Column, 10, 40)
26 hom_mat2d_scale (HomMat2DIdentity, 2.0, 1.05, Row, Column, HomMat2DScale)
27 affine_trans_region (Region, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')

效果分別如下:

 

 

有時候,並不需要創建初始化矩陣也可以執行仿射變換,例如vector_angle_to_rigid算子就是如此。

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

 

該算子意思是:先將圖像旋轉,旋轉角度爲(Angle2 - Angle1) (逆時針爲正),旋轉中心座標是(Row1, Column1)。再將原圖的點(Row1, Column1)一一對應移到點 (Row2, Column2)上,移動的row和column方向的位移分別是( Row2 - Row1)、( Column2 - Column1),
 

如果Row1 = Row2, Column1 = Column2,那麼就完整等價於旋轉變換。可以執行下面的程序感受一下:

複製代碼

 1 read_image (Image, 'hogn-1.jpg')
 2 Row := 100
 3 Column := 200
 4 dev_display (Image)
 5 
 6 
 7 for Index := 1 to 150 by 1  
 8     vector_angle_to_rigid (Row, Column, 0, Row, Column, rad(10), HomMat2D)
 9     disp_cross (3600, 100, 200, 10, 40)
10     affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'nearest_neighbor', 'false')
11     copy_image (ImageAffinTrans, Image) 
12 endfor

複製代碼

 

可以將vector_angle_to_rigid理解爲同時執行旋轉變換和平移變換。最難弄明白的是旋轉中心是什麼?下面的程序可以說明如果先旋轉後平移,那麼旋轉中心是(Row1, Column1),而不是 (Row2, Column2)。(如果先平移後旋轉,那麼結論剛好相反,大家可以試試)

複製代碼

 1 read_image (Image, 'hogn-1.jpg')
 2 Row1 := 100
 3 Column1 := 100
 4 
 5 Row2 := 100
 6 Column2 := 200
 7 dev_display (Image)
 8 *用vector_angle_to_rigid實現縮放、平移
 9 vector_angle_to_rigid (Row1, Column1, 0, Row2, Column2, rad(10), HomMat2D)
10 affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'nearest_neighbor', 'false')
11 
12 *分兩步依次執行縮放、平移
13 hom_mat2d_identity (HomMat2DIdentity)
14 hom_mat2d_rotate (HomMat2DIdentity, rad(10) - 0, Row1, Column1, HomMat2DRotate)
15 hom_mat2d_translate (HomMat2DRotate,Row2 - Row1, Column2 - Column1, HomMat2DTranslate)
16 *觀察圖像ImageAffinTrans和ImageAffinTrans_2能夠完全重合
17 affine_trans_image (Image, ImageAffinTrans_2, HomMat2DTranslate, 'nearest_neighbor', 'false')
18 
19 disp_cross (3600, Row1, Column1, 10, 40)

複製代碼

 

vector_angle_to_rigid最常用到的場合一般是模板匹配之類的算法場合,通常用在find_shape_model等算子後面。

 

下面用一個例子說明一下仿射變換的綜合應用,即當圖片旋轉90°時,想辦法變換Region使之能夠翻轉到對應的位置。

將圖片順時針翻轉90°的方法可以是:rotate_image (image, ImageRotate, -90, 'constant')。

但其實它不僅經過了旋轉變換、還進行了平移變換,最明顯的證據就是:翻轉前後的圖像,他們的中心點座標不一樣。完整程序如下:

複製代碼

 1 read_image (image, 'C:/Users/happy xia/Desktop/dynPic.png')
 2 binary_threshold (image, Region, 'max_separability', 'dark', UsedThreshold)
 3 dev_set_draw ('margin')
 4 connection (Region, ConnectedRegions)
 5 select_shape_std (ConnectedRegions, SelectedReg, 'max_area', 70)
 6 area_center (image, Area, Row, Column)
 7 
 8 rotate_image (image, ImageRotate, -90, 'constant')
 9 area_center (ImageRotate, Area2, Row2, Column2)
10 
11 hom_mat2d_identity (HomMat2DIdentity)
12 hom_mat2d_rotate (HomMat2DIdentity, -rad(90), Row, Column, HomMat2DRotate)
13 hom_mat2d_translate (HomMat2DRotate,Row2 - Row, Column2 - Column, HomMat2DTranslate)
14 
15 affine_trans_region (SelectedReg, RegionAffineTrans, HomMat2DTranslate, 'constant')

複製代碼

該算法順利達到了目的——圖像翻轉以後,原先生成的Region也翻轉到了對應的位置。

注意:用rotate_image 算子旋轉圖像時,如果旋轉角度不是0°、90°、180°、270°等角度,那麼圖像其實只做了旋轉變換,而沒有進行平移變換。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章