calibrator.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002  *
00003  * $Id: file.cpp 3496 2011-11-22 15:14:32Z fnovotny $
00004  *
00005  * This file is part of the ViSP software.
00006  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
00007  * 
00008  * This software is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * ("GPL") version 2 as published by the Free Software Foundation.
00011  * See the file LICENSE.txt at the root directory of this source
00012  * distribution for additional information about the GNU GPL.
00013  *
00014  * For using ViSP with software that can not be combined with the GNU
00015  * GPL, please contact INRIA about acquiring a ViSP Professional 
00016  * Edition License.
00017  *
00018  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
00019  * 
00020  * This software was developed at:
00021  * INRIA Rennes - Bretagne Atlantique
00022  * Campus Universitaire de Beaulieu
00023  * 35042 Rennes Cedex
00024  * France
00025  * http://www.irisa.fr/lagadic
00026  *
00027  * If you have questions regarding the use of this file, please contact
00028  * INRIA at visp@inria.fr
00029  * 
00030  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00031  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00032  *
00033  * Contact visp@irisa.fr if any conditions of this licensing are
00034  * not clear to you.
00035  *
00036  * Description:
00037  * 
00038  *
00039  * Authors:
00040  * Filip Novotny
00041  * 
00042  *
00043  *****************************************************************************/
00044 
00050 #include "calibrator.h"
00051 #include "names.h"
00052 #include <visp_bridge/image.h>
00053 #include <visp_bridge/camera.h>
00054 #include "sensor_msgs/SetCameraInfo.h"
00055 #include "sensor_msgs/CameraInfo.h"
00056 #include "visp_camera_calibration/CalibPoint.h"
00057 
00058 #include <vector>
00059 
00060 namespace visp_camera_calibration
00061 {
00062   Calibrator::Calibrator() :
00063     queue_size_(1000)
00064   {
00065     //prepare function objects
00066     point_correspondence_subscriber_callback_t point_correspondence_callback = boost::bind(&Calibrator::pointCorrespondenceCallback, this, _1);
00067     calibrate_service_callback_t calibrate_callback = boost::bind(&Calibrator::calibrateCallback, this, _1, _2);
00068     //define subscribers
00069     point_correspondence_subscriber_ = n_.subscribe(visp_camera_calibration::point_correspondence_topic, queue_size_,
00070                                                     point_correspondence_callback);
00071 
00072     //define services
00073     calibrate_service_ = n_.advertiseService(visp_camera_calibration::calibrate_service,calibrate_callback);
00074 
00075     //connect to services
00076     set_camera_info_service_ = n_.serviceClient<sensor_msgs::SetCameraInfo> (visp_camera_calibration::set_camera_info_service);
00077     set_camera_info_bis_service_ = n_.serviceClient<sensor_msgs::SetCameraInfo> (visp_camera_calibration::set_camera_info_bis_service);
00078 
00079   }
00080 
00081   void Calibrator::pointCorrespondenceCallback(const visp_camera_calibration::CalibPointArray::ConstPtr& point_correspondence){
00082     vpCalibration calib_all_points;
00083     calib_all_points.clearPoint();
00084 
00085     for(std::vector<visp_camera_calibration::CalibPoint>::const_iterator i = point_correspondence->points.begin();
00086         i != point_correspondence->points.end();
00087         i++
00088     ){
00089       vpImagePoint ip(i->i,i->j);
00090       calib_all_points.addPoint(i->X,i->Y,i->Z,ip);
00091     }
00092     calibrations_.push_back(calib_all_points);
00093 
00094   }
00095 
00096   bool Calibrator::calibrateCallback(visp_camera_calibration::calibrate::Request  &req, visp_camera_calibration::calibrate::Response &res){
00097     std::vector<double> dev;
00098     std::vector<double> dev_dist;
00099     double lambda = .5;
00100     ROS_INFO("called service calibrate");
00101     vpCameraParameters cam;
00102 
00103     double px = cam.get_px();
00104     double py = cam.get_px();
00105     double u0 = req.sample_width/2;
00106     double v0 = req.sample_height/2;
00107     double error = 0.;
00108 
00109     cam.initPersProjWithoutDistortion(px, py, u0, v0);
00110     vpCalibration::setLambda(lambda);
00111 
00112     vpCalibration::computeCalibrationMulti(vpCalibration::vpCalibrationMethodType(req.method), calibrations_, cam, error, false);
00113 
00114     for(std::vector<vpCalibration>::iterator i=calibrations_.begin();
00115         i!=calibrations_.end();
00116         i++
00117     ){
00118       double deviation;
00119       double deviation_dist;
00120       i->cam = cam;
00121       i->cam_dist = cam;
00122       i->computeStdDeviation(deviation,deviation_dist);
00123       dev.push_back(deviation);
00124       dev_dist.push_back(deviation_dist);
00125     }
00126     switch(req.method){
00127       case vpCalibration::CALIB_LAGRANGE_VIRTUAL_VS:
00128       case vpCalibration::CALIB_VIRTUAL_VS:
00129         res.stdDevErrs = dev;
00130         break;
00131       case vpCalibration::CALIB_LAGRANGE_VIRTUAL_VS_DIST:
00132       case vpCalibration::CALIB_VIRTUAL_VS_DIST:
00133         res.stdDevErrs= dev_dist;
00134         break;
00135     }
00136 
00137 
00138     ROS_INFO_STREAM("" << cam);
00139     sensor_msgs::SetCameraInfo set_camera_info_comm;
00140 
00141     set_camera_info_comm.request.camera_info = visp_bridge::toSensorMsgsCameraInfo(cam,req.sample_width,req.sample_height);
00142 
00143     set_camera_info_service_.call(set_camera_info_comm);
00144     if(set_camera_info_bis_service_.call(set_camera_info_comm)){
00145       ROS_INFO("set_camera_info service called successfully");
00146     }else{
00147       ROS_ERROR("Failed to call service set_camera_info");
00148     }
00149     return true;
00150   }
00151   void Calibrator::spin(){
00152     ros::spin();
00153   }
00154 
00155   Calibrator::~Calibrator()
00156   {
00157     // TODO Auto-generated destructor stub
00158   }
00159 }


visp_camera_calibration
Author(s): Filip Novotny
autogenerated on Sun Feb 19 2017 03:28:38