Skip to main content

Read and write PCL .pcd files in python

Project description

pypcd4

Test PyPI - Version PyPI - Python Version GitHub License PyPI - Downloads

Table of Contents

Description

pypcd4 is a modern reimagining of the original pypcd library, offering enhanced capabilities and performance for working with Point Cloud Data (PCD) files.

This library builds upon the foundation laid by the original pypcd while incorporating modern Python3 syntax and methodologies to provide a more efficient and user-friendly experience.

Installation

To get started with pypcd4, install it using pip:

pip install pypcd4

Usage

Let’s walk through some examples of how you can use pypcd4:

Getting Started

First, import the PointCloud class from pypcd4:

from pypcd4 import PointCloud

Working with .pcd Files

If you have a .pcd file, you can read it into a PointCloud object:

pc: PointCloud = PointCloud.from_path("point_cloud.pcd")

pc.fields
# ('x', 'y', 'z', 'intensity')

Converting Between PointCloud and NumPy Array

You can convert a PointCloud to a NumPy array:

array: np.ndarray = pc.numpy()

array.shape
# (1000, 4)

You can also specify the fields you want to include in the conversion:

array: np.ndarray = pc.numpy(("x", "y", "z"))

array.shape
# (1000, 3)

And you can convert a NumPy array back to a PointCloud. The method you use depends on the fields in your array:

# If the array has x, y, z, and intensity fields,
pc = PointCloud.from_xyzi_points(array)

# Or if the array has x, y, z, and label fields,
pc = PointCloud.from_xyzl_points(array, label_type=np.uint32)

Creating Custom Conversion Methods

If you can’t find your preferred point type in the pre-defined conversion methods, you can create your own:

fields = ("x", "y", "z", "intensity", "new_field")
types = (np.float32, np.float32, np.float32, np.float32, np.float64)

pc = PointCloud.from_points(array, fields, types)

Working with ROS PointCloud2 Messages

You can convert a ROS PointCloud2 Message to a PointCloud and vice versa:

def callback(in_msg: sensor_msgs.msg.PointCloud2):
    # Convert ROS PointCloud2 Message to a PointCloud
    pc = PointCloud.from_msg(in_msg)

    pc.fields
    # ("x", "y", "z", "intensity", "ring", "time")

    # Convert PointCloud to ROS PointCloud2 Message with the input message header
    out_msg = pc.to_msg(in_msg.header)

    publisher.publish(out_msg)

Concatenating Two PointClouds

The pypcd4 supports concatenating two PointCloud objects together using the + operator. This can be very useful when you want to merge two point clouds into one.

Here's how you can use it:

pc1: PointCloud = PointCloud.from_path("xyzi1.pcd")
pc2: PointCloud = PointCloud.from_path("xyzi2.pcd")

# Concatenate two PointClouds
pc3: PointCloud = pc1 + pc2

Please note that the two PointCloud objects must have the same fields and types. If they don’t, a ValueError will be raised.

Filtering a PointCloud

The pypcd4 library provides a convenient way to filter a PointCloud using a subscript.

Using a Slice

You can use a slice to access a range of points in the point cloud. Here’s an example:

# Create a point cloud with random points
pc = PointCloud.from_xyz_points(np.random.rand(10, 3))

# Access points using a slice
subset = pc[3:8]

In this case, subset will be a new PointCloud object containing only the points from index 3 to 7.

Using a Boolean Mask

You can use a boolean mask to access points that satisfy certain conditions. Here’s an example:

# Create a point cloud with random points
pc = PointCloud.from_xyz_points(np.random.rand(10000, 3))

# Create a boolean mask
mask = (pc.pc_data["x"] > 0.5) & (pc.pc_data["y"] < 0.5)

# Access points using the mask
subset = pc[mask]

In this case, subset will be a new PointCloud object containing only the points where the x-coordinate is greater than 0.5 and the y-coordinate is less than 0.5.

Using Field Names

You can use a field name or a sequence of field names to access specific fields in the point cloud. Here’s an example:

# Create a point cloud with random points
pc = PointCloud.from_xyz_points(np.random.rand(100, 3))

# Access specific fields
subset = pc[("x", "y")]

In this case, subset will be a new PointCloud object containing only the x and y coordinates of the points. The z-coordinate will not be included.

Saving Your Work

Finally, you can save your PointCloud as a .pcd file:

pc.save("nice_point_cloud.pcd")

Contributing

We are always looking for contributors. If you are interested in contributing, please run the lint and test before submitting a pull request:

Using Rye (Recommended)

Just run the following command:

rye sync
rye run lint

Using pip

Install the testing dependencies by the following command:

pip install mypy pytest ruff

Then run the following command:

ruff check --fix src
ruff format src
mypy src
pytest

Make sure all lints and tests pass before submitting a pull request.

License

The library was rewritten and does not borrow any code from the original pypcd library. Since it was heavily inspired by the original author's work, we extend his original BSD 3-Clause License and include his Copyright notice.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypcd4-1.1.0.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

pypcd4-1.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file pypcd4-1.1.0.tar.gz.

File metadata

  • Download URL: pypcd4-1.1.0.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for pypcd4-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a10e4769d74691eb1e564304005c98c7693b17522cffaa53c3513f812e597469
MD5 113bf844ab60fcf9cb60441f2b9645ad
BLAKE2b-256 96809d997a0b0d0b11653f4c18c5b0846fdfefcef6e8cc1581c8e0492b2041b1

See more details on using hashes here.

File details

Details for the file pypcd4-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pypcd4-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for pypcd4-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c30385025bc691b6481211edc9d30bf772c86a2ffeb8a173b45863aec7d708fa
MD5 65dbb922c39f092c7b15e72006d549f2
BLAKE2b-256 b62887aaffd9928b453ff6efc941432bb55d989de466c23be98bc2b3a061b6d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page