【Halcon學(xué)習(xí)筆記】10.仿射變換(2D)
1.概述
????????仿射變換,即在二維平面內(nèi),對(duì)象進(jìn)行平移(Translation)、縮放(Scale)、翻轉(zhuǎn)(Flip)、旋轉(zhuǎn)(Rotation)和剪切(Shear)等操作。
????????仿射變換可以保持原來(lái)的線共點(diǎn)、點(diǎn)共線的關(guān)系不變;保持原來(lái)相互平行的線仍然平行;保持原來(lái)的中點(diǎn)仍然是中點(diǎn);保持原來(lái)在一直線上幾段線段之間的比例關(guān)系不變;但不能保持原來(lái)的線段長(zhǎng)度和夾角角度不變。

2.相關(guān)算子
2.1 創(chuàng)建(創(chuàng)建一個(gè)空的二維空變換矩陣)
1)hom_mat2d_identity()
????????HomMat2D:變換矩陣
2.2 添加(添加“旋轉(zhuǎn)/縮放/平移”到變換矩陣)
1)hom_mat2d_rotate()
????????HomMat2D:輸入的變換矩陣
????????Phi:旋轉(zhuǎn)角度
????????Px:旋轉(zhuǎn)中心x坐標(biāo)
????????Py:旋轉(zhuǎn)中心y坐標(biāo)
????????HomMat2DRotate:輸出的變換矩陣
2)hom_mat2d_scale()? ?
????????HomMat2D:輸入的變換矩陣
????????Sx:沿x軸縮放比例
????????Sy:沿y軸縮放比例
????????Px:縮放中心x坐標(biāo)
????????Py:縮放中心y坐標(biāo)
????????HomMat2DScale:輸出的變換矩陣
3)hom_mat2d_translate()
????????HomMat2D:輸入的變換矩陣
????????Tx:沿x軸平移量
????????Ty:沿y軸平移量
????????HomMat2DTranslate:輸出的變換矩陣
2.3?計(jì)算(計(jì)算變換矩陣)
1)vector_angle_to_rigid()????????//根據(jù)2個(gè)點(diǎn)坐標(biāo)和角度計(jì)算變換矩陣(旋轉(zhuǎn)/平移)
????????Row1:原點(diǎn)行坐標(biāo)
????????Column1:原點(diǎn)列坐標(biāo)
????????Angle1:原點(diǎn)角度
????????Row2:變換點(diǎn)行坐標(biāo)
????????Column2:變換點(diǎn)列坐標(biāo)
????????Angle2:變換點(diǎn)角度
????????HomMat2D:輸出的變換矩陣
2)?vector_to_rigid()????????//根據(jù)2個(gè)以上點(diǎn)坐標(biāo)計(jì)算變換矩陣(旋轉(zhuǎn)/平移)
????????Px:原點(diǎn)的x坐標(biāo)集
????????Py:原點(diǎn)的y坐標(biāo)集
????????Qx:變換點(diǎn)的x坐標(biāo)集
????????Qy:變換點(diǎn)的y坐標(biāo)集
????????HomMat2D:輸出的變換矩陣
3)vector_to_similarity()?????????//根據(jù)2個(gè)以上點(diǎn)坐標(biāo)計(jì)算變換矩陣(旋轉(zhuǎn)/平移/縮放)
4)vector_to_hom_mat2d()????//根據(jù)3個(gè)以上點(diǎn)坐標(biāo)計(jì)算變換矩陣(旋轉(zhuǎn)/平移/縮放/斜切)
2.4?執(zhí)行(執(zhí)行仿射變換)
1)affine_trans_region()??????????????????//對(duì)區(qū)域進(jìn)行二維仿射變換
????????Region:待變換的額區(qū)域
????????RegionAffineTrans:變換后的區(qū)域
????????HomMat2D:變換矩陣
????????Interpolate:插值方式
2)affine_trans_image()???????????????????//對(duì)圖像進(jìn)行二維仿射變換
3)affine_trans_contour_xld()?????????//對(duì)XLD進(jìn)行二維仿射變換
4)affine_trans_polygon_xld()?????????//對(duì)XLD多邊形進(jìn)行二維仿射變換
5)affine_trans_point_2d()????????????????//對(duì)點(diǎn)進(jìn)行二維仿射變換
6)?affine_trans_pixel()??????????????????????//對(duì)像素進(jìn)行二維仿射變換
2.5?查看(查看仿射變換參數(shù))
1)hom_mat2d_to_affine_par()??
????????HomMat2D:輸入的變換矩陣
????????Sx:沿x軸縮放比例
????????Sy:沿y軸縮放比例
????????Phi:旋轉(zhuǎn)角度
????????Theta:斜切角度
????????Tx:沿x軸平移量
????????Ty:沿y軸平移量
3.仿射變換在Halcon中的應(yīng)用步驟
3.1 場(chǎng)景1:根據(jù)輸入的參數(shù)直接生成變換矩陣
1)創(chuàng)建一個(gè)單位矩陣
????hom_mat2d_identity(HomMat2D)
2)輸入?yún)?shù),生成變換矩陣
????hom_mat2d_translate (HomMat2D, 100, 100, HomMat2DTranslate)? ??
3)對(duì)區(qū)域進(jìn)行仿射變換
????affine_trans_region (Region, RegionTranslate, HomMat2DTranslate, 'nearest_neighbor')

3.2 場(chǎng)景2:根據(jù)已知的參數(shù)(點(diǎn)、角度)計(jì)算生成變換矩陣(區(qū)域跟隨、標(biāo)定)
1)設(shè)定模板圖片的標(biāo)記點(diǎn),RowM, ColM
????read_image (ImageModel, path+'/model.jpg')??
????threshold (ImageModel, Region, 0, 50)
????connection (Region, ConnectedRegions)
????select_shape (ConnectedRegions, SelectedRegions, ['area','width'], 'and', [3000,40], [6000,70])
????opening_circle (SelectedRegions, RegionOpeningM, 9.5)
????area_center (RegionOpeningM, AreaM, RowM, ColM)
????elliptic_axis (RegionOpeningM, Ra, Rb, PhiM)
????dev_display (ImageModel)
????disp_cross (WindowHandle, RowM, ColM, 100, 0)
????disp_message(WindowHandle, 'MarkModel:'+RowM$'.0f'+','+ColM$'.0f', 'window', 0,0, 'green', 'false')

2)設(shè)定基于標(biāo)記點(diǎn)偏移的區(qū)域,RoiM
????gen_rectangle2 (RoiM, 1026.82, 1137.46, rad(-0.150432), 177.281, 35.8157)

3)在新的圖像上求出標(biāo)記點(diǎn),Row, Col
????read_image (Image, path+'/detect.jpg')??
? ? threshold (Image, Region, 0, 50)
? ? connection (Region, ConnectedRegions)
? ? select_shape (ConnectedRegions, SelectedRegions, ['area','width'], 'and',[1000,20], [8000,90])
? ? opening_circle (SelectedRegions, RegionOpening, 9.5)
? ? area_center (RegionOpening, Area, Row, Col)
? ? elliptic_axis (RegionOpening, Ra, Rb, Phi)
? ? dev_display (Image)
? ? disp_cross (WindowHandle, Row, Col, 100, 0)
? ? disp_message(WindowHandle, 'MarkImage:'+Row$'.0f'+','+Col$'.0f', 'window', 0,0, 'green', 'false')

4)根據(jù)(RowM, ColM, PhiM)和(RowM, ColM, Phi)求出變換矩陣,HomMat2D
????hom_mat2d_identity (HomMat2D)
????vector_angle_to_rigid (RowM, ColM, PhiM, Row, Col, Phi, HomMat2D)
5)對(duì)RoiM進(jìn)行仿射變換
? ? affine_trans_region (RoiM, Roi, HomMat2D, 'nearest_neighbor')??


by Ryou2-