Halcon图像仿射变换

图 像 仿 射 变 换 图像仿射变换 仿


二维仿射变换,顾名思义就是在二维平面内,对对象进行平移(Translation)、缩放(Scale)、翻转(Flip)、旋转(Rotation)和剪切(Shear)。


图像的变换包括仿射变换与透视变换,一般多数情况下只需要仿射变换

图像的仿射变换是指图像的平移与旋转

映射矩阵:在平移与旋转中,是由一个含有6个参数的矩阵组成,用来描述图像的旋转平移关系。所谓平移关系,是指移动前图像的位置与移动后图像的位置之间的关系,只有有了着关系,才能进行图像的移动。

比如:点(1,2)与点(2,3)之间的关系为[1,0,1,0,1,1]。

vector_angle_to_rigid()计算移动前后的矩阵关系
旋转图像:根据矩阵进行图像的旋转。
affine_trans_image() 旋转图像

1.图像选择

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_open_file_dialog ('read_image', 'default', 'default', Selection)

2.读取图像

read_image (Image, Selection)

3.绘制矩形

draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)

4.生成矩形

gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)

5.裁剪出区域

reduce_domain (Image, Rectangle, ImageReduced)
area_center (Rectangle, Area, Row, Column)

tuple_rad (45, Rad)
vector_angle_to_rigid (Row, Column, Area, Row, Column, Rad, HomMat2D)

affine_trans_image (ImageReduced, ImageAffineTrans, HomMat2D, 'constant', 'false')
affine_trans_region (Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')

* 另一种方式,旋转图片
hom_mat2d_identity (H1)
area_center (Image, Area1, Row3, Column3)
hom_mat2d_rotate (H1, Rad, Column3, Row3, HomMat2DRotate)
affine_trans_image (ImageReduced, ImageAffineTrans1, HomMat2DRotate, 'constant', 'false')

Halcon中进行仿射变换的常见步骤如下:


① 通过hom_mat2d_identity算子创建一个初始化矩阵(即[1.0, 0.0, 0.0, 0.0, 1.0, 0.0]);

hom_mat2d_identity (HomMat2DIdentity)

② 在初始化矩阵的基础上,使用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中的两个参数的意思是:Tx和Ty分别代表Row方向和Column方向的平移量

hom_mat2d_translate (HomMat2DIdentity,30, 150, HomMat2DTranslate)

*hom_mat2d_rotate中的三个参数的意思是:旋转角度(逆时针为正,弧度制),旋转中心的row和column值

hom_mat2d_rotate (HomMat2DIdentity, rad(20), Row, Column, HomMat2DRotate)

*hom_mat2d_scale中的四个参数的意思是:Sx和Sy分别代表Row方向和Column方向的缩放系数,缩放中心的row和column值

hom_mat2d_scale (HomMat2DIdentity, 2.0, 1.05, Row, Column, HomMat2DScale)

read_image (Image, 'test.jpg')
threshold (Image, Region, 0, 200)
opening_circle (Region, Region, 1.5)
connection (Region, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
*得到变换的中心点
area_center (SelectedRegions, Area, Row, Column)
dev_set_draw ('margin')

*hom_mat2d_translate中的两个参数的意思是:Tx和Ty分别代表Row方向和Column方向的平移量
dev_display (Image)
disp_cross (3600, Row, Column, 10, 40)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity,30, 150, HomMat2DTranslate)
affine_trans_region (Region, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')

*hom_mat2d_rotate中的三个参数的意思是:旋转角度(逆时针为正,弧度制),旋转中心的row和column值
dev_display (Image)
disp_cross (3600, Row, Column, 10, 40)
hom_mat2d_rotate (HomMat2DIdentity, rad(20), Row, Column, HomMat2DRotate)
affine_trans_region (Region, RegionAffineTrans, HomMat2DRotate, 'nearest_neighbor')

*hom_mat2d_scale中的四个参数的意思是:Sx和Sy分别代表Row方向和Column方向的缩放系数,缩放中心的row和column值
dev_display (Image)
disp_cross (3600, Row, Column, 10, 40)
hom_mat2d_scale (HomMat2DIdentity, 2.0, 1.05, Row, Column, HomMat2DScale)
affine_trans_region (Region, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')

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

vector_to_rigid (Column, Row, Column, Row, HomMat2D)
vector_to_similarity (Column, Row, Column, Row, HomMat2D)

实例

* This program gives an example of how to use vector_to_similarity.
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 500, 500, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_part (0, 0, 499, 499)
* To generate a set of points from which we can construct a transformation,
* we will use a synthetic ellipse and read out its coordinates.
gen_ellipse_contour_xld (ContEllipse, 200, 200, rad(20), 150, 100, rad(0), rad(260), 'positive', 1)
get_contour_xld (ContEllipse, Rows1, Cols1)
* Now we will transform the ellipse by a known transformation and add some
* noise to the coordinates.
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, rad(80), 0, 0, HomMat2DRotate)
hom_mat2d_translate (HomMat2DRotate, 600, 400, HomMat2DTranslate)
hom_mat2d_scale (HomMat2DTranslate, 0.5, 0.5, 0, 0, HomMat2DScale)
affine_trans_point_2d (HomMat2DScale, Rows1, Cols1, Rows2, Cols2)
gen_contour_polygon_xld (Contour, Rows2, Cols2)
add_noise_white_contour_xld (Contour, NoisyContours, 5, 5)
* We will read out the coordinates of the noisy ellipse and use it to construct
* the transformation.  Note that by his approach we know exactly which points
* correspond to each other.  In a real application, the step of determining
* the correspondences is usually the hard part.
get_contour_xld (NoisyContours, Rows2, Cols2)
* Now we can determine the similarity transformation that best approximates the
* the point correspondences.
vector_to_similarity (Rows1, Cols1, Rows2, Cols2, HomMat2D)
* Finally, we transform the original contour by the calculated transformation
* to get an impression of how accurate the calculated transformation is
* (but before that, correct the coordinate system).
hom_mat2d_translate (HomMat2D, 0.5, 0.5, HomMat2DTmp)
hom_mat2d_translate_local (HomMat2DTmp, -0.5, -0.5, HomMat2DAdapted)
affine_trans_contour_xld (ContEllipse, ContoursAffineTrans, HomMat2DAdapted)
* Now let's display the results:
dev_clear_window ()
Message := 'Calculate similarity transformation'
Message[1] := 'of an ellipse:'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
dev_set_color ('white')
disp_message (WindowHandle, 'Original ellipse', 'window', 80, 30, '', 'false')
dev_display (ContEllipse)
dev_set_color ('red')
disp_message (WindowHandle, 'Noisy contours', 'window', 300, 250, '', 'false')
dev_display (NoisyContours)
dev_set_color ('green')
disp_message (WindowHandle, 'Transformed ellipse', 'window', 320, 250, '', 'false')
dev_display (ContoursAffineTrans)
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值