zoukankan      html  css  js  c++  java
  • 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')

    效果分别如下:

    有时候,并不需要创建初始化矩阵也可以执行仿射变换,例如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°等角度,那么图像其实只做了旋转变换,而没有进行平移变换。

  • 相关阅读:
    Android 程序架构: MVC、MVP、MVVM、Unidirectional、Clean...
    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    反射中使用 BindingFlags.IgnoreCase
    JQuery判断数组中是否包含某个元素$.inArray("js", arr);
    Sql日期时间格式转换
    c#组元(Tuple)的使用
    逆向最大匹配分词算法C#
    [WEB API] CLIENT 指定请求及回应格式(XML/JSON)
    Jquery 将表单序列化为Json对象
    JS调试加断点
  • 原文地址:https://www.cnblogs.com/xh6300/p/7442164.html
Copyright © 2011-2022 走看看