ROS+OpenCV 일반 이미지의 주제를 내보내는 노드[c++][Python]

7627 단어 C++파이썬OpenCVROS

실행 환경



우분투 16.04
ROS kinetic

코드 [c++]



(2019/6/10 업데이트)

dummy_image_pub_node.cpp
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

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/color/image_raw", 1);
  cv::Mat image(cv::Size(640, 480), CV_8UC3, cv::Scalar(0, 100, 0));

  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

  ros::Rate loop_rate(1000);//Hz
  while (nh.ok()) {
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
}

실행 결과





코드 [Python]



(2020/8/23 업데이트)

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


import rospy
from sensor_msgs.msg import Image 
from cv_bridge import CvBridge
import cv2
import numpy as np



if __name__ == '__main__':
    pub = rospy.Publisher('/camera/color/image_raw', Image, queue_size=10)
    rospy.init_node('image_publisher')
    r = rospy.Rate(10) # 10hz

    cols = 640
    rows = 480
    img = np.full((rows, cols, 3), 0, dtype=np.uint8)
    img[0:480, 0:640, 1] = 100

    bridge = CvBridge()
    imgMsg = bridge.cv2_to_imgmsg(img, "bgr8")


    while not rospy.is_shutdown():
        rospy.loginfo(img)

        pub.publish(imgMsg)

        r.sleep()


참고 사이트

좋은 웹페이지 즐겨찾기