[英]How can I associate rgb values to pixel locations in a perspective projection?
I am trying to associate rgb values to pixel coordinates after having done a perspective projection.在完成透视投影后,我试图将 rgb 值与像素坐标相关联。 The equation for the perspective projection is:
透视投影的方程为:
where x
, y
, are the pixel locations of the point, X
, Y
, and Z
are locations of points in the camera frame, and the other parameters denote the intrinsic camera parameters.其中
x
、 y
是点的像素位置, X
、 Y
和Z
是点在相机框架中的位置,其他参数表示相机的固有参数。 Given a point cloud containing the point locations and rgb values, I would like to associate rgb values to pixel locations according to the perspective projection.给定包含点位置和 rgb 值的点云,我想根据透视投影将 rgb 值与像素位置相关联。
The following code should create the correct image:以下代码应创建正确的图像:
import matplotlib.pyplot as plt
import open3d as o3d
import numpy as np
cx = 325.5;
cy = 253.5;
fx = 518.0;
fy = 519.0;
K = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
pcd = o3d.io.read_point_cloud('freiburg.pcd', remove_nan_points=True)
points = np.array(pcd.points)
colors = np.array(pcd.colors)
projection = (K @ points.T).T
normalization = projection / projection[:, [2]] #last elemet must be 1
pixel_coordinates = normalization.astype(int)
img = np.zeros((480, 640, 3))
#how can I fill the img appropriately? The matrix pixel coordinates should
# inform about where to place the color intensities.
for position, intensity in zip(pixel_coordinates, colors):
row, column = position[0], position[1]
#img[row, column, :] = intensity # returns with error
img[column, row, :] = intensity # gives a strange picture.
The point cloud can be read here .点云可以在这里阅读。 I expect to be able to associate the rgb values in the last loop:
我希望能够在最后一个循环中关联 rgb 值:
for position, intensity in zip(pixel_coordinates, colors):
row, column = position[0], position[1]
#img[row, column, :] = intensity # returns with error
img[column, row, :] = intensity # gives a strange picture.
Strangely, if the second-to-last line is not commented, the program returns and IndexError
while attempting to write a rgb values outside the range of available columns.奇怪的是,如果倒数第二行没有注释,程序会在尝试写入超出可用列范围的 rgb 值时返回
IndexError
。 The last line in the loop runs however without problems.循环中的最后一行运行但是没有问题。 The generated picture and the correct picture can be seen below:
生成的图片和正确的图片如下图:
How can I modify the code above to obtain the correct image?如何修改上面的代码以获得正确的图像?
A couple of issues:几个问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.