python-pcl icon indicating copy to clipboard operation
python-pcl copied to clipboard

How do I change pointcloud color and points dimension?

Open A7F opened this issue 7 years ago • 9 comments

Up to now, I came up with this:

cloud = pcl.PointCloud()
points = [[0,0,0],[1,0,0],[1,1,1],[2,1,2],[0,0,1]]
my_array = np.array(points, dtype=np.float32)
cloudr.from_array(my_array)
viewer = pcl_visualization.PCLVisualizering()
viewer.AddPointCloud(cloud)
viewer.Spin()

Now, instead of a white cloud, I want it let's say red, with points a little bigger to make them easily distinguishable. Is this possible? Asking here because there are no examples regarding this use case and documentation out there only covers C++.

EDIT: partially solved using:

viewer.AddPointCloud(cloud, bytes(0))
viewer.SetPointCloudRenderingProperties(pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 17, bytes(0))
pccolor = pcl.pcl_visualization.PointCloudColorHandleringCustom(cloud, 0,0,255)
viewer.AddPointCloud_ColorHandler(cloud, pccolor, bytes(1), 0)

The problem still remains because when I change point size, seems like I can't change color.

A7F avatar Oct 29 '18 16:10 A7F

Did you figure this out? I still dont know how to add a color point cloud. the documentation is terrible

soulslicer avatar Jan 24 '20 23:01 soulslicer

Hi, I send you here a very small slice of code I used in my project in which I set the color and other properties for my pointcloud visualizer. Hope it helps! Also, do you know there's another library able to handle pontclouds? I's called open3D. I decided to give it a try and works very good so it might be a chance to combine both libraries in your project according to what you are planning to do. Unfortunately, the PCL library apart from documentation is also slightly buggy...

viewer = pcl_visualization.PCLVisualizering()

# add the clouds previously made to the viewer. First pointcloud has ID 0, second has ID 2 (remember, must be bytes!)
viewer.AddPointCloud(cloud1, bytes(0))
viewer.AddPointCloud(cloud2, bytes(1))

# also add a small text saying "hello world" on position (x,y)=(100,50). Because it's a new layer, assign a new ID
testo = "hello world"
viewer.AddText(str.encode(testo), 100,50,bytes(2),0)

# set red color to the second pointcloud and increase point size to 17, making them HUGE
viewer.SetPointCloudRenderingProperties(pcl.pcl_visualization.PCLVISUALIZER_POINT_SIZE, 17, bytes(1))
pccolor = pcl.pcl_visualization.PointCloudColorHandleringCustom(cloud2, 255,0,0)
viewer.AddPointCloud_ColorHandler(cloud2, pccolor, bytes(4), 0)

# show everything
viewer.Spin()```

A7F avatar Jan 25 '20 09:01 A7F

Ok but how did you create cloud1 and cloud2 from a numpy object. I have nx3 xyz and nx3 color

soulslicer avatar Jan 25 '20 13:01 soulslicer

@A7F I have a [N*6] matrix. How do i create a cloud1 object from that

soulslicer avatar Jan 29 '20 23:01 soulslicer

ok so let's say you have this pointcloud array: cloud1_points = [[0,0,1],[0,1,0],[1,0,0]]

you put it into a numpy object cloud1_array = np.array(cloud1_points, dtype=np.float32)

then you instance a pointcloud object which has the built in method to convert the numpy array into a cloud cloud1 = pcl.PointCloud() cloud1.from_array(cloud1_array)

A7F avatar Jan 30 '20 10:01 A7F

Ok but what about color

On Thursday, January 30, 2020, Luke [email protected] wrote:

ok so let's say you have this pointcloud array: cloud1_points = [[0,0,1],[0,1,0],[1,0,0]]

you put it into a numpy object cloud1_array = np.array(cloud1_points, dtype=np.float32)

then you instance a pointcloud object which has the built in method to convert the numpy array into a cloud cloud1 = pcl.PointCloud() cloud1.from_array(cloud1_array)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/strawlab/python-pcl/issues/241?email_source=notifications&email_token=AAYLS3CRB6F6XUPEAHI5NRTRAKTPVA5CNFSM4F76LOG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKKPM6Q#issuecomment-580187770, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYLS3FI34FNA4SD2TNPTZ3RAKTPVANCNFSM4F76LOGQ .

soulslicer avatar Jan 30 '20 14:01 soulslicer

@soulslicer if you have a [N, 6] matrix, you need to reshape the matrix to [N, 4] of form (x, y, z, rgb). You can see the way they express color in a single number in python-pcl/examples/example.py: line 32. Here is the code I use to render a color point cloud from .npy files, but the color seems very strange and I don't know why. When I adjust the rgb value in example.py, I find the rgb value (255,255,255) seems to be blue, (255,255,0) seems to be green. It's confusing.

import os
import sys
import argparse
import numpy as np
import pcl
import pcl.pcl_visualization


def change_form(pt_cloud):
    if pt_cloud.shape[1] != 6:
        print("input point cloud should be of type XYZRGB")
        return None
    xyz = pt_cloud[:, :3].astype(np.float32) * 10
    rgb = np.array([x[0] << 16 | x[1] << 8 | x[2] for x in pt_cloud[:, 3:6].astype(np.int32)], dtype=np.float32)
    return np.concatenate((xyz.astype(np.float32), rgb.reshape(-1, 1)), axis=1)


if __name__ == '__main__':
    filename = "../npydata/Area_1_conferenceRoom_1.npy"
    data = np.load(file=filename).astype(np.float32)  # data.shape = [N,7] XYZRGBL
    print("data.shape: ", data.shape)
    points = data[:, 0:6]
    labels = data[:, -1]
    print("points.shape: ", points.shape)
    print("labels.shape: ", labels.shape)
    point_cloud = pcl.PointCloud_PointXYZRGB(change_form(points))
    print("point_cloud: ", point_cloud)
    viewer = pcl.pcl_visualization.CloudViewing()
    viewer.ShowColorCloud(point_cloud)
    last_c = True
    while last_c:
        last_c = not(viewer.WasStopped())

isunLt avatar Apr 07 '20 15:04 isunLt

@isunLt, I have same issue

an2tong avatar Aug 06 '20 07:08 an2tong

It is a bug. Use myCloud = pcl.PointCloud_PointXYZRGBA()

ValterZoldhegyi avatar Sep 17 '20 23:09 ValterZoldhegyi