如何加快合成光线投射功能?

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

我目前正在python中进行体积渲染项目,在此情况下,给定由体素组成的3D体积,我使用合成射线投射功能来生成图像。该函数(我在下面显示)可以正常运行,但是运行时间很长。你们有如何使此功能更快的提示吗?代码是Python 3.6.8,并使用各种numpy数组。

    def render_compositing(self, view_matrix: np.ndarray, volume: Volume, image_size: int, image: np.ndarray):
        # Clear the image
        self.clear_image()

        # U, V, View vectors. See documentation in parent's class
        u_vector = view_matrix[0:3]
        v_vector = view_matrix[4:7]
        view_vector = view_matrix[8:11]

        # Center of the image. Image is squared
        image_center = image_size / 2

        # Center of the volume (3-dimensional)
        volume_center = [volume.dim_x / 2, volume.dim_y / 2, volume.dim_z / 2]

        # Define a step size to make the loop faster
        step = 2 if self.interactive_mode else 1

        for i in range(0, image_size, step):
            for j in range(0, image_size, step):
                sum_color = TFColor(0, 0, 0, 0)
                for k in range(0, image_size, step):
                    # Get the voxel coordinate X
                    voxel_coordinate_x = u_vector[0] * (i - image_center) + v_vector[0] * (j - image_center) + \
                                         view_vector[0] * (k - image_center) + volume_center[0]

                    # Get the voxel coordinate Y
                    voxel_coordinate_y = u_vector[1] * (i - image_center) + v_vector[1] * (j - image_center) + \
                                         view_vector[1] * (k - image_center) + volume_center[1]

                    # Get the voxel coordinate Y
                    voxel_coordinate_z = u_vector[2] * (i - image_center) + v_vector[2] * (j - image_center) + \
                                         view_vector[2] * (k - image_center) + volume_center[2]

                    color = self.tfunc.get_color(
                        get_voxel(volume, voxel_coordinate_x, voxel_coordinate_y, voxel_coordinate_z))

                    sum_color.r = color.a * color.r + (1 - color.a) * sum_color.r
                    sum_color.g = color.a * color.g + (1 - color.a) * sum_color.g
                    sum_color.b = color.a * color.b + (1 - color.a) * sum_color.b
                    sum_color.a = color.a + (1 - color.a) * sum_color.a

                red = sum_color.r
                green = sum_color.g
                blue = sum_color.b
                alpha = sum_color.a

                # Compute the color value (0...255)
                red = math.floor(red * 255) if red < 255 else 255
                green = math.floor(green * 255) if green < 255 else 255
                blue = math.floor(blue * 255) if blue < 255 else 255
                alpha = math.floor(alpha * 255) if alpha < 255 else 255

                # Assign color to the pixel i, j
                image[(j * image_size + i) * 4] = red
                image[(j * image_size + i) * 4 + 1] = green
                image[(j * image_size + i) * 4 + 2] = blue
                image[(j * image_size + i) * 4 + 3] = alpha
python performance runtime raycasting volume-rendering
1个回答
0
投票

我不明白为什么要为此代码使用python。如果您担心速度,使用着色器不是更好的方法吗?

无论如何,在当前代码中,这里有几件事可以完成。

  1. 体素坐标可以使用numpy计算。您可以制作3通道2d图像,并在单次拍摄中计算整个切片(k)的x,y,z坐标。
  2. 通过将alpha值阈值设置为0.999或0.99使用早期射线终止。这看起来不多,但可以带来很大的速度提升。
© www.soinside.com 2019 - 2024. All rights reserved.