Menu

Video Stream Template

brandon shrewsbury

Video Streams are pretty common video feed from teleoperated robot's yet each transport video feeds a different way. Luckily OpenCV has a pretty wide interface for consuming video feeds. Below is an example of the OpenCV to ROS functionality. You can substitute the cvCaptureFromFile parameter with just about any feed or file but you may have to change the encoding.

AirRobot video Capture Example

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cv_bridge/cv_bridge.h>

namespace enc = sensor_msgs::image_encodings;

static const char WINDOW[] = "Image window";


int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_ = image_transport::ImageTransport(nh_);
  image_transport::Publisher pub = it_.advertise("camera/airrobot", 1);
  cv::Mat frame;
  CvCapture* pCapture;
  pCapture = cvCaptureFromFile("udp://@localhost:1234");
  cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
  cv_ptr->header.frame_id = "/pe_camera";
  cv_ptr->encoding = "bgr8";


  while(true)
  {     
     frame = cvQueryFrame(pCapture);
     cv::imshow(WINDOW, frame);
     cv_ptr->image = frame;

     // Publish the image to ROS
     cv_ptr->header.stamp = ros::Time::now();
     pub.publish(cv_ptr->toImageMsg());
     frame.release();
  }


  return 0;
}

Related

Wiki: Interfacing with Systems

MongoDB Logo MongoDB