当前位置:网站首页>ROS——使用OpenCV实现摄像头的发送和接收
ROS——使用OpenCV实现摄像头的发送和接收
2022-04-21 21:57:00 【Irving.Gao】
使用OpenCV读取摄像头并发布图像话题
image_pub.cpp
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <cv_bridge/cv_bridge.h>
#include <iostream>
int main(int argc, char** argv)
{
ros::init(argc, argv, "img_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("/camera/color/image_raw", 1);
cv::VideoCapture cap;
cv::Mat frame;
int deviceID=0;
if(argc>1)
deviceID=argv[1][0]-'0';
int apiID=cv::CAP_ANY;
cap.open(deviceID+apiID);
if(!cap.isOpened()){
std::cerr<<"ERROR! Unable to open camera"<<std::endl;
return -1;
}
ros::Rate loop_rate(30);
while (nh.ok()) {
cap.read(frame);
if(!frame.empty()){
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
pub.publish(msg);
}
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
使用OpenCV接收图像
image_sub.cpp
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
cv::Mat image;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
ROS_INFO_STREAM("Get Msg");
try
{
image = cv_bridge::toCvShare(msg, "bgr8") -> image;
cv::imshow("view", image);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
cv::waitKey(1);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_listener");
ros::NodeHandle nh;
cv::namedWindow("view");
cv::startWindowThread();
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe("/camera/color/image_raw", 1, imageCallback);
ros::spin();
cv::destroyWindow("view");
}
CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
roscpp
OpenCV
cv_bridge
image_transport
)
find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_executable(image_pub src/image_pub.cpp)
target_link_libraries(image_pub ${catkin_LIBRARIES} ${OpenCV_LIBS} ${Boost_LIBRARIES})
add_executable(image_sub src/image_sub.cpp)
target_link_libraries(image_sub ${catkin_LIBRARIES} ${OpenCV_LIBS} ${Boost_LIBRARIES})

编译过程报错解决
- 参考文章:OpenCV找不到config.cmake(简单直接的解决方法):Could not find a package configuration file provided by “OpenCV“
C++节点和python节点间传送图像
版权声明
本文为[Irving.Gao]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45779334/article/details/124314425
边栏推荐
- MYPINPAD和SmartPesa合并,成为移动支付受理领域的全球领导者
- GD32F303学习笔记(1)——搭建环境、编译烧写
- Leetcode0785. 判断二分图(medium,二分图,DFS)
- ISMAR 2022 Journal Paper (TVCG)投稿交流
- The @ select annotation is used in the mapper of mybtais, and the if annotation is used
- LU分解、LDLT分解和Cholesky分解_zjuzly的博客-CSDN博客_ldlt
- UML之一综合设计例题
- kotlin爬虫app,Android开发面试准备
- Ffmpeg serial 3-video decoding
- es6如何求两个数组的交集
猜你喜欢

GD32F103学习笔记(8)——ADC接口使用

During iPhone test, the picture of custom tabbar will slide along the screen

UML integrated design example

Live555 learning

Practice of JVM custom class loader in code extensibility

JS to realize automatic scrolling of announcements

Restcloud ETL开箱即用-永久免费

什么?你们公司还没有将JVM初始和最大堆内存大小设置为相同值?

static,const,volatile,extern,register关键字深入解析

Short video live broadcast mode enables agricultural products in remote areas to "go global"
随机推荐
2022 safety officer-a certificate examination exercises and online simulation examination
How to realize the automatic message sending function of wechat with vbs
FFmpeg连载3-视频解码
面试必刷算法TOP101之背包九讲篇 TOP14
数据库设计与实现
Ffmpeg serial 3-video decoding
LU decomposition, LDLT decomposition and Cholesky decomposition_ Zjuzly's blog - CSDN blog_ ldlt
【ES6】函数的扩展
ROS机器人从起点到终点(四)蓝桥云实践复现
找到新技术与自身的结合方式,才是开启产业互联网发展的关键所在
GD32F303学习笔记(1)——搭建环境、编译烧写
GAMES101 Lec6 反走样与深度缓冲
Oracle查询执行计划
Architecture document of outsourcing student management system
[use case level definition]
LU分解、LDLT分解和Cholesky分解_zjuzly的博客-CSDN博客_ldlt
Is it safe for futures to open an account directly online?
Travel notes of provincial election and later basic planning
【ES6】let和const命令
Live555 learning