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 #include <v4r_opencv_cam/opencv_cam_defaults.h>
00023 #include <v4r_opencv_cam/opencv_cam_node.h>
00024 #include <camera_info_manager/camera_info_manager.h>
00025
00026
00027
00028 int main ( int argc, char **argv ) {
00029 ros::init ( argc, argv, "opencv_cam" );
00030 ros::NodeHandle n;
00031 OpenCvCam cam ( n );
00032 ros::Rate rate ( cam.frequency() );
00033 cam.init();
00034 while ( ros::ok() ) {
00035 cam.capture();
00036 cam.publishCamera();
00037 cam.show();
00038 rate.sleep();
00039 }
00040 return 0;
00041 }
00042
00043 void OpenCvCam::CvCaptureInfo::getCameraParam(cv::VideoCapture &videoCapture) {
00044
00045 if(cap_prop_frame_width_support_) {
00046 cap_prop_frame_width_ = videoCapture.get ( CV_CAP_PROP_FRAME_WIDTH);
00047 if(cap_prop_frame_width_ == -1) {
00048 cap_prop_frame_width_support_ = false;
00049 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_FRAME_WIDTH not supported");
00050 }
00051 }
00052 if(cap_prop_frame_height_support_) {
00053 cap_prop_frame_height_ = videoCapture.get ( CV_CAP_PROP_FRAME_HEIGHT);
00054 if(cap_prop_frame_height_ == -1) {
00055 cap_prop_frame_height_support_ = false;
00056 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_FRAME_WIDTH not supported");
00057 }
00058 }
00059 if(cap_prop_fps_support_) {
00060 cap_prop_fps_ = videoCapture.get ( CV_CAP_PROP_FPS);
00061 if(cap_prop_fps_ == -1) {
00062 cap_prop_fps_support_ = false;
00063 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_FPS not supported");
00064 }
00065 }
00066 }
00067
00068 void OpenCvCam::CvCaptureInfo::update(cv::VideoCapture &videoCapture, cv::Mat &img) {
00069 if ( videoCapture.isOpened() ) {
00070 videoCapture >> img;
00071 if(cap_prop_pos_msec_support_) {
00072 cap_prop_pos_msec_ = videoCapture.get ( CV_CAP_PROP_POS_MSEC);
00073 if(cap_prop_pos_msec_ == -1) {
00074 cap_prop_pos_msec_support_ = false;
00075 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_POS_MSEC not supported");
00076 }
00077 }
00078 if(cap_prop_pos_frames_support_) {
00079 cap_prop_pos_frames_ = videoCapture.get (CV_CAP_PROP_POS_FRAMES);
00080 if(cap_prop_pos_frames_ == -1) cap_prop_pos_frames_support_ = false;
00081 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_POS_FRAMES not supported");
00082 }
00083 if(cap_prop_frame_count_support_) {
00084 cap_prop_frame_count_ = videoCapture.get ( CV_CAP_PROP_FRAME_COUNT);
00085 if(cap_prop_frame_count_ == -1) cap_prop_frame_count_support_ = false;
00086 ROS_WARN ( "OpenCvCam: CV_CAP_PROP_FRAME_COUNT not supported");
00087 }
00088 }
00089 }
00090
00091 unsigned int OpenCvCam::CvCaptureInfo::getTimeStamp(int min, int sec, int ms) {
00092 if(cap_prop_pos_msec_support_){
00093 double millis = cap_prop_pos_msec_;
00094 min = (int)(millis/1000/60);
00095 millis -= min*60000;
00096 sec = (int)(millis/1000);
00097 ms -= sec*1000;
00098 }
00099 return (unsigned int ) cap_prop_frame_count_;
00100 }
00101
00102 OpenCvCam::OpenCvCam ( ros::NodeHandle & n ) :
00103 n_ ( n ), n_param_ ( "~" ), imageTransport_(n_param_) {
00104
00105 readParam();
00106
00107 cameraPublisher_ = imageTransport_.advertiseCamera("image_raw", 1);
00108
00109 }
00110
00111 void OpenCvCam::readParam()
00112 {
00113 n_param_.param<double> ( "frequency", frequency_, DEFAULT_FREQUENCY);
00114 ROS_INFO ( "OpenCvCam: frequency: %5.2f", frequency_ );
00115
00116 n_param_.param<bool> ( "show_camera_image", show_camera_image_, DEFAULT_SHOW_CAMERA_IMAGE );
00117 ROS_INFO ( "OpenCvCam: debug: %s", ((show_camera_image_) ? "true" : "false"));
00118
00119 n_param_.param<int>("video_device", video_device_, DEFAULT_VIDEODEVICE);
00120 ROS_INFO("OpenCvCam: video_device: %i", video_device_);
00121
00122 std::string camera_info_url;
00123 cameraInfo_.width = DEFAULT_FRAME_WIDTH;
00124 cameraInfo_.height = DEFAULT_FRAME_HEIGHT;
00125 if(n_param_.getParam("camera_info_url", camera_info_url)) {
00126 camera_info_manager::CameraInfoManager cinfo(n_param_);
00127 if(cinfo.validateURL(camera_info_url)) {
00128 cinfo.loadCameraInfo(camera_info_url);
00129 cameraInfo_ = cinfo.getCameraInfo();
00130 } else {
00131 ROS_FATAL("camera_info_url not valid.");
00132 n_param_.shutdown();
00133 return;
00134 }
00135 } else {
00136 XmlRpc::XmlRpcValue double_list;
00137 n_param_.getParam("K", double_list);
00138 if((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) && (double_list.size() == 9)) {
00139 for(int i = 0; i < 9; i++) {
00140 ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00141 cameraInfo_.K[i] = double_list[i];
00142 }
00143 }
00144
00145 n_param_.getParam("D", double_list);
00146 if((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) && (double_list.size() == 5)) {
00147 for(int i = 0; i < 5; i++) {
00148 ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00149 cameraInfo_.D[i] = double_list[i];
00150 }
00151 }
00152
00153 ROS_INFO("OpenCvCam: tf_camera_id: %s", cameraInfo_.header.frame_id.c_str());
00154 n_param_.getParam("R", double_list);
00155 if((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) && (double_list.size() == 9)) {
00156 for(int i = 0; i < 9; i++) {
00157 ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00158 cameraInfo_.R[i] = double_list[i];
00159 }
00160 }
00161
00162 n_param_.getParam("P", double_list);
00163 if((double_list.getType() == XmlRpc::XmlRpcValue::TypeArray) && (double_list.size() == 12)) {
00164 for(int i = 0; i < 12; i++) {
00165 ROS_ASSERT(double_list[0].getType() == XmlRpc::XmlRpcValue::TypeDouble);
00166 cameraInfo_.P[i] = double_list[i];
00167 }
00168 }
00169 }
00170 n_param_.param<std::string>("frame_id", cameraInfo_.header.frame_id, DEFAULT_FRAME_ID);
00171 ROS_INFO("OpenCvCam: frame_id: %s", cameraInfo_.header.frame_id.c_str());
00172 }
00173
00174 void OpenCvCam::init() {
00175 cap_.open ( video_device_ );
00176 if ( !cap_.isOpened() ) {
00177 ROS_ERROR ( "OpenCvCam: Could not initialize capturing device %i", video_device_);
00178 }
00179
00180 cap_.set ( CV_CAP_PROP_FRAME_WIDTH, cameraInfo_.width );
00181 cap_.set ( CV_CAP_PROP_FRAME_HEIGHT, cameraInfo_.height );
00182 captureInfo_.getCameraParam(cap_);
00183 ROS_INFO ( "OpenCvCam: frame size %i x %i @ %3.1f Hz",
00184 (int) captureInfo_.cap_prop_frame_width_ ,
00185 (int) captureInfo_.cap_prop_frame_height_ ,
00186 captureInfo_.cap_prop_fps_ );
00187
00188 if ( cap_.isOpened() ) {
00189 cap_ >> img_;
00190 }
00191 cameraInfo_.width = img_.cols;
00192 cameraInfo_.height = img_.rows;
00193 if(show_camera_image_) {
00194 cv::namedWindow ( DEBUG_WINDOWS_NAME,1 );
00195 }
00196 }
00197
00198 void OpenCvCam::capture() {
00199 captureInfo_.update(cap_, img_);
00200 int min, sec, ms;
00201 unsigned int counter = captureInfo_.getTimeStamp(min, sec, ms);
00202
00203 }
00204
00205 void OpenCvCam::show() {
00206 if(!show_camera_image_) return;
00207 cv::imshow ( DEBUG_WINDOWS_NAME, img_ );
00208 cv::waitKey(5);
00209 }
00210
00211 void OpenCvCam::publishCamera()
00212 {
00213 cameraInfo_.header.stamp = ros::Time::now();
00214 cameraImage_.header = cameraInfo_.header;
00215 cameraImage_.height = cameraInfo_.height = img_.rows;
00216 cameraImage_.width = cameraInfo_.width = img_.cols;
00217 cameraImage_.is_bigendian = true;
00218 cameraImage_.step = cameraInfo_.width * 3;
00219 cameraImage_.encoding = "bgr8";
00220 cameraImage_.data.resize(cameraImage_.height * cameraImage_.width * 3);
00221 cameraImage_.step = cameraInfo_.width * 3;
00222 memcpy(&cameraImage_.data[0], img_.data, cameraImage_.data.size());
00223 cameraPublisher_.publish(cameraImage_, cameraInfo_);
00224 }
00225 OpenCvCam::~OpenCvCam() {
00226 }