ROS保存RBGD的深度图像、RGB图像

11 篇文章 14 订阅

前言

想要采集一些深度图和RGB图像,且两者是一一对应的,即一张深度图对应一张RGB图像,于是就有了以下的程序。

相关准备

我用的深度摄像头是奥比中光的摄像头,Ubuntu16的好像可以直接用sudo apt-get install ros-xxxx进行下载,我用的Ubuntu18好像没有直接的包,所以要找到相关驱动并下载下来,这里我就不多介绍了,CSDN还是有很多优质资源介绍如何下载和使用的。

如果有其他RGBD摄像头也可以使用其他的。

Show you the Code

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import rospy
from geometry_msgs.msg import PointStamped
from nav_msgs.msg import Path
from sensor_msgs.msg import Image
import cv2 as cv
import numpy as np
from cv_bridge import CvBridge

class save_image():
    def __init__(self):
        self.count = 0
        self.cvbridge = CvBridge()
    
    def message(self, data):
        print(data.encoding)

    
    def save_image(self, data):
        image = self.cvbridge.imgmsg_to_cv2(data,  desired_encoding='rgb8')
        image = cv.cvtColor(image,cv.COLOR_BGR2RGB)
        image = image[144:336]

        if self.count < 10:
            name = '00000{}'.format(self.count)      
        elif self.count < 100 and self.count >= 10:
            name = '0000{}'.format(self.count)      
        elif self.count < 1000 and self.count >= 100:
            name = '00{}'.format(self.count)      
        elif self.count < 10000 and self.count >= 1000:
            name = '0{}'.format(self.count)      
        elif self.count < 100000 and self.count >= 10000:
            name = '{}'.format(self.count)    
        else:
            name = '0000000000000'
        
        cv.imwrite('/home/a/gazebo_test_ws/src/DF_VO/nodes/image/{}.jpg'.format(name), image)
        
        print('image:  {}'.format(name))

        self.count += 1

    
    def save_depth(self, data):
        depth = self.cvbridge.imgmsg_to_cv2(data,  desired_encoding='16UC1')
        depth = depth[144:336]

        if self.count < 10:
            name = '00000{}'.format(self.count)      
        elif self.count < 100 and self.count >= 10:
            name = '0000{}'.format(self.count)      
        elif self.count < 1000 and self.count >= 100:
            name = '000{}'.format(self.count)      
        elif self.count < 10000 and self.count >= 1000:
            name = '00{}'.format(self.count)      
        elif self.count < 100000 and self.count >= 10000:
            name = '0{}'.format(self.count)    
        else:
            name = '0000000000000'
        
        cv.imwrite('/home/a/gazebo_test_ws/src/DF_VO/nodes/depth/{}.png'.format(name), depth)

        print('depth:  {}'.format(name))


'''-------------define main----------------'''
if __name__ == '__main__':
    try:
        a = save_image()
        rospy.init_node('save_image', anonymous = True)
        rospy.Subscriber("/image_raw", 
                         Image, 
                         a.save_image)  
        rospy.Subscriber("/camera/depth/image_raw", 
                         Image, 
                         a.save_depth)  
        rospy.spin()
    except rospy.ROSInterruptException:
        pass

Run

  1. 启动RGBD摄像头(以奥比中光的摄像头为例)
roslaunch astra_launch astra.launch
  1. 查阅相关的话题,看一下深度图数据和RGB图数据的话题是那个,这里可以使用rviz查看,但是我用rviz无法直接查看到RGB图,所以用了rqt_image_view包,如图左上角的就是RGB的话题名称,rviz里默认的是不正确的,会存在警告。
rosrun rqt_image_view rqt_image_view

请添加图片描述

  1. 运行Code,运行前需要保证代码路径下有一个depth和image文件夹,不然可能会报错哦
    请添加图片描述

  2. 检查,这时候image里和depth里面的图像就会一一对应了

其他要注意的

  1. RGB和深度图的解码格式是不一样的
depth = self.cvbridge.imgmsg_to_cv2(data,  desired_encoding='16UC1')
image = self.cvbridge.imgmsg_to_cv2(data,  desired_encoding='rgb8')

这个地方的desired_encoding需要查阅sensor_msgs/Image 消息的格式,看一下里面的encoding是什么,这里就写什么

  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值