简体   繁体   English

如何将 rgb 值关联到透视投影中的像素位置?

[英]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.其中xy是点的像素位置, XYZ是点在相机框架中的位置,其他参数表示相机的固有参数。 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:几个问题:

  • You are ignoring the nonlinear distortion in the projection.您忽略了投影中的非线性失真。 Are the images you are comparing to undistorted?您要比较的图像是否未失真? If they are, are you sure your projection matrix K is the one associated to the undistorted image?如果是,你确定你的投影矩阵 K 是与未失真图像相关联的吗?
  • Projecting the 3D points will inevitably produce a point cloud on the image plane, not a continuous image.投影这3D个点必然会在图像平面上产生点云,而不是连续图像。 To produce an image somewhat natural you likely need to interpolate nearby samples in the 2D point cloud.要生成有点自然的图像,您可能需要在 2D 点云中插入附近的样本。 Your choice of interpolation filter determines the quality of the result.您选择的插值滤波器决定了结果的质量。 For example, you could first make an image of rgb buckets, a similar image of weights, project the 3d points, place their rgb values in the closest bucket (the one obtained by rounding the projection x,y coords), with a weight equal to the reciprocal of the distance of the projection from the bucket's center (ie the reciprocal of the euclidean norm of the rounding residuals).例如,您可以先制作一个 rgb 桶的图像,一个类似的权重图像,投影 3d 个点,将它们的 rgb 值放在最近的桶中(通过四舍五入投影 x,y 坐标获得的那个桶),权重等于到桶中心的投影距离的倒数(即舍入残差的欧几里德范数的倒数)。 You then first compute the output pixel values as weighted averages at each bucket and then, if there are any unfilled bucket, you fill them by (say) bilinear interpolation of the filled neighbors.然后,您首先将 output 像素值计算为每个桶的加权平均值,然后,如果有任何未填充的桶,则通过(比方说)已填充邻居的双线性插值来填充它们。 The last step will fill 1-pixel holes surrounded by already filled values.最后一步将填充由已填充值包围的 1 像素孔。 For larger holes you will need to choose some kind of infill procedure.对于较大的孔,您需要选择某种填充程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从透视投影图像计算正交投影图像? - How can I compute orthographical projection image from perspective projection image? 我如何在此库中使用透视投影 - How do i use perspective projection in this library 如何读取活动窗口给定像素的 RGB 值? - How can I read the RGB value of a given pixel of an active window? 如何将像素的 rgb 值转换为 HSV? - how can i convert rgb value of pixel to HSV? 如何使用 matplotlib 获取像素 rgb 值? - How to get pixel rgb values using matplotlib? 如何使用python获取此图像的某些颜色(RGB)的像素坐标? - How can I get pixel coordinates of certain color(RGB) of this image using python? 将其更改为每个像素的3维数组后,如何创建彩色图像(RGB)? - How can I create a color image (RGB), after changing it into a 3 dimensional array of each pixel? 如何循环浏览图像中的单个像素并输出每行的最高RGB值像素? - How can I cycle through individual pixels in an image and output the highest RGB valued pixel of each row? 如何使用python更改位图图像上每个像素的RGB值? - How can I change each pixel's RGB value on bitmap image with python? 如何将单个像素位置作为 plt.imshow 的参数? - How can I give individual pixel locations as arguments for plt.imshow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM