2016-12-09 10 views
1

人々、ROS CompressedDepthはnumpyの(又はCV2)

私はCompressedDepth変換する出発点として、このリンクを使用していた(タイプの画像:;メートルで "32FC1 compressedDepthを、")OpenCVのフレームの画像:

Python CompressedImage Subscriber Publisher

compressedDepを変換する正しい方法は何であるなど、私が印刷しようとすると、私は空のデータを取得し、または私は私の配列の結果を見たとき、私は非型を取得しますイメージ? wifi /ルータの帯域幅と速度の制約には、再発行は機能しません。

+0

あなたがこれまで持っているものを共有気にしませんか? –

+0

行43が「/ camera/depth_registered/image_raw/compressedDepth;」に変更されました。 行57が に変更されました。 "image_np = cv2.imdecode(np_arr、cv2.CV_LOAD_IMAGE_GRAYSCALE);" データ以外は、「機能」と「再発行部分」を削除しました。他のすべては同じことです。 – Pototo

答えて

0

compressedDepthをデコードする正しい方法は、最初に生データからヘッダーを削除し、残りのデータを変換することです。

これはimage_transport_plugins/compressed_depth_image_transport/src/codec.cppに記載されています。 私のマシンでは、ヘッダーサイズは12バイトです。ただし、列挙型のサイズが定義されていないため、これは他のアーキテクチャでは異なる場合があります。

PNGファイルとして16UC1と32FC1奥行き画像を圧縮し、次のPythonのコードスニペットの輸出:

# 'msg' as type CompressedImage 
depth_fmt, compr_type = msg.format.split(';') 
# remove white space 
depth_fmt = depth_fmt.strip() 
compr_type = compr_type.strip() 
if compr_type != "compressedDepth": 
    raise Exception("Compression type is not 'compressedDepth'." 
        "You probably subscribed to the wrong topic.") 

# remove header from raw data 
depth_header_size = 12 
raw_data = msg.data[depth_header_size:] 

depth_img_raw = cv2.imdecode(np.fromstring(raw_data, np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED) 
if depth_img_raw is None: 
    # probably wrong header size 
    raise Exception("Could not decode compressed depth image." 
        "You may need to change 'depth_header_size'!") 

if depth_fmt == "16UC1": 
    # write raw image data 
    cv2.imwrite(os.path.join(path_depth, "depth_" + str(msg.header.stamp) + ".png"), depth_img_raw) 
elif depth_fmt == "32FC1": 
    raw_header = msg.data[:depth_header_size] 
    # header: int, float, float 
    [compfmt, depthQuantA, depthQuantB] = struct.unpack('iff', raw_header) 
    depth_img_scaled = depthQuantA/(depth_img_raw.astype(np.float32)-depthQuantB) 
    # filter max values 
    depth_img_scaled[depth_img_raw==0] = 0 

    # depth_img_scaled provides distance in meters as f32 
    # for storing it as png, we need to convert it to 16UC1 again (depth in mm) 
    depth_img_mm = (depth_img_scaled*1000).astype(np.uint16) 
    cv2.imwrite(os.path.join(path_depth, "depth_" + str(msg.header.stamp) + ".png"), depth_img_mm) 
else: 
    raise Exception("Decoding of '" + depth_fmt + "' is not implemented!")