我正在numpy中编写一个简单的脚本,它采用640 x 480
深度图像(2D numpy字节数组),并在给定针孔相机模型的情况下将其转换为num_points x 3
numpy点阵列。数学很简单,我已经让脚本工作 - 但它非常慢。显然,每帧需要2秒。我之前用C ++编写过类似的脚本,每帧得到~100ms。我想知道我可以对我的python脚本进行哪些优化。任何一个都可以被矢量化吗?我可以从并行化中受益吗?
def create_point_cloud(self, depth_image):
shape = depth_image.shape;
rows = shape[0];
cols = shape[1];
points = np.zeros((rows * cols, 3), np.float32);
bytes_to_units = (1.0 / 256.0);
# Linear iterator for convenience
i = 0
# For each pixel in the image...
for r in xrange(0, rows):
for c in xrange(0, cols):
# Get the depth in bytes
depth = depth_image[r, c, 0];
# If the depth is 0x0 or 0xFF, its invalid.
# By convention it should be replaced by a NaN depth.
if(depth > 0 and depth < 255):
# The true depth of the pixel in units
z = depth * bytes_to_units;
# Get the x, y, z coordinates in units of the pixel
# using the intrinsics of the camera (cx, cy, fx, fy)
points[i, 0] = (c - self.cx) / self.fx * z;
points[i, 1] = (r - self.cy) / self.fy * z;
points[i, 2] = z
else:
# Invalid points have a NaN depth
points[i, 2] = np.nan;
i = i + 1
return points
答案 0 :(得分:1)
我无法检查它,因为我没有您的数据,但以下代码应该完成这项工作
def create_point_cloud_vectorized(self,depth_image):
im_shape = depth_image.shape
# get the depth
d = depth_image[:,:,0]
# replace the invalid data with np.nan
depth = np.where( (d > 0) & (d < 255), d /256., np.nan)
# get x and y data in a vectorized way
row = (np.arange(im_shape[0])[:,None] - self.cx) / self.fx * depth
col = (np.arange(im_shape[1])[None,:] - self.cy) / self.fy * depth
# combine x,y,depth and bring it into the correct shape
return array((row,col,depth)).reshape(3,-1).swapaxes(0,1)