-1

I want to display 3D points with YELLOW color in .pcd file format (Documentation of .pcd) . Use below C# code to retrieve PCD color format from YELLOW color (R: 255, G:255, B: 0).

 byte red = 255;
 byte green = 255;
 byte blue = 0;
 int rgb = ((int)red) << 16 | ((int)green) << 8 | ((int)blue);
 float rgb2 = (float)rgb;

Then the value returned is rgb2 = 1.677696E+7

And save the points to PCD file like (x, y, z, 1.677696E+7).

I use PCD viewer to display the points but the color is not YELLOW as I am expecting ?

Is anything wrong with the viewer?

Minh Nguyen
  • 2,106
  • 1
  • 28
  • 34
  • I also found that the actual color result is the same green and blue value, but the red value is less than 128. – Minh Nguyen Aug 21 '13 at 08:50
  • How are you setting the colour of the points? i.e. above you calculate the colour but are not setting the colour of the points. Then you are saving again to PCD? – D.J.Duff Aug 21 '13 at 10:20
  • Yes. I saved the points to PCD file like (x, y, z, color). Eg: 100, 100, 100, 1.677696E+7. – Minh Nguyen Aug 21 '13 at 12:14

2 Answers2

1

I don't use C# but I believe the cast from int to float is preserving the value not the bit structure. i.e. you are not doing a reinterpret cast but actually converting the number.

Searching this website, I believe that what you will need is the BitConverter:

Two C# questions; how can I reinterpret cast a float to an int? Does C# have a non-static conversion operator or user-defined assignment operator so that the conversion takes place on 'this'?

Community
  • 1
  • 1
D.J.Duff
  • 1,565
  • 9
  • 14
  • i.e. clearly the float value you print is the same as the int value but it should not be. If the bit structure were the same then you would be getting a very different value, because a bitstring interpreted as a float is very different from the same bitstring interpreted as an int. – D.J.Duff Aug 21 '13 at 13:31
0

The problem solved. Just use the PCD color structure with Integer instead of Float (need to modify PCD file structure to use color with Integer). This way I do not need to do reinterpret cast from int to float.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Minh Nguyen
  • 2,106
  • 1
  • 28
  • 34