世界看热讯:【愚公系列】2023年04月 Halcon机器视觉-仿射变换详解

来源:腾讯云 发布时间:2023-04-08 21:23:54


【资料图】

文章目录

一、概述二、仿射变换类型三、仿射变换流程四、根据特征点、角度计算仿射变换矩阵1.1 从空变换矩阵创建仿射变换矩阵1.2 把旋转角度添加到仿射变换矩阵1.3 把缩放添加到仿射变换矩阵1.4 把平移添加到防射变换矩阵1.5 把斜切添加到仿射变换矩阵1.6 根据点和角度计算刚性仿射变换矩阵1.7 实战Demo(把车牌号码转正)五、根据多个特征点计算仿射变换矩阵1.1 根据两个以上特征点计算仿射变换矩阵1.2 根据三个以上特征点获取仿射变换矩阵1.3 计算仿射变换参数六、对图像、region和XLD进行仿射变换1.1 对XLD进行仿射变换1.2 对image进行仿射变换1.3 对region进行仿射变换1.4 对polygon_xld进行仿射变换

一、概述

仿射变换,即在二维平面内,对象进行平移(Translation)、缩放(Scale)、翻转(Flip)、旋转(Rotation)和斜切(Shear)等操作。

仿射变换可以保持原来的线共点、点共线的关系不变;保持原来相互平行的线仍然平行;保持原来的中点仍然是中点;保持原来在一直线上几段线段之间的比例关系不变;但不能保持原来的线段长度和夹角角度不变。

二、仿射变换类型

仿射变换有:平移、旋转、缩放、斜切(就是将斜体字导正)。

三、仿射变换流程

(1.)获取特征点坐标、角度

(2.)计算仿射变换矩阵

(3.)对图像、区域、轮廓进行仿射变换

四、根据特征点、角度计算仿射变换矩阵

1.1 从空变换矩阵创建仿射变换矩阵

hom_mat2d_identity( : : : HomMat2DIdentity)功能:产生仿射变换矩阵(产生一个空的二维空变换矩阵)
* 清理窗口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)* 创建一个缩放得仿射变换矩阵hom_mat2d_scale (HomMat2DIdentity, 0.5, 0.5, 0, 0, HomMat2DScale)* 仿射变换图片affine_trans_image (Image, ImageAffineTrans, HomMat2DScale, "constant", "false")

1.2 把旋转角度添加到仿射变换矩阵

hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)   //功能:把旋转角度添加到仿射变换矩阵    HomMat2D :(输入参数)仿射变换矩阵    Phi  :旋转角度(单位弧度)    Px   :变换的固定点行坐标。固定点是指以该点为支撑进行仿射变换  (这里是指围绕这点进行旋转)    Py :  变换的固定点列坐标    HomMat2DRotate:输出的旋转变换的二维矩阵
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(输入参数):仿射变换矩阵   Sx(输入参数):x轴方向的缩放因子   Sy(输入参数):y轴方向的缩放因子   Px(输入参数):变换的固定点行坐标   Py(输入参数):  变换的固定点列坐标   HomMat2DScale(输出参数):输出缩放变换矩阵
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:(输入参数)仿射变换矩阵   Tx(输入参数):沿x轴方向平移的距离   Ty:输入参数):沿y轴方向平移的距离   HomMat2DTranslate(输出参数):输出变换矩阵
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        (输入参数):仿射变换矩阵   Theta           (输入参数):斜切角度(单位:弧度)   Axis            (输入参数):斜切的坐标轴。取值列表:x,y   Px               (输入参数):变换的固定点x坐标   Py              (输入参数):变换的固定点y坐标   HomMat2DSlant  (输出参数):输出斜切仿射变换矩阵
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 根据点和角度计算刚性仿射变换矩阵

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, "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 实战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, 151)* 打断以不同颜色进行呈现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, RegionOpening, 1)* 联合所有区域为一个区域union1 (RegionOpening, RegionUnion)* 生成一个联合区域矩阵shape_trans (RegionUnion, RegionTrans, "rectangle2")* 1 获取矩阵中心点位坐标area_center (RegionTrans, Area, Row, Column)* 2 获取当前矩阵的旋转的原始弧度orientation_region (RegionTrans, Phi)* 生成仿射变换矩阵vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)* 根据仿射变换矩阵扶正图片affine_trans_image (Image, ImageAffineTrans, HomMat2D, "constant", "false")

五、根据多个特征点计算仿射变换矩阵

1.1 根据两个以上特征点计算仿射变换矩阵

vector_to_rigid(::Px,Py,Qx,Qy:HomMat2D)    功能:根据两个以上点对计算计算刚性仿射变换矩阵,支持旋转和平移    Px:(输入参数)原始点组的x坐标    Py:(输入参数)原始点组的y坐标    Qx:(输入参数)变换的目的点组的x坐标    Qy:输入参数)变换的目的点组的y坐标    HomMat2D:(输出参数)输出仿射变换矩阵
* 生成椭圆形gen_ellipse_contour_xld (ContEllipse, 200, 100, rad(0), 70, 95, rad(0), rad(360), "positive", 2.5)get_contour_xld (ContEllipse, Row, Col)* 根据多个点生成仿射变换矩阵vector_to_rigid (Col, Row, Col, Row, HomMat2D)* 平移hom_mat2d_translate (HomMat2D, 50, 150, HomMat2DTranslate)* 旋转hom_mat2d_rotate (HomMat2DTranslate, rad(90), 244, 257, HomMat2DRotate)* 仿射变换轮廓affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DRotate)
vector_to_similarity(::Px,Py,Qx,Qy:HomMat2D)功能:根据两个以上点对计算相似仿射变换矩阵,支持旋转、平移和缩放Px:(输入参数)原始点组的x坐标Py:(输入参数)原始点组的y坐标Qx:(输入参数)变换的目的点组的x坐标Qy:(输入参数)变换的目的点组的y坐标HomMat2D:(输出参数)输出仿射变换矩阵
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)* 创建一个矩阵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 根据三个以上特征点获取仿射变换矩阵

vector_to_hom_mat2d(::Px,Py,Qx,Qy:HomMat2D)    功能:根据三个以上点对计算仿射变换矩阵,支持旋转、平移、缩放、斜切    Px:(输入参数)原始点组的x坐标    Py:(输入参数)原始点组的y坐标    Qx:(输入参数)变换的目的点组的x坐标    Qy:(输入参数)变换的目的点组的y坐标    HomMat2D:(输出参数)输出仿射变换矩阵
* 图像坐标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 计算仿射变换参数

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)

六、对图像、region和XLD进行仿射变换

1.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)

1.2 对image进行仿射变换

affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : ) 功能:对图像轮廓进行二维仿射变换 (支持缩放、旋转、平移,斜切)Image          (输入参数):输入图像ImageAffinTrans (输出参数):变换后的图像HomMat2D      (输入参数):仿射变换矩阵Interpolation    (输入参数):插值算法。参数值列表 nearest_neighbor,bilinear,constant,weightedAdaptImageSize  (输入参数):结果图像尺寸是否自适应。默认值: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 : ) 功能:对区域进行任意二维仿射变换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")

1.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)

标签:

Copyright ©  2015-2023 华夏饰品网版权所有  备案号:琼ICP备2022009675号-37   联系邮箱:435 227 67@qq.com