通过在ros中使用Opencv对图片进行黑白处理:从ROS中读取图象,转换后将彩色图象变成灰度图象,并返回灰度图象,转换后在ROS下输出。帮助理解ros的通讯机制。 使用系统简介: ubuntu18.04 ros melodic
ROS Melodic 安装OpenCV
在ROS里和opencv相关的package有三个:vision_opencv, cv_bridge, 和image_transport,在安装ros时一般都打包安装过了,不需要下载opencv的包安装
1.创建工作空间
mkdir -p cv_ws/src
cd src
catkin_init_workspace
cd ..
catkin_make
2.在工作空间下创建程序包
cd src
catkin_create_pkg robot_vision roscpp std_msgs cv_bridge image_transport sensor_msgs
cd ..
catkin_make
3.创建发布者与订阅者
发布者
在创建的程序包robot_vision的src文件中创建一个文本文件,并命名为image_publisher.cpp
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include<iostream>
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it_(nh);
image_transport::Publisher image_pub_=it_.advertise("camera/rgb/image_raw", 1);
cv::Mat image = cv::imread("RGB.png");
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
ros::Rate loop_rate(5);
while (nh.ok()) {
image_pub_.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
订阅者
在创建的程序包robot_vision的src文件中创建一个文本文件,并命名为grayImage.cpp
#include<ros/ros.h>
#include<iostream>
#include<cv_bridge/cv_bridge.h>
#include<sensor_msgs/image_encodings.h>
#include<image_transport/image_transport.h>
#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";
class RGB_GRAY
{
private:
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
public:
RGB_GRAY()
:it_(nh_)
{
image_sub_ = it_.subscribe("camera/rgb/image_raw", 1, &RGB_GRAY::convert_callback, this);
cv::namedWindow(INPUT);
cv::namedWindow(OUTPUT);
}
~RGB_GRAY()
{
cv::destroyWindow(INPUT);
cv::destroyWindow(OUTPUT);
}
void convert_callback(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);
}
catch(cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
image_process(cv_ptr->image);
}
void image_process(cv::Mat img)
{
cv::Mat img_out;
cv::cvtColor(img, img_out, CV_RGB2GRAY);
if(!img.empty())
{
cv::imshow(INPUT, img);
}
if(!img_out.empty())
{
cv::imshow(OUTPUT, img_out);
}
cv::waitKey(5);
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "grayImage");
RGB_GRAY obj;
ros::spin();
}
4.编译成可执行文件
在编写程序后,这个文本程序在编译成可执行文件之前是不能够运行的。首先在建立的robot_vision的程序包中的CMakeLists.txt文件中加入如下代码:
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(image_publisher src/image_publisher)
target_link_libraries(image_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
add_executable(grayImage src/grayImage.cpp) //将src中的文件添加成名字为grayImage的可执行文件
target_link_libraries(grayImage ${catkin_LIBRARIES} ${OpenCV_LIBS}) //将src中的文件添加成名字为grayImage的可执行文件
add_dependencies(grayImage robot_vision_generate_messages_cpp) //给可执行文件添加依赖包
5.运行
编译
$ cd cv_ws
$ catkin_make
运行
$ roscore
$ source devel/setup.bash
$ rosrun robot_vision grayImage
$ rosrun robot_vision image_publisher
运行结果
6.参考
在ROS中使用opencv进行简单的图像处理 在ROS中使用opencv查看图片 Ubuntu 18.04 + ROS Melodic 安装OpenCV 文档看得不仔细没法做太多原理性的解释,空了补上呀~
|