Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <ros/ros.h>
00038 #include <image_transport/image_transport.h>
00039 #include <camera_info_manager/camera_info_manager.h>
00040 #include <opencv2/highgui/highgui.hpp>
00041 #include <opencv2/imgproc/imgproc.hpp>
00042 #include <cv_bridge/cv_bridge.h>
00043 #include <sstream>
00044 #include <boost/assign/list_of.hpp>
00045
00046
00047
00048 sensor_msgs::CameraInfo get_default_camera_info_from_image(sensor_msgs::ImagePtr img){
00049 sensor_msgs::CameraInfo cam_info_msg;
00050 cam_info_msg.header.frame_id = img->header.frame_id;
00051
00052 cam_info_msg.height = img->height;
00053 cam_info_msg.width = img->width;
00054 ROS_INFO_STREAM("The image width is: " << img->width);
00055 ROS_INFO_STREAM("The image height is: " << img->height);
00056
00057 cam_info_msg.distortion_model = "plumb_bob";
00058
00059 cam_info_msg.D.resize(5, 0.0);
00060
00061 cam_info_msg.K = boost::assign::list_of(1.0) (0.0) (img->width/2.0)
00062 (0.0) (1.0) (img->height/2.0)
00063 (0.0) (0.0) (1.0);
00064
00065 cam_info_msg.R = boost::assign::list_of (1.0) (0.0) (0.0)
00066 (0.0) (1.0) (0.0)
00067 (0.0) (0.0) (1.0);
00068
00069 cam_info_msg.P = boost::assign::list_of (1.0) (0.0) (img->width/2.0) (0.0)
00070 (0.0) (1.0) (img->height/2.0) (0.0)
00071 (0.0) (0.0) (1.0) (0.0);
00072 return cam_info_msg;
00073 }
00074
00075
00076
00077 int main(int argc, char** argv)
00078 {
00079 ros::init(argc, argv, "image_publisher");
00080 ros::NodeHandle nh;
00081 ros::NodeHandle _nh("~");
00082 image_transport::ImageTransport it(nh);
00083 image_transport::CameraPublisher pub = it.advertiseCamera("camera", 1);
00084
00085
00086 std::string video_stream_provider;
00087 cv::VideoCapture cap;
00088 if (_nh.getParam("video_stream_provider", video_stream_provider)){
00089 ROS_INFO_STREAM("Resource video_stream_provider: " << video_stream_provider);
00090
00091
00092 if (video_stream_provider.size() < 4){
00093 ROS_INFO_STREAM("Getting video from provider: /dev/video" << video_stream_provider);
00094 cap.open(atoi(video_stream_provider.c_str()));
00095 }
00096 else{
00097 ROS_INFO_STREAM("Getting video from provider: " << video_stream_provider);
00098 cap.open(video_stream_provider);
00099 }
00100 }
00101 else{
00102 ROS_ERROR("Failed to get param 'video_stream_provider'");
00103 return -1;
00104 }
00105
00106 std::string camera_name;
00107 _nh.param("camera_name", camera_name, std::string("camera"));
00108 ROS_INFO_STREAM("Camera name: " << camera_name);
00109
00110 int fps;
00111 _nh.param("fps", fps, 240);
00112 ROS_INFO_STREAM("Throttling to fps: " << fps);
00113
00114 std::string frame_id;
00115 _nh.param("frame_id", frame_id, std::string("camera"));
00116 ROS_INFO_STREAM("Publishing with frame_id: " << frame_id);
00117
00118 std::string camera_info_url;
00119 _nh.param("camera_info_url", camera_info_url, std::string(""));
00120 ROS_INFO_STREAM("Provided camera_info_url: '" << camera_info_url << "'");
00121
00122 bool flip_horizontal;
00123 _nh.param("flip_horizontal", flip_horizontal, false);
00124 ROS_INFO_STREAM("Flip horizontal image is: " << ((flip_horizontal)?"true":"false"));
00125
00126 bool flip_vertical;
00127 _nh.param("flip_vertical", flip_vertical, false);
00128 ROS_INFO_STREAM("Flip vertical image is: " << ((flip_vertical)?"true":"false"));
00129
00130 int width_target;
00131 int height_target;
00132 _nh.param("width", width_target, 0);
00133 _nh.param("height", height_target, 0);
00134 if (width_target != 0 && height_target != 0){
00135 ROS_INFO_STREAM("Forced image width is: " << width_target);
00136 ROS_INFO_STREAM("Forced image height is: " << height_target);
00137 }
00138
00139
00140
00141 bool flip_image = true;
00142 int flip_value;
00143 if (flip_horizontal && flip_vertical)
00144 flip_value = 0;
00145 else if (flip_horizontal)
00146 flip_value = 1;
00147 else if (flip_vertical)
00148 flip_value = -1;
00149 else
00150 flip_image = false;
00151
00152 if(!cap.isOpened()){
00153 ROS_ERROR_STREAM("Could not open the stream.");
00154 return -1;
00155 }
00156 if (width_target != 0 && height_target != 0){
00157 cap.set(CV_CAP_PROP_FRAME_WIDTH, width_target);
00158 cap.set(CV_CAP_PROP_FRAME_HEIGHT, height_target);
00159 }
00160
00161
00162 ROS_INFO_STREAM("Opened the stream, starting to publish.");
00163
00164 cv::Mat frame;
00165 sensor_msgs::ImagePtr msg;
00166 sensor_msgs::CameraInfo cam_info_msg;
00167 std_msgs::Header header;
00168 header.frame_id = frame_id;
00169 camera_info_manager::CameraInfoManager cam_info_manager(nh, camera_name, camera_info_url);
00170
00171 cam_info_msg = cam_info_manager.getCameraInfo();
00172
00173 ros::Rate r(fps);
00174 while (nh.ok()) {
00175 cap >> frame;
00176 if (pub.getNumSubscribers() > 0){
00177
00178 if(!frame.empty()) {
00179
00180 if (flip_image)
00181 cv::flip(frame, frame, flip_value);
00182 msg = cv_bridge::CvImage(header, "bgr8", frame).toImageMsg();
00183
00184 if (cam_info_msg.distortion_model == ""){
00185 ROS_WARN_STREAM("No calibration file given, publishing a reasonable default camera info.");
00186 cam_info_msg = get_default_camera_info_from_image(msg);
00187 cam_info_manager.setCameraInfo(cam_info_msg);
00188 }
00189
00190 pub.publish(*msg, cam_info_msg, ros::Time::now());
00191 }
00192
00193 ros::spinOnce();
00194 }
00195 r.sleep();
00196 }
00197 }