商城网站租服务器安全不,怎么做和美团一样的网站,免费创立网站,技工外包网二维仿射变换#xff0c;顾名思义就是在二维平面内#xff0c;对对象进行平移、旋转、缩放等变换的行为(当然还有其他的变换#xff0c;这里仅论述这三种最常见的)。Halcon中进行仿射变换的常见步骤如下#xff1a;① 通过hom_mat2d_identity算子创建一个初始化矩阵(即[1.0…二维仿射变换顾名思义就是在二维平面内对对象进行平移、旋转、缩放等变换的行为(当然还有其他的变换这里仅论述这三种最常见的)。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)910 *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)1617 *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)2223*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 : 1003 Column : 2004 dev_display (Image)567 for Index : 1 to 150 by 18 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 : 1003 Column1 : 10045 Row2 : 1006 Column2 : 2007 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)1112 *分两步依次执行缩放、平移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)1819 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)78 rotate_image (image, ImageRotate, -90, constant)9 area_center (ImageRotate, Area2, Row2, Column2)1011 hom_mat2d_identity (HomMat2DIdentity)12 hom_mat2d_rotate (HomMat2DIdentity, -rad(90), Row, Column, HomMat2DRotate)13 hom_mat2d_translate (HomMat2DRotate,Row2 - Row, Column2 -Column, HomMat2DTranslate)1415 affine_trans_region (SelectedReg, RegionAffineTrans, HomMat2DTranslate, constant)该算法顺利达到了目的——图像翻转以后原先生成的Region也翻转到了对应的位置。注意用rotate_image 算子旋转图像时如果旋转角度不是0°、90°、180°、270°等角度那么图像其实只做了旋转变换而没有进行平移变换。