HALCON geometric transformation

In layman's language HALCON geometric transformation

In machine vision systems, the lens is one of the imaging member, and based on the principle of the imaging lens apertures tend to produce industrial perspective distortion phenomenon, is a prerequisite for how to correct the distortion of the image analysis, which will be used projective transformation is one kind of geometric transformation. In addition, the image processing used to the translation, rotation, scaling, also belong to the geometric transformation. This article describes the geometric transformation HALCON in detail by way of example.

 

Basics

 

Homogeneous coordinates (Homogenous Coordinate)

 

Homogeneous coordinates is to be an original n-dimensional vector a with n + 1-dimensional vector represented. For example, two-dimensional point p (x, y) àp (x, y, w) became homogeneous coordinates, three-dimensional point empathy p (x, y, z) àp (x, y, z, w) has become homogeneous coordinates. Clearly, the homogeneous coordinate expression is not the only, with different values ​​and different w. In computer graphics, w is a common scale factor, it could be any value, but is generally in the geometric transformation, always take w = 1. Homogeneous coordinate representation is an important means of computer graphics, a clear distinction can be used both vectors and points, but also easier to perform geometric transformation.

 

Point p (x, y) as an example, if it wants translated to p (x + a, y + b), it is impossible to calculate the matrix completed, now replaced by homogeneous coordinates (x, y, 1) the coordinates (x + a, y + b) after, by matrix multiplication (left formula), it is convenient to obtain a translation. The transformation matrix is ​​usually written on the left, in order to maintain a consistent transformation matrix into the lower right corner of the form, which is homogeneous transformation matrix.

 

http://img.xiumi.us/xmi/ua/T5lQ/i/ea772a8791f2edf46620c793afeba87f-sz_65251.jpg?x-oss-process=style/xmorient

 

Homogeneous transformation matrix

 

In the same transformation matrix represents either orientation (rotation, scaling, etc.) and indicates the position (translation), this form of matrix is ​​called homogeneous transformation matrix. as follows:

http://img.xiumi.us/xmi/ua/T5lQ/i/d437ab9c99824dfd6c1aec02335f05e5-sz_63697.jpg?x-oss-process=style/xmorient

Use homogeneous coordinates, making it easier to calculate the geometric transformation, especially for affine transformation (2D / 3D) is more convenient, because the graphics hardware, vision algorithms have generally supported the homogeneous coordinates and matrix multiplication, and therefore more to promote the homogeneous coordinates use, making it a graphics standard, the below-mentioned geometric transformation (affine transformation) are homogeneous coordinates and to homogeneous transformation matrix based.

 

Geometric transformation

 

Geometric transformations include:

Similarity transformation (Similarity Transformation)

Affine transformation (Affine Transformation)

Projective transformation (Projective Transformation)

 

NOTE: linear transformation does not contain a translation, it has not been included geometric transformation, commonly used in short HALCON geometric transformation is affine and projective transformation, a similarity transformation is a simple and affine transformation! Similarity transformation, affine transformation, projection transformation may occur either in the two-dimensional space may also occur in three dimensional space.

 

Similarity transformation

 

Similar transformation is defined: a is converted by the 2D / 3D graphics to another 2D / 3D graphics, the shape remains the same (variable size orientation and position) in the process of change, any similarity transformation can be decomposed as scaling, translation , a combination of rotation, e.g., for zooming, a homogeneous transformation matrix can be represented (2D and 3D), wherein a ≠ 0 as follows.

 

http://img.xiumi.us/xmi/ua/T5lQ/i/a2f3d71d102e594fa1d0620fdcfa0296-sz_61319.jpg?x-oss-process=style/xmorient

 

Affine transformation

 

Affine transformation defined by: a 2D / 3D graphics transformation to another 2D / 3D graphics, lines and parallel lines maintaining unchanged (parallel lines parallel lines mapped) in the process of change, any of the affine transformation can be decomposed as scaling, combination of translation, rotation and shear (shearing,), for the affine transformation, homogeneous transformation matrix is ​​expressed as follows (2D and 3D):

http://img.xiumi.us/xmi/ua/T5lQ/i/e2b011e05d3d684dcd732322e3de24ff-sz_65230.jpg?x-oss-process=style/xmorient

 

For the affine transformation, there are two special transformation: non-scaling and shear (below), in addition to these two special transformation, similarity transformation can be seen as a special form of the affine transformation, Note: linear transformation including rotation, scaling, shearing, but does not contain a translation, and therefore an affine transformation is also defined as a linear transformation plus a translation transform.

http://img.xiumi.us/xmi/ua/T5lQ/i/c804551e007d858b9303e3f46169c26e-sz_43987.jpg?x-oss-process=style/xmorient

Projection Transformation

 

Projection transformation is defined: transformation process, the linear mapping a linear (but not necessarily guarantee parallelism), any two-dimensional projective transformation matrix can be represented by a reversible 3x3, and any three-dimensional projection transformation matrix can be represented by the reversible 4x4. as follows:

http://img.xiumi.us/xmi/ua/T5lQ/i/0ca85cd2393078a9e7f2c9c0d85dce33-sz_72936.jpg?x-oss-process=style/xmorient

By definition, the affine transformation can be seen as a special form of projection transformation. Furthermore, the last line of the projection transformation matrix becomes [0,0,1] or [0,0,0,1], changes to affine transformation matrix, can also prove a special form of the affine transformation is projective transformation . Thus, for a pan, zoom, shear, etc., affine transformation and a projective transformation can be achieved.

 

The following examples, the left becomes the right rotating operation, and may be an affine projection transformation in two ways.

http://img.xiumi.us/xmi/ua/T5lQ/i/32cd78b5600b1503068ded7663b95b6c-sz_172066.jpg?x-oss-process=style/xmorient

  • Affine transform method

 

hom_mat2d_identity (HomMat2DIdentity)

hom_mat2d_rotate (HomMat2DIdentity, rad (-90), 256, 256, HomMat2DRotate)

affine_trans_image (Image, ImageAffinTrans, HomMat2DRotate, 'constant', 'false')

 

  • 投影变换实现方法之一

(直接用给定点生成投影变换矩阵)

 

hom_vector_to_proj_hom_mat2d([0,0,512,512],[0,512,512,0],[1,1,1,1], [512,512,0,0], [1,1,1,1], ‘dlt', HomMat2D)

projective_trans_image (Image, TransImage, HomMat2D, 'bilinear', 'false', 'false')

 

  • 投影变换实现方法之二

(用三维仿射变换矩阵生成投影变换矩阵)

 

注:对于旋转来说,也可以看做是相机沿Z轴旋转90度后的成像效果,因此也可以用三维仿射变换,先转换相机坐标系,再生成投影变换矩阵!

 

hom_mat3d_identity (HomMat3DIdentity)

hom_mat3d_rotate (HomMat3DIdentity, rad(90), 'z', 256, 256, 512, HomMat3DRotate)

hom_mat3d_project (HomMat3DRotate, 256, 256, 512, HomMat2D)

projective_trans_image (Image, TransImage, HomMat2D, 'bilinear', 'false', 'false')

 

旋转示例中的仿射变换矩阵(也是齐次变换矩阵)

http://img.xiumi.us/xmi/ua/T5lQ/i/6ac5f7d3f12ee3806ea6f93b95b909d6-sz_65572.jpg?x-oss-process=style/xmorient

旋转示例中的投影变换矩阵(3X3),也是可逆矩阵

http://img.xiumi.us/xmi/ua/T5lQ/i/fdc4032ac1ea706724b46eb1ebd2bba6-sz_76502.jpg?x-oss-process=style/xmorient

同理,缩放、切变等,仿射变换和投影变换也都可以实现,但如下变换,只能用二维投影变换矩阵实现,因为变换过程中没有保证线的平行性。

http://img.xiumi.us/xmi/ua/T5lQ/i/a3d23ec81a0fc85f98cdcda0d92ca234-sz_168731.jpg?x-oss-process=style/xmorient

S

总结

 

HALCON中所用的变换知识只涉及到了二维、三维仿射变换和二维投影变换,二维仿射变换多用于平面图像的平移、旋转、缩放等;三维仿射变换多用于三维坐标系的平移、旋转等(或者用3D位姿表示),而二维投影变换多用于校正透视畸变,也就是三维空间的景象如何投影到相机靶面上的效果,如果相机和被测平面不垂直就会有畸变。

 

在成像过程中,普通工业镜头(小孔成像原理)都会带来透视畸变,也就是常见的近大远小现象,除非相机和被测平面保持绝对垂直,否则透视畸变是不可避免的。因此,通过三维空间的仿射变换(变换坐标系,使相机不垂直于被摄平面),可以产生透视畸变效果,也就是相当于进行了投影变换,HALCON也提供了算子hom_mat3d_project,能直接把三维仿射变换矩阵转换成二维投影变换矩阵(4x4的矩阵转换成3x3矩阵)。下面我们通过一个例子来说明一下。

http://img.xiumi.us/xmi/ua/T5lQ/i/e908fb114226888ce9dc9c760dc3b4e4-sz_136465.jpg?x-oss-process=style/xmorient

首先生成单位矩阵,进行一系列的旋转变换,产生三维仿射变换矩阵,也就是使得相机和被摄平面不垂于!

 

hom_mat3d_identity (HomMat3D)

hom_mat3d_rotate (HomMat3D, rad(20.0), 'z', PrincipalRow, PrincipalColumn, Focus, HomMat3D)

hom_mat3d_rotate (HomMat3D, rad(35.0), 'y', PrincipalRow, PrincipalColumn, Focus, HomMat3D)

hom_mat3d_rotate (HomMat3D, rad(25.0), 'x', PrincipalRow, PrincipalColumn, Focus, HomMat3D)

 

把三维仿射变换矩阵再转化成二维投影变换矩阵

 

hom_mat3d_project (HomMat3D, PrincipalRow, PrincipalColumn, Focus, ProjectionMatrix)

 

进行投影变换,也就是我们看到的右侧的效果

 

projective_trans_image (Image, TransImage, ProjectionMatrix, 'bilinear', 'false', 'false')

 

另外一种方法是,直接使用给定点生成投影变换矩阵,可以得到同样的效果

 

hom_vector_to_proj_hom_mat2d ([0,0,512,512], [0,512,512,0], [1,1,1,1], [-120,146,555,296], [120,752,368,72], [1,1,1,1], 'normalized_dlt', HomMat2D)

projective_trans_image (Image, TransImage1, HomMat2D, 'bilinear', 'false', 'false')

 

上面的这个例子是很好的说明了透视畸变现象如何产生的,左侧图是相机垂直于被摄平面,没有透视变形现象。然后进行了一系列的三维仿射变换,产生的效果就是相机不再垂直于被摄面,所以右图产生了透视畸变现象,那么从左图到右图恰恰是进行了一次二维投影变换。因为投影变换矩阵是可逆的,所以也可以把右图校正为左图,这恰恰是HALCON中投影变换的用处所在!

 

以下例子也是HALCON中典型的畸变校正示例,使用的正是投影变换。

 

http://img.xiumi.us/xmi/ua/T5lQ/i/d33a32af69401138980de23e0d270eba-sz_178725.jpg?x-oss-process=style/xmorient

 

附录

 

解释一下HALCON中的:

hom_mat2d_*和hom_mat3d_*算子

 

hom_mat2d_identity产生2x3矩阵,用于描述二维变换,实际上是3x3齐次变换矩阵(仿射变换);

hom_mat3d_identity produce 3x4 matrix is used to describe three-dimensional transformation, in fact, 4x4 homogeneous transformation matrix (affine transformation);

hom_mat2d_ * supports both 2x3 matrix (affine transformation) also supports 3x3 matrix (projection transformation);

hom_mat3d_ * Support 3x4 matrix (affine transformation), which is the 4x4 homogeneous transformation matrix

 

On how to generate in the HALCON:

Affine transformation matrix and the projection matrix

 

Affine transformation matrix

After generating hom_mat2d_identity hom_mat3d_identity or matrix, and then through translation, rotation, zooming and obtain affine transformation matrix.

 

Projection matrix

hom_mat3d_project can be directly converted into a three-dimensional projection transformation matrix affine transformation matrix, if the system is to know how to transform coordinate space, this method can be used;

hom_vector_to_proj_hom_mat2d generating projective transformation matrix for a given point, three-dimensional coordinates of the input point homogeneous coordinates, transformation can be supported and limited infinity point distant point;

vector_to_proj_hom_mat2d with a projection transformation matrix to generate a given point, the input point of the two-dimensional non-homogeneous coordinates, transformation support point far limited;

 

NOTE: For transformation involves a limited far point, the two or more operators can be!

 

Released eight original articles · won praise 0 · Views 184

Guess you like

Origin blog.csdn.net/Luckboy_shy/article/details/104716707