Python ROS压缩深度至颈部(或cv2)
各位 我使用此链接作为起点,将压缩深度(图像类型:“32FC1;compressedepth”,以米为单位)图像转换为OpenCV帧: 当我尝试打印时会得到一个空数据,或者当我看到数组的结果时会得到一个非类型,等等 转换压缩深度图像的正确方法是什么?Python ROS压缩深度至颈部(或cv2),python,numpy,compression,ros,subscriber,Python,Numpy,Compression,Ros,Subscriber,各位 我使用此链接作为起点,将压缩深度(图像类型:“32FC1;compressedepth”,以米为单位)图像转换为OpenCV帧: 当我尝试打印时会得到一个空数据,或者当我看到数组的结果时会得到一个非类型,等等 转换压缩深度图像的正确方法是什么? 重新发布不会影响wifi/路由器的带宽和速度限制。解码压缩数据的正确方法是首先从原始数据中删除标题,然后转换剩余数据 这在中有记录。 在我的机器上,标题大小是12字节。但是,这在其他体系结构上可能有所不同,因为没有定义枚举的大小 以下python
重新发布不会影响wifi/路由器的带宽和速度限制。解码压缩数据的正确方法是首先从原始数据中删除标题,然后转换剩余数据 这在中有记录。 在我的机器上,标题大小是12字节。但是,这在其他体系结构上可能有所不同,因为没有定义枚举的大小 以下python代码段将压缩的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!")
解码压缩数据头的正确方法是首先从原始数据中删除头,然后转换剩余数据 这在中有记录。 在我的机器上,标题大小是12字节。但是,这在其他体系结构上可能有所不同,因为没有定义枚举的大小 以下python代码段将压缩的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!")
您是否介意分享您到目前为止拥有的内容?第43行更改为“/camera/depth\u registed/image\u raw/compressedepth”;第57行更改为“image\u np=cv2.imdecode(np\u arr,cv2.CV\u LOAD\u image\u GRAYSCALE);”除数据外,删除了“特征”和“重新发布部分”其他一切都是一样的。你介意分享你到目前为止所拥有的吗?第43行改为“/camera/depth\u registed/image\u raw/compressedepth”;第57行改为“image\u np=cv2.imdecode(np\u arr,cv2.CV\u LOAD\u image\u GRAYSCALE);”除数据外,删除了“功能”和“重新发布部分”。其他一切都是一样的。