Affine transformation
lastmod 2024-07-03created 2015-12-303 edits
In affine geometry, an affine transformation is a transformation between two affine spaces that preserves collinear points.
Estimating and applying an affine transformation on a 3D point cloud
The following example estimates an affine transformation of a 3D point cloud and transforms the original point cloud using the estimated transformation matrix.
import numpy as np
import cv2
# 대응점 쌍 생성
src_pts = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 1]], dtype=np.float32)
dst_pts = np.array([[0, 0, 0], [2, 0, 0], [0, 2, 0], [2, 2, 2]], dtype=np.float32)
# 3D Affine 변환 추정
retval, out, inliers = cv2.estimateAffine3D(src_pts, dst_pts)
# 변환 행렬 출력
print("Estimated Affine Transformation:\n", out)
# 원본 점군 변환
transformed_pts = cv2.transform(src_pts.reshape(-1, 1, 3), out)
# 결과 출력
print("Original Points:\n", src_pts)
print("Transformed Points:\n", transformed_pts.reshape(-1, 3))
In the example above, the src_pts and dst_pts variables define the original and transformed 3D point clouds, respectively.
The cv2.estimateAffine3D() function estimates the 3D affine transformation matrix and stores it in the out variable.
After printing the estimated transformation matrix, the cv2.transform() function transforms the original point cloud.
- Pros
- A 3D affine transformation is useful for representing linear transformations in 3D space, including translation, rotation, and scaling.
- The estimateAffine3D() function provided by OpenCV makes it easy to estimate a 3D affine transformation.
- Cons
- A 3D affine transformation is one kind of generalized 3D transformation, so it may be hard to accurately represent some complex transformations.
See also
- Affine transformation
- Perspective transform
- OpenCV - Geometric Transformations of Images