【问题标题】:ROS CompressedDepth to numpy (or cv2)ROS CompressedDepth 到 numpy(或 cv2)
【发布时间】:2016-12-09 02:02:44
【问题描述】:

伙计们,

我使用此链接作为起点,将我的 CompressedDepth(图像类​​型:“32FC1;compressedDepth”,以米为单位)图像转换为 OpenCV 帧:

Python CompressedImage Subscriber Publisher

当我尝试打印时得到一个空数据,或者当我看到我的数组的结果时得到一个 NonType,等等。

转换压缩深度图像的正确方法是什么? 重新发布对 wifi/路由器带宽和速度限制不起作用。

【问题讨论】:

  • 您介意分享一下您目前所拥有的吗?
  • 第 43 行更改为“/camera/depth_registered/image_raw/compressedDepth;”第 57 行更改为“image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_GRAYSCALE);”数据之外,去掉了“特征”和“再版部分”。其他一切都是一样的。

标签: python numpy compression ros subscriber


【解决方案1】:

解码compressedDepth的正确方法是先从原始数据中去掉header,然后再转换剩余的数据。

这在image_transport_plugins/compressed_depth_image_transport/src/codec.cpp 中有记录。 在我的机器上,标头大小为 12 个字节。然而,这在其他架构上可能会有所不同,因为枚举的大小没有定义。

以下 python 代码 sn-p 将压缩的 16UC1 和 32FC1 深度图像导出为 png 文件:

# '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!")

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多