본문 바로가기
AI & Data/AI&ML Examples

[AI & ML Examples] Computer Vision Calibration: Normalized Points 예제코드

by Henry Cho 2023. 10. 13.
728x90

Computer Vision Calibration: Normalized Points 예제코드

포스트 난이도: HOO_Senior


# Example Code

이번 포스트에서는 Computer vision에서 Normalized points에 대한 값을 구하기 위해서 Calibration이 어떻게 이루어지는지 살펴볼 수 있다. 아래의 예제코드를 보면 2 dimensional과 3 dimensional에 해당하는 포인트 값들이 homogeneous과정을 거치고 svd를 통해서 Normalized point에 대한 "M" 값이 구해지는 과정을 볼 수 있다. 여기서 꼭 svd function 말고도 lstsq () funciton을 사용해서도 Normalized 된 M을 산출해 낼 수 있다. 또한 산출된 residuals를 출력함으로써 Calibration이 어떻게 이루어졌는지를 Nomoralized point 단계에서 직접 살펴볼 수 있다.


# Calibration
#The matrix M recovered from the normalized points (3x4)

import numpy as np

# Load the normalized 2D and 3D points from files
pts2d_norm = np.loadtxt(" ")
pts3d_norm = np.loadtxt(" ")

# Add a column of ones to normalized 3D points to make them homogeneous
pts3d_norm_homogeneous = np.hstack((pts3d_norm, np.ones((pts3d_norm.shape[0], 1))))

# Create matrices A and B for the least squares problem
A = np.zeros((len(pts2d_norm) * 2, 12))
B = np.zeros((len(pts2d_norm) * 2, 1))

for i in range(len(pts2d_norm)):
    X, Y, Z, W = pts3d_norm_homogeneous[i]  # Use the homogeneous 3D points
    u, v = pts2d_norm[i]

    A[2 * i] = [X, Y, Z, 1, 0, 0, 0, 0, -u*X, -u*Y, -u*Z, -u]
    A[2 * i + 1] = [0, 0, 0, 0, X, Y, Z, 1, -v*X, -v*Y, -v*Z, -v]
    B[2 * i] = 0
    B[2 * i + 1] = 0

# Solve for MnormA using SVD
U, S, V = np.linalg.svd(A)
MnormA = V[-1].reshape(3, 4)

# Test MnormA by applying it to the 3D points and comparing to given 2D points
predicted_2d_points_homogeneous = np.dot(pts3d_norm_homogeneous, MnormA.T)
predicted_2d_points = predicted_2d_points_homogeneous[:, :2] / predicted_2d_points_homogeneous[:, 2][:, np.newaxis]

# Calculate residuals
residuals = np.sqrt(np.sum((predicted_2d_points - pts2d_norm) ** 2, axis=1))

# Print the residuals for each point
for i, residual in enumerate(residuals):
    print(f"Point {i + 1} Residual: {residual}")

# Print the MnormA matrix
print("\nCalculated MnormA matrix:")
print(MnormA)

Result of Example Code


# github link

https://github.com/WhoisHOO/HOOAI/blob/main/Data%20Science/calibration_normalized_point


 

728x90

댓글