Skip to content

pixano.core.depth_image

DepthImage(depth_map=None, bytes=None, shape=None)

Bases: PixanoType, BaseModel

Depth image type

Attributes:

Name Type Description
_depth_map ndarray

Depth image as NumPy array

_bytes bytes

Depth image as bytes

_shape list[int]

Depth image shape

Parameters:

Name Type Description Default
depth_map ndarray

Depth image as NumPy array. Defaults to None.

None
bytes bytes

Depth image as bytes. Defaults to None.

None
shape list[int]

Depth image shape. Defaults to None.

None
Source code in pixano/core/depth_image.py
def __init__(
    self,
    depth_map: np.ndarray = None,
    bytes: bytes = None,
    shape: list[int] = None,
):
    """Initialize Depth image

    Args:
        depth_map (np.ndarray, optional): Depth image as NumPy array. Defaults to None.
        bytes (bytes, optional): Depth image as bytes. Defaults to None.
        shape (list[int], optional): Depth image shape. Defaults to None.
    """

    # Define public attributes through Pydantic BaseModel
    super().__init__()

    # Define private attributes manually
    self._depth_map = depth_map
    self._bytes = bytes
    self._shape = shape

bytes: bytes property

Return Depth image as bytes

Returns:

Type Description
bytes

Depth image as bytes

depth_map: np.ndarray property

Returns Depth image as NumPy array

Returns:

Type Description
ndarray

Depth image as NumPy array

shape: list[int] property

Return Depth image shape

Returns:

Type Description
list[int]

Depth image shape

display()

Display Depth image with matplotlib

Returns:

Type Description
Figure

Plotted image

Source code in pixano/core/depth_image.py
def display(self):
    """Display Depth image with matplotlib

    Returns:
        plt.Figure: Plotted image
    """

    fig, ax = plt.subplots(figsize=self._shape)
    ax.imshow(self.depth_map.astype(np.int8), cmap="gray", vmin=0, vmax=255)
    ax.axis("off")

    plt.ion()
    plt.show()

    return fig

load(path) staticmethod

Create depth image from 16-bit .png file

Parameters:

Name Type Description Default
path str

Path of .png file of depth image

required

Returns:

Type Description
DepthImage

Depth image

Source code in pixano/core/depth_image.py
@staticmethod
def load(path: str) -> "DepthImage":
    """Create depth image from 16-bit .png file

    Args:
        path (str): Path of .png file of depth image

    Returns:
        DepthImage: Depth image
    """

    depth_map = imageio.v3.imread(path).astype(np.uint16)
    return DepthImage(depth_map=depth_map, shape=depth_map.shape)

load_npy(path) staticmethod

Create depth image from .npy file

Parameters:

Name Type Description Default
path str

Path to .npy file containing depth image as NumPy Array.

required

Returns:

Type Description
DepthImage

Depth image

Source code in pixano/core/depth_image.py
@staticmethod
def load_npy(path: str) -> "DepthImage":
    """Create depth image from .npy file

    Args:
        path (str): Path to .npy file containing depth image as NumPy Array.

    Returns:
        DepthImage: Depth image
    """

    depth_map = np.load(path)
    return DepthImage(depth_map=depth_map, shape=depth_map.shape)

open()

Open depth image

Returns:

Type Description
IO

Opened depth image

Source code in pixano/core/depth_image.py
def open(self) -> IO:
    """Open depth image

    Returns:
        IO: Opened depth image
    """

    return io.BytesIO(self.bytes)

save(path)

Save depth image to .png file.

Parameters:

Name Type Description Default
path str

Path to .png file to save

required
Source code in pixano/core/depth_image.py
def save(self, path):
    """Save depth image to .png file.

    Args:
        path (str): Path to .png file to save
    """

    depth_image = self.depth_map.astype(np.uint16)
    imageio.v3.imwrite(path, depth_image)

to_grayscale()

Transform Depth image to 8-bit grayscale depth image

Returns:

Type Description
DepthImage

8-bit grayscale depth image

Source code in pixano/core/depth_image.py
def to_grayscale(
    self,
) -> "DepthImage":
    """Transform Depth image to 8-bit grayscale depth image

    Returns:
        DepthImage: 8-bit grayscale depth image
    """

    depth = self.depth_map
    d_min, d_max = depth.min(), depth.max()
    depth_n: np.ndarray = ((depth - d_min) / (d_max - d_min)) * 255
    return DepthImage(depth_map=depth_n.astype(np.uint8), shape=depth.shape)

to_struct() staticmethod

Return DepthImage type as PyArrow Struct

Returns:

Type Description
StructType

Custom type corresponding PyArrow Struct

Source code in pixano/core/depth_image.py
@staticmethod
def to_struct() -> pa.StructType:
    """Return DepthImage type as PyArrow Struct

    Returns:
        pa.StructType: Custom type corresponding PyArrow Struct
    """

    return pa.struct(
        [
            pa.field("bytes", pa.binary()),
            pa.field("shape", pa.list_(pa.int32(), list_size=2)),
        ]
    )