knorth55's memo

Technical memo by @knorth55

C++でuint8配列に32FC1などのfloat配列を格納する

Depth Imageは32FC1などのfloat型(32bit, 4byte)の配列である場合があります。
しかしROSのsensor_msgs/Imageのdataはuint8_t型(8bit, 1byte)の配列であり直接追加することはできません。

ここでやるべきことはfloat型を4つのuin8_tに分割する変換です。
floatの配列からuint8_t型の配列への変換はmemcopyを使うことでできますが、pixel単位での変換ではありません。
pixel単位で行う場合は以下のように行います。

  float depth_pixel;
  uint8_t *depth_pixel_array = reinterpret_cast<uint8_t *>(&depth_pixel);

これをsensor_msgs/Imageのdataに順に格納していけばOKです。


具体的な32FC1の変換は以下のGistです。