ROS+OpenCV读取图像

ROS+OpenCV读取图像

新建一个freenect.launch文件,填入一下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<launch>
<!-- 启动freenect驱动 -->
<include file="$(find freenect_launch)/launch/freenect.launch">
<arg name="publish_tf" value="false" />
<arg name="depth_registration" value="true" />
<arg name="rgb_processing" value="true" />
<arg name="ir_processing" value="false" />
<arg name="depth_processing" value="false" />
<arg name="depth_registered_processing" value="true" />
<arg name="disparity_processing" value="false" />
<arg name="disparity_registered_processing" value="false" />
<arg name="sw_registered_processing" value="false" />
<arg name="hw_registered_processing" value="true" />
</include>
</launch>

在ros下创建工作空间

1
2
3
4
5
mkdir -p cv_ws/src
cd cv_ws/src
catkin_init_workspace
cd ..
catkin_make

在工作空间下创建程序包

1
2
3
cd src
catkin_create_pkg robot_vision roscpp std_msgs cv_bridge image_transport sensor_msgs
cd ..
1
创建程序包的一般格式是catkin_create_pkg <name> <dependencies package>

在src/robot_vision/src/目录下创建getImage.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<ros/ros.h> //ros标准库头文件
#include<iostream> //C++标准输入输出库
/*
cv_bridge中包含CvBridge库
*/
#include<cv_bridge/cv_bridge.h>
/*
ROS图象类型的编码函数
*/
#include<sensor_msgs/image_encodings.h>
/*
image_transport 头文件用来在ROS系统中的话题上发布和订阅图象消息
*/
#include<image_transport/image_transport.h>

//OpenCV2标准头文件
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

static const std::string INPUT = "Input"; //定义输入窗口名称
static const std::string OUTPUT = "Output"; //定义输出窗口名称
static const std::string DEPTH = "DEPTH"; //定义depth窗口名称

//定义一个转换的类
class Main
{
private:
ros::NodeHandle nh_; //定义ROS句柄
image_transport::ImageTransport it_; //定义一个image_transport实例
image_transport::Subscriber image_sub_0,image_sub_1; //定义ROS图象接收器
//image_transport::Publisher image_pub_; //定义ROS图象发布器
public:
Main()
:it_(nh_) //构造函数
{
image_sub_0 = it_.subscribe("/camera/rgb/image_raw", 1, &Main::convert_callback, this); //定义图象接受器,订阅话题是“camera/rgb/image_raw”
image_sub_1 = it_.subscribe("/camera/depth_registered/image_raw", 1, &
Main::convert_callback_depth, this); //定义图象接受器,订阅话题是“/camera/depth_registered/image_raw”

// image_pub_ = it_.publishe("", 1); //定义图象发布器
//初始化输入输出窗口
cv::namedWindow(INPUT);
cv::namedWindow(OUTPUT);
cv::namedWindow(DEPTH);
}
~Main() //析构函数
{
cv::destroyWindow(INPUT);
cv::destroyWindow(OUTPUT);
cv::destroyWindow(DEPTH);
}
/*
rgb图像的转换会调函数
*/
void convert_callback(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr; // 声明一个CvImage指针的实例

try
{
cv_ptr = cv_bridge::toCvCopy(msg, "bgr8"); //将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
}
catch(cv_bridge::Exception& e) //异常处理
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}

image_process(cv_ptr->image); //得到了cv::Mat类型的图象,在CvImage指针的image中,将结果传送给处理函数
}
/*
depth图像的转换会调函数
*/
void convert_callback_depth(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr; // 声明一个CvImage指针的实例

try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1); //将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
}
catch(cv_bridge::Exception& e) //异常处理
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}

imshow(DEPTH,cv_ptr->image); //得到了cv::Mat类型的图象,在CvImage指针的image中,将结果传送给处理函数
}
/*
这是图象处理的主要函数,一般会把图像处理的主要程序写在这个函数中。这里的例子只是一个彩色图象到灰度图象的转化
*/
void image_process(cv::Mat img)
{
cv::Mat img_out;
cv::cvtColor(img, img_out, CV_RGB2GRAY); //转换成灰度图象
cv::imshow(INPUT, img);
cv::imshow(OUTPUT, img_out);
cv::waitKey(5);
}
};

//主函数
int main(int argc, char** argv)
{
ros::init(argc, argv, "RGB");
Main obj;
ros::spin();
}
  • 将ROS消息中的图象信息提取,生成新cv类型的图象,复制给CvImage指针
    • cv_ptr = cv_bridge::toCvCopy(msg, “bgr8”);
    • cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1);
    • 第二个参数表示转换的类型
      • mono8: CV_8UC1, grayscale image
      • mono16: CV_16UC1, 16-bit grayscale image
      • bgr8: CV_8UC3, color image with blue-green-red color order
      • rgb8: CV_8UC3, color image with red-green-blue color order
      • bgra8: CV_8UC4, BGR color image with an alpha channel
      • rgba8: CV_8UC4, RGB color image with an alpha channel
    • cv_ptr->image转化成cv::Mat类型
  • ros图片路径
    • rgb图片:/camera/rgb/image_raw
    • 深度图片:/camera/depth_registered/image_raw

向src/robot_vision/CMakeLists.txt添加如下内容

1
2
3
4
5
FIND_PACKAGE(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(getImage src/getImage.cpp)
target_link_libraries(getImage ${catkin_LIBRARIES} ${OpenCV_LIBS})
add_dependencies(getImage robot_vision_generate_messages_cpp)

编译

  • 返回根目录
  • 终端输入

    catkin_make

运行

  • 将节点导入bash,在终端输入

    source ./devel/setup.bash

  • 打开另一个终端进入之前创建的freenect.launch目录,输入

    roslaunch freenect.launch

  • 在终端输入运行(rosrun 包名 节点名)

    rosrun robot_vision getImage

参考博客