下一篇12p_国产精品白浆无码浪潮av_国产精品videossex国产高清_亚洲 制服 丝袜日韩 第一页_97久久精品无码一区二区三区

您當前的位置:首頁 >> 快訊 >  >> 
【愚公系列】2023年04月 Halcon機器視覺-仿射變換詳解
來源: 騰訊云      時間:2023-04-09 20:22:06


(相關(guān)資料圖)

文章目錄

一、概述二、仿射變換類型三、仿射變換流程四、根據(jù)特征點、角度計算仿射變換矩陣1.1 從空變換矩陣創(chuàng)建仿射變換矩陣1.2 把旋轉(zhuǎn)角度添加到仿射變換矩陣1.3 把縮放添加到仿射變換矩陣1.4 把平移添加到防射變換矩陣1.5 把斜切添加到仿射變換矩陣1.6 根據(jù)點和角度計算剛性仿射變換矩陣1.7 實戰(zhàn)Demo(把車牌號碼轉(zhuǎn)正)五、根據(jù)多個特征點計算仿射變換矩陣1.1 根據(jù)兩個以上特征點計算仿射變換矩陣1.2 根據(jù)三個以上特征點獲取仿射變換矩陣1.3 計算仿射變換參數(shù)六、對圖像、region和XLD進行仿射變換1.1 對XLD進行仿射變換1.2 對image進行仿射變換1.3 對region進行仿射變換1.4 對polygon_xld進行仿射變換

一、概述

仿射變換,即在二維平面內(nèi),對象進行平移(Translation)、縮放(Scale)、翻轉(zhuǎn)(Flip)、旋轉(zhuǎn)(Rotation)和斜切(Shear)等操作。

仿射變換可以保持原來的線共點、點共線的關(guān)系不變;保持原來相互平行的線仍然平行;保持原來的中點仍然是中點;保持原來在一直線上幾段線段之間的比例關(guān)系不變;但不能保持原來的線段長度和夾角角度不變。

二、仿射變換類型

仿射變換有:平移、旋轉(zhuǎn)、縮放、斜切(就是將斜體字導(dǎo)正)。

三、仿射變換流程

(1.)獲取特征點坐標、角度

(2.)計算仿射變換矩陣

(3.)對圖像、區(qū)域、輪廓進行仿射變換

四、根據(jù)特征點、角度計算仿射變換矩陣

1.1 從空變換矩陣創(chuàng)建仿射變換矩陣

hom_mat2d_identity( : : : HomMat2DIdentity)功能:產(chǎn)生仿射變換矩陣(產(chǎn)生一個空的二維空變換矩陣)
* 清理窗口dev_clear_window ()* 讀取圖片read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")* 生成一個仿射變換矩陣(齊次矩陣)hom_mat2d_identity (HomMat2DIdentity)* 二值化rgb1_to_gray (Image, GrayImage)* 按照rgb仿射拆成3個通道decompose3 (Image, r, g, b)* 創(chuàng)建一個縮放得仿射變換矩陣hom_mat2d_scale (HomMat2DIdentity, 0.5, 0.5, 0, 0, HomMat2DScale)* 仿射變換圖片affine_trans_image (Image, ImageAffineTrans, HomMat2DScale, "constant", "false")

1.2 把旋轉(zhuǎn)角度添加到仿射變換矩陣

hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)   //功能:把旋轉(zhuǎn)角度添加到仿射變換矩陣    HomMat2D :(輸入?yún)?shù))仿射變換矩陣    Phi  :旋轉(zhuǎn)角度(單位弧度)    Px   :變換的固定點行坐標。固定點是指以該點為支撐進行仿射變換  (這里是指圍繞這點進行旋轉(zhuǎn))    Py :  變換的固定點列坐標    HomMat2DRotate:輸出的旋轉(zhuǎn)變換的二維矩陣
dev_clear_window ()dev_get_window (WindowHandle)read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_rotate (HomMat2DIdentity, rad(90), 200, 200, HomMat2DRotate)affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, "constant", "false")

1.3 把縮放添加到仿射變換矩陣

hom_mat2d_scale( : : HomMat2D, Sx, Sy, Px, Py : HomMat2DScale)  //把縮放添加到仿射變換矩陣   HomMat2D(輸入?yún)?shù)):仿射變換矩陣   Sx(輸入?yún)?shù)):x軸方向的縮放因子   Sy(輸入?yún)?shù)):y軸方向的縮放因子   Px(輸入?yún)?shù)):變換的固定點行坐標   Py(輸入?yún)?shù)):  變換的固定點列坐標   HomMat2DScale(輸出參數(shù)):輸出縮放變換矩陣
dev_clear_window ()dev_get_window (WindowHandle)read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_scale (HomMat2DIdentity, 0.5, 0.5, 0, 0, HomMat2DScale)affine_trans_image (Image, ImageAffineTrans, HomMat2DScale, "constant", "false")

1.4 把平移添加到防射變換矩陣

hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)   功能:把平移添加到防射變換矩陣   HomMat2D:(輸入?yún)?shù))仿射變換矩陣   Tx(輸入?yún)?shù)):沿x軸方向平移的距離   Ty:輸入?yún)?shù)):沿y軸方向平移的距離   HomMat2DTranslate(輸出參數(shù)):輸出變換矩陣
dev_clear_window ()dev_get_window (WindowHandle)read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_translate (HomMat2DIdentity, 200, 200, HomMat2DTranslate)affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, "constant", "false")

1.5 把斜切添加到仿射變換矩陣

hom_mat2d_slant( : : HomMat2D, Theta, Axis, Px, Py : HomMat2DSlant)  功能:把斜切添加到防射變換矩陣   HomMat2D        (輸入?yún)?shù)):仿射變換矩陣   Theta           (輸入?yún)?shù)):斜切角度(單位:弧度)   Axis            (輸入?yún)?shù)):斜切的坐標軸。取值列表:x,y   Px               (輸入?yún)?shù)):變換的固定點x坐標   Py              (輸入?yún)?shù)):變換的固定點y坐標   HomMat2DSlant  (輸出參數(shù)):輸出斜切仿射變換矩陣
dev_clear_window ()dev_get_window (WindowHandle)read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_slant (HomMat2DIdentity, 0.52, "y", 0, 0, HomMat2DSlant)affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, "constant", "false")

1.6 根據(jù)點和角度計算剛性仿射變換矩陣

vector_angle_to_rigid(::Row1,Column1,Angle1,Row2,Column2,Angle2:HomMat2D)  功能:根據(jù)點和角度計算剛性仿射變換矩陣,支持旋轉(zhuǎn)和平移  Row1(輸入?yún)?shù)):原始點行坐標  Column1(輸入?yún)?shù)):原始點列坐標  Angle1(輸入?yún)?shù)):原始點角度  Row2(輸入?yún)?shù)):變換的目的點行坐標  Column2(輸入?yún)?shù)):變換的目的點列坐標  Angle2(輸入?yún)?shù)):變換的目的點角度  HomMat2D(輸出參數(shù)):輸出仿射變換矩陣
dev_clear_window ()dev_get_window (WindowHandle)read_image (Image, "E:/NetProjects/測試項目/Halcon/素材/chepai1.jpg")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")

1.7 實戰(zhàn)Demo(把車牌號碼轉(zhuǎn)正)

dev_clear_window ()read_image (Image, "C:/Users/Gerry/Pictures/Saved Pictures/halcon/5.png")* 轉(zhuǎn)為灰度圖片rgb1_to_gray (Image, GrayImage)* 二值化threshold (GrayImage, Regions, 105, 151)* 打斷以不同顏色進行呈現(xiàn)connection (Regions, ConnectedRegions)* 在根據(jù)特征篩選,找到車牌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, RegionOpening, 1)* 聯(lián)合所有區(qū)域為一個區(qū)域union1 (RegionOpening, RegionUnion)* 生成一個聯(lián)合區(qū)域矩陣shape_trans (RegionUnion, RegionTrans, "rectangle2")* 1 獲取矩陣中心點位坐標area_center (RegionTrans, Area, Row, Column)* 2 獲取當前矩陣的旋轉(zhuǎn)的原始弧度orientation_region (RegionTrans, Phi)* 生成仿射變換矩陣vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)* 根據(jù)仿射變換矩陣扶正圖片affine_trans_image (Image, ImageAffineTrans, HomMat2D, "constant", "false")

五、根據(jù)多個特征點計算仿射變換矩陣

1.1 根據(jù)兩個以上特征點計算仿射變換矩陣

vector_to_rigid(::Px,Py,Qx,Qy:HomMat2D)    功能:根據(jù)兩個以上點對計算計算剛性仿射變換矩陣,支持旋轉(zhuǎn)和平移    Px:(輸入?yún)?shù))原始點組的x坐標    Py:(輸入?yún)?shù))原始點組的y坐標    Qx:(輸入?yún)?shù))變換的目的點組的x坐標    Qy:輸入?yún)?shù))變換的目的點組的y坐標    HomMat2D:(輸出參數(shù))輸出仿射變換矩陣
* 生成橢圓形gen_ellipse_contour_xld (ContEllipse, 200, 100, rad(0), 70, 95, rad(0), rad(360), "positive", 2.5)get_contour_xld (ContEllipse, Row, Col)* 根據(jù)多個點生成仿射變換矩陣vector_to_rigid (Col, Row, Col, Row, HomMat2D)* 平移hom_mat2d_translate (HomMat2D, 50, 150, HomMat2DTranslate)* 旋轉(zhuǎn)hom_mat2d_rotate (HomMat2DTranslate, rad(90), 244, 257, HomMat2DRotate)* 仿射變換輪廓affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DRotate)
vector_to_similarity(::Px,Py,Qx,Qy:HomMat2D)功能:根據(jù)兩個以上點對計算相似仿射變換矩陣,支持旋轉(zhuǎn)、平移和縮放Px:(輸入?yún)?shù))原始點組的x坐標Py:(輸入?yún)?shù))原始點組的y坐標Qx:(輸入?yún)?shù))變換的目的點組的x坐標Qy:(輸入?yún)?shù))變換的目的點組的y坐標HomMat2D:(輸出參數(shù))輸出仿射變換矩陣
dev_clear_window ()* 生成橢圓形輪廓gen_ellipse_contour_xld (ContEllipse, 200, 100, rad(60), 100, 50, rad(0), rad(306), "positive", 1.5)get_contour_xld (ContEllipse, Row, Col)* 創(chuàng)建一個矩陣hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_translate (HomMat2DIdentity, 200, 150, HomMat2DTranslate)affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DTranslate)hom_mat2d_rotate (HomMat2DTranslate, rad(90), 200, 150, HomMat2DRotate)affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DRotate)affine_trans_point_2d (HomMat2DRotate, Col, Row, Col1, Row1)* 生成一個近似的仿射變換矩陣vector_to_similarity (Col, Row, Col1, Row1, HomMat2D)hom_mat2d_translate (HomMat2D, 200, 150, HomMat2DTranslate)affine_trans_contour_xld (ContEllipse, ContoursAffineTrans1, HomMat2DTranslate)

1.2 根據(jù)三個以上特征點獲取仿射變換矩陣

vector_to_hom_mat2d(::Px,Py,Qx,Qy:HomMat2D)    功能:根據(jù)三個以上點對計算仿射變換矩陣,支持旋轉(zhuǎn)、平移、縮放、斜切    Px:(輸入?yún)?shù))原始點組的x坐標    Py:(輸入?yún)?shù))原始點組的y坐標    Qx:(輸入?yún)?shù))變換的目的點組的x坐標    Qy:(輸入?yún)?shù))變換的目的點組的y坐標    HomMat2D:(輸出參數(shù))輸出仿射變換矩陣
* 圖像坐標r:=[455,343,673,34,53]c:=[354,432,522,32,65]* 物理坐標(機械手坐標)r1:=[0,2,-2,-1,1]c1:=[0,1,-4,-2,10]* 生成仿射變換矩陣vector_to_hom_mat2d (r, c, r1, c1, HomMat2D)* 圖像坐標仿射變換為物理坐標affine_trans_point_2d (HomMat2D, 500, 200, Qx, Qy)* 物理坐標仿射為圖像坐標hom_mat2d_invert (HomMat2D, HomMat2DInvert)affine_trans_point_2d (HomMat2DInvert, Qx, Qy, Qx1, Qy1)

1.3 計算仿射變換參數(shù)

hom_mat2d_to_affine_par( : : HomMat2D : Sx, Sy, Phi, Theta, Tx, Ty)   功能:根據(jù)仿射變換矩陣(齊次二維變換矩陣)計算仿射變換參數(shù)    HomMat2D   (輸入?yún)?shù)):仿射變換矩陣    Sx          (輸出參數(shù)):x方向的縮放因子(如果從圖像空間變換到物理空間,就是x方向的像素單量)    Sy          (輸出參數(shù)):y方向的縮放因子(如果從圖像空間變換到物理空間,就是y方向的像素單量)    Phi         (輸出參數(shù)):旋轉(zhuǎn)角度    Theta       (輸出參數(shù)):斜切角度    Tx          (輸出參數(shù)):沿x方向平移的距離    Ty          (輸出參數(shù)):沿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)

六、對圖像、region和XLD進行仿射變換

1.1 對XLD進行仿射變換

affine_trans_contour_xld(Contours : ContoursAffinTrans : HomMat2D : )功能:對XLD輪廓進行二維仿射變換 (支持縮放,旋轉(zhuǎn),平移,斜切)     Contours(輸入?yún)?shù)):輸入XLD輪廓     ContoursAffinTrans(輸出參數(shù)):輸出變換的XLD輪廓     HomMat2D(輸入?yún)?shù)):仿射變換矩陣
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")*將區(qū)域移動到圖像中心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)

1.2 對image進行仿射變換

affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : ) 功能:對圖像輪廓進行二維仿射變換 (支持縮放、旋轉(zhuǎn)、平移,斜切)Image          (輸入?yún)?shù)):輸入圖像ImageAffinTrans (輸出參數(shù)):變換后的圖像HomMat2D      (輸入?yún)?shù)):仿射變換矩陣Interpolation    (輸入?yún)?shù)):插值算法。參數(shù)值列表 nearest_neighbor,bilinear,constant,weightedAdaptImageSize  (輸入?yún)?shù)):結(jié)果圖像尺寸是否自適應(yīng)。默認值: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")

1.3 對region進行仿射變換

affine_trans_region(Region : RegionAffineTrans : HomMat2D, Interpolate : ) 功能:對區(qū)域進行任意二維仿射變換Region              (輸入?yún)?shù)):輸入?yún)^(qū)域RegionAffineTrans   (輸出參數(shù)):變換的區(qū)域HomMat2D            (輸入?yún)?shù)):仿射變換矩陣Interpolate          (輸入?yún)?shù)):插值算法。默認值:nearest_neighbor。參數(shù)值列表: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")

1.4 對polygon_xld進行仿射變換

affine_trans_polygon_xld(Polygon:PolygonsAffinTrans:HomMat2D:)功能:對XLD多邊形進行任意二維仿射變換   Polygon(輸入?yún)?shù)):輸入XLD多邊形PolygonsAffinTrans(輸出參數(shù)):變換的XLD多邊形HomMat2D(輸入?yún)?shù)):仿射變換矩陣
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)
標簽:

X 關(guān)閉

X 關(guān)閉