def convert_depth_map_to_cam_coords(self, depth_map_display_sparsity=100):
        """Convert the depth map to points in camera coordinates."""
        assert depth_map_display_sparsity > 0

        depth_map = self.get_depth_map()

        height, width = depth_map.shape
        if self.height == height and self.width == width:
            x_step_size = 1.0
            y_step_size = 1.0
        else:
            x_step_size = self.width / width
            y_step_size = self.height / height

        fx, fy, skew, cx, cy = self._split_intrinsic_mat(
            self.get_calibration_mat()
        )

        # Use the local coordinate system of the camera to analyze its viewing
        # directions.The Blender camera coordinate system looks along the
        # negative z axis (blue), the up axis points along the y axis (green).

        indices = np.indices((height, width))
        y_index_list = indices[0].flatten()
        x_index_list = indices[1].flatten()

        depth_values = depth_map.flatten()

        assert len(x_index_list) == len(y_index_list) == len(depth_values)

        if self._shift_depth_map_to_pixel_center:
            # https://github.com/simonfuhrmann/mve/blob/master/libs/mve/depthmap.cc
            #  math::Vec3f v = invproj * math::Vec3f(
            #       (float)x + 0.5f, (float)y + 0.5f, 1.0f);
            u_index_coord_list = x_step_size * x_index_list + 0.5
            v_index_coord_list = y_step_size * y_index_list + 0.5
        else:
            # https://github.com/colmap/colmap/blob/dev/src/base/reconstruction.cc
            # COLMAP assumes that the upper left pixel center is (0.5, 0.5)
            # i.e. the pixels are already shifted
            u_index_coord_list = x_step_size * x_index_list
            v_index_coord_list = y_step_size * y_index_list

        # The cannoncial vectors are defined according to p.155 of
        # "Multiple View Geometry" by Hartley and Zisserman using a canonical
        # focal length of 1 , i.e. vec = [(x - cx) / fx, (y - cy) / fy, 1]
        skew_correction = (cy - v_index_coord_list) * skew / (fx * fy)
        x_coords_canonical = (u_index_coord_list - cx) / fx + skew_correction
        y_coords_canonical = (v_index_coord_list - cy) / fy
        z_coords_canonical = np.ones(len(depth_values), dtype=float)

        # Determine non-background data
        depth_values_not_nan = np.nan_to_num(depth_values)
        non_background_flags = depth_values_not_nan > 0
        x_coords_canonical_filtered = x_coords_canonical[non_background_flags]
        y_coords_canonical_filtered = y_coords_canonical[non_background_flags]
        z_coords_canonical_filtered = z_coords_canonical[non_background_flags]
        depth_values_filtered = depth_values[non_background_flags]

        if depth_map_display_sparsity > 1:
            x_coords_canonical_filtered = x_coords_canonical_filtered[
                ::depth_map_display_sparsity
            ]
            y_coords_canonical_filtered = y_coords_canonical_filtered[
                ::depth_map_display_sparsity
            ]
            z_coords_canonical_filtered = z_coords_canonical_filtered[
                ::depth_map_display_sparsity
            ]
            depth_values_filtered = depth_values_filtered[
                ::depth_map_display_sparsity
            ]

        if self._depth_map_semantic == Camera.DEPTH_MAP_WRT_CANONICAL_VECTORS:
            # In this case, the depth values are defined w.r.t. the canonical
            # vectors. This kind of depth data is used by Colmap.
            x_coords_filtered = (
                x_coords_canonical_filtered * depth_values_filtered
            )
            y_coords_filtered = (
                y_coords_canonical_filtered * depth_values_filtered
            )
            z_coords_filtered = (
                z_coords_canonical_filtered * depth_values_filtered
            )

        elif self._depth_map_semantic == Camera.DEPTH_MAP_WRT_UNIT_VECTORS:
            # In this case the depth values are defined w.r.t. the normalized
            # canonical vectors. This kind of depth data is used by MVE.
            cannonical_norms_filtered = np.linalg.norm(
                np.array(
                    [
                        x_coords_canonical_filtered,
                        y_coords_canonical_filtered,
                        z_coords_canonical_filtered,
                    ],
                    dtype=float,
                ),
                axis=0,
            )
            # Instead of normalizing the x,y and z component, we divide the
            # depth values by the corresponding norm.
            normalized_depth_values_filtered = (
                depth_values_filtered / cannonical_norms_filtered
            )
            x_coords_filtered = (
                x_coords_canonical_filtered * normalized_depth_values_filtered
            )
            y_coords_filtered = (
                y_coords_canonical_filtered * normalized_depth_values_filtered
            )
            z_coords_filtered = (
                z_coords_canonical_filtered * normalized_depth_values_filtered
            )

        else:
            assert False

        cam_coords = np.dstack(
            (x_coords_filtered, y_coords_filtered, z_coords_filtered)
        )[0]

        return cam_coords
Convert the depth map to points in camera coordinates.
photogrammetry_importer/types/camera.py