cv2.projectPoints 正交到平行投影

问题描述 投票:0回答:0

我想使用

cv2.projectPoints()
函数将 3D 三角形投影到 2D 平面/图像上。

但是,它不返回正交投影,我该如何修改它以强制投影与图像平面平行。在这里您可以在右侧看到它非常弯曲。我希望它是平的,没有变形。

import numpy as np
import cv2
import matplotlib.pyplot as plt

# Load the 3D object from the text file
with open("PATH_TO.txt", "r") as file:
    lines = file.readlines()

# Parse the object file and create a list of triangles
triangles = []
for line in lines:
    x, y, z = map(float, line.strip().split())
    triangles.append((x, y, z))

# Define the camera parameters
camera_matrix = np.array([[800, 0, 100], [0, 800, 100], [0, 0, 1]], dtype='float32')
dist_coeffs = np.array([0, 0, 0, 0, 0], dtype='float32')

# Define the zenith and azimuth angles (in radians)
zenith = np.radians(0)
azimuth = np.radians(0)

# Create a rotation matrix
R_x = np.array([[1, 0, 0], [0, np.cos(zenith), -np.sin(zenith)], [0, np.sin(zenith), np.cos(zenith)]], dtype='float32')
R_y = np.array([[np.cos(azimuth), 0, np.sin(azimuth)], [0, 1, 0], [-np.sin(azimuth), 0, np.cos(azimuth)]], dtype='float32')
R = np.dot(R_y, R_x)

# Convert rotation matrix to Rodrigues object
rvec, _ = cv2.Rodrigues(R)

# Define the camera position
t = np.array([0, 0, 10], dtype='float32')

# Create the Z-Buffer (with a default value of +inf)
width, height = 500,500

# Draw the triangles on the image
image = np.zeros((height, width, 3), dtype='uint8')

for i in range(0, len(triangles), 3):
    triangle = triangles[i:i+3]
    object_points = np.array([triangle], dtype='float32')
    image_points, _ = cv2.projectPoints(object_points, rvec, t, camera_matrix, None)
    image_points = np.int32(image_points.reshape(-1, 2))

    cv2.fillConvexPoly(image, image_points, (255, 255, 255))

plt.imshow(image)
plt.show()

python opencv projection
© www.soinside.com 2019 - 2024. All rights reserved.