我是靠谱客的博主 幽默豆芽,最近开发中收集的这篇文章主要介绍ROS:OpenCV读取摄像头并发布话题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ROS:OpenCV读取本地照片发布到rviz中 

ROS读取摄像头视频数据发布到rviz中

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <vector>
//#include <netinet/in.h>
//#include <sys/socket.h>
//#include <arpa/inet.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
//#include <pthread.h>
//#include "opencv2/opencv.hpp"
using namespace std;

#include <stdio.h>

cv::Mat img0,img1;
	
int main(int argc, char** argv)
{
	ros::init(argc, argv, "image_publisher");
	ros::NodeHandle nh;
	image_transport::ImageTransport it(nh);
	image_transport::Publisher pub = it.advertise("camera/image", 1);
	
	cv::VideoCapture capture(0);
	while (nh.ok()) {
//	cv::VideoCapture capture(0);
	//img0为3通道
	capture >> img0;
	//cout << ":img0.channels()" << img0.channels() << endl;
	//转换为灰度图 ,灰度图为单通道
	cv::cvtColor(img0, img1, CV_RGB2GRAY);
	//cout << ":img1.channels()" << img1.channels() << endl;
	//灰度单通道转化为三通道
	cv::Mat three_channel = cv::Mat::zeros(img0.rows,img0.cols,CV_8UC3);
    vector<cv::Mat > channels;
    for (int i=0;i<3;i++)
    {
        channels.push_back(img1);
    }
    merge(channels,three_channel);
	//cout << ":three_channel.channels()" << three_channel.channels() << endl;
	sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", three_channel).toImageMsg();
	pub.publish(msg);
	//ros::spinOnce();
	//loop_rate.sleep();
	}
}

在CMakeLists.txt中添加

find_package(OpenCV REQUIRED)
add_executable(cameraproc
  src/cameraproc.cpp
)
target_link_libraries(cameraproc
  ${catkin_LIBRARIES}
  ${OpenCV_LIBRARIES}
)

 

最后

以上就是幽默豆芽为你收集整理的ROS:OpenCV读取摄像头并发布话题的全部内容,希望文章能够帮你解决ROS:OpenCV读取摄像头并发布话题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部