Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加速深度图像到点云的转换(Python)_Python_Performance_Computer Vision_Cython_Point Clouds - Fatal编程技术网

加速深度图像到点云的转换(Python)

加速深度图像到点云的转换(Python),python,performance,computer-vision,cython,point-clouds,Python,Performance,Computer Vision,Cython,Point Clouds,我目前正在尝试用python加速从深度到点云的转换。目前代码如下所示: color_points = [] for v in range(depth.shape[1]): for u in range(depth.shape[0]): z = depth[u,v] / self.scaling_factor if z == 0: # here np.where can be used prior to the loo

我目前正在尝试用python加速从深度到点云的转换。目前代码如下所示:

   color_points = []
   for v in range(depth.shape[1]):
        for u in range(depth.shape[0]):
            z = depth[u,v] / self.scaling_factor
            if z == 0:   # here np.where can be used prior to the loop
                continue
            x = (u - self.center_x) * z / self.focalLength     # u and v are needed, no clue how to do this
            y = (v - self.center_y) * z / self.focalLength

            color_points.append([x, y, z])
我的主要问题是,我不知道任何函数(比如np.where)可以加速图像阵列的循环,同时仍然具有像素的索引(例如u和v)。需要索引,因为计算取决于像素在传感器上的位置

Cython将是加速它的一个选项,但我仍然认为NumPy中一定有更好的功能可用

谢谢你的帮助


编辑:将Z计算添加到函数中。比例因子取决于获取深度图像的格式。在我的例子中,我得到[mm]中的深度并将其转换为米。

这里是我的尝试,使用
np.mgrid
在numpy数组中计算
u
v
,避免了缓慢的循环,并允许利用numpy的通用函数

import numpy as np
depth = np.random.uniform(size=(4,4)) # Just so this sample can be run
grid = np.mgrid[0:depth.shape[0],0:depth.shape[1]]
u, v = grid[0], grid[1]
z = depth / scaling_factor
adjusted_z = z / focal_length
x = (u - center_x) * adjusted_z
y = (v - center_y) * adjusted_z
color_points = np.stack([x,y,z], axis=-1).reshape(-1,3)

请注意,我没有为
z=0
过滤
color\u点
,但您可以使用
color\u点[z.reformate(-1)!=0]
进行过滤。还要注意的是,我的
color\u点数
的顺序与你的不同(
u
之前的
v
)。深度估计场景可能并不重要。

请提供计算z的代码。@francoisr刚刚根据要求添加了z计算。感谢您的快速回答!我甚至从来没有想过用u和v填充一个数组,然后用它进行进一步的计算。绝对是一件容易的工作!关于排序:对于点云,哪一个点先出现并不重要。