Ask Your Question

Color problems extracting RGB from Pointcloud2

asked 2019-04-24 17:01:20 -0500

Enmar gravatar image

updated 2019-04-25 17:07:31 -0500

Hello all,

I am trying to convert a PointCloud2 message into an xyzrgb text file but am having problems with the rgb.

I am using and Intel RealSense camera and can extract RGB values just fine using the following code.

    def store_cloud(self, pc2_msg): = []
        gen = pc2.read_points(pc2_msg, skip_nans=True, field_names=("x", "y", "z", "rgb"))
        for data in gen:            
            test = data[3]
            s = struct.pack('>f' ,test)
            i = struct.unpack('>l',s)[0]
            pack = ctypes.c_uint32(i).value
            r = int((pack & 0x00FF0000)>> 16)
            g = int((pack & 0x0000FF00)>> 8)
            b = int((pack & 0x000000FF))
  [data[0], data[1], data[2], r, g, b])

However upon viewing the point cloud in CloudCompare the colors behave strangely.

Some of the colors such as my skin and a good portion of the cieling and walls are colored correctly, but the rest of the image is colored wrong. Blue botches bloom where there is high reflection and my pink shirt turns blue. The flourescent lights are also a bright shade of blue. I do not understand how there can be such a discrepancy between the RVIZ display and the colored point cloud i extracted.

These images show the problem I am having.

If anyone has any ideas of how to fix this i would love to hear them.

RVIZ_pointcloud image description

My_pointcloud image description

edit retag flag offensive close merge delete


Please attach your images directly to your question. I've given you sufficient karma.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-25 02:08:39 -0500 )edit

@gvdhoorn Thanks for the karma!

Enmar gravatar image Enmar  ( 2019-04-25 17:06:12 -0500 )edit

I am also facing the same problem? Is there a solution/ reason to it?

Anuj07 gravatar image Anuj07  ( 2020-01-09 05:21:42 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-03-20 15:30:46 -0500

Enmar gravatar image

While i don't have a fix to show i was able to determine what was causing my coloring errors from the pointcloud.


Each field of the PointCloud2 message has a datatype property that tells you how to unpack the data into its proper values. I was attempting to unpack one datatype as another which caused the mismatch.


Each point cloud message contains a property msg.fields which describes the type of data stored in the point cloud message.

For the RealSense D435i they are set up as follows

  • msg.fields[0]/name = "x"
  • msg.fields[1]/name = "y"
  • msg.fields[2]/name = "z"
  • msg.fields[3]/name = "rgb"

The RealSense camera publishes the xyz fields with a datatype 7 (float32) and the rgb field with a datatype of 6 (uint32). See the ROS PointCloud2 message description for more details.

You must first be careful when unpacking the data to make sure you unpack it assuming the correct datatype as that may change between your differing colored point cloud sources. Then you must ensure to cast/store the individual unpacked r, g, b using a datatype that displays their proper value (uint8). Only then will you get the correct 0-255 range for the data.

I know this explanation is lacking but i hope it helps some of you!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-04-24 17:01:20 -0500

Seen: 628 times

Last updated: Mar 20 '20