Camera.h
Go to the documentation of this file.
00001 /*
00002  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
00003  *
00004  * Copyright 2007-2012 VTT Technical Research Centre of Finland
00005  *
00006  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
00007  *          <http://www.vtt.fi/multimedia/alvar.html>
00008  *
00009  * ALVAR is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU Lesser General Public License as published by the Free
00011  * Software Foundation; either version 2.1 of the License, or (at your option)
00012  * any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but WITHOUT
00015  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00017  * for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with ALVAR; if not, see
00021  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
00022  */
00023 
00024 #ifndef CAMERA_H
00025 #define CAMERA_H
00026 
00034 #include "Alvar.h"
00035 #include "Pose.h"
00036 #include "Util.h"
00037 #include "FileFormat.h"
00038 #include <vector>
00039 
00040 #include <ros/ros.h>
00041 #include <ros/package.h>
00042 #include <ros/console.h>
00043 #include <geometry_msgs/TransformStamped.h>
00044 #include <image_transport/image_transport.h>
00045 #include <sensor_msgs/CameraInfo.h>
00046 #include <visualization_msgs/Marker.h>
00047 #include <resource_retriever/retriever.h>
00048 
00049 namespace alvar {
00050 
00052 struct ALVAR_EXPORT ProjPoints {
00053         int width;
00054         int height;
00055 
00057         std::vector<CvPoint3D64f> object_points;
00064         std::vector<CvPoint2D64f> image_points;
00066         std::vector<int> point_counts;
00067 
00069         void Reset();
00070 
00072         bool AddPointsUsingChessboard(IplImage *image, double etalon_square_size, int etalon_rows, int etalon_columns, bool visualize);
00073 
00075         bool AddPointsUsingMarkers(std::vector<PointDouble> &marker_corners, std::vector<PointDouble> &marker_corners_img, IplImage* image);
00076 };
00077 
00078 
00082 class ALVAR_EXPORT Camera {
00083 
00084 public:
00085 
00086         CvMat calib_K; double calib_K_data[3][3];
00087         CvMat calib_D; double calib_D_data[4];
00088         int calib_x_res;
00089         int calib_y_res;
00090         int x_res;
00091         int y_res;
00092         bool getCamInfo_;
00093 
00094 protected:
00095         std::string cameraInfoTopic_;
00096         sensor_msgs::CameraInfo cam_info_;
00097         void camInfoCallback (const sensor_msgs::CameraInfoConstPtr &);
00098         ros::Subscriber sub_;
00099         ros::NodeHandle n_;
00100 
00101 private:
00102         bool LoadCalibXML(const char *calibfile);
00103         bool LoadCalibOpenCV(const char *calibfile);
00104         bool SaveCalibXML(const char *calibfile);
00105         bool SaveCalibOpenCV(const char *calibfile);
00106 
00107 public:
00109         std::string SerializeId() { return "camera"; };
00136         bool Serialize(Serialization *ser) {
00137                 if (!ser->Serialize(calib_x_res, "width")) return false;
00138                 if (!ser->Serialize(calib_y_res, "height")) return false;
00139                 if (!ser->Serialize(calib_K, "intrinsic_matrix")) return false;
00140                 if (!ser->Serialize(calib_D, "distortion")) return false;
00141                 return true;
00142         }
00143 
00145         Camera();
00146         Camera(ros::NodeHandle & n, std::string cam_info_topic);
00147 
00149         void SetCameraInfo(const sensor_msgs::CameraInfo& camInfo);
00150 
00152         double GetFovX() {
00153                 return (2.0f * atan2(double(x_res) / 2.0f, (double)calib_K_data[0][0]));        
00154         }
00156         double GetFovY() {
00157                 return (2.0f * atan2(double(y_res) / 2.0f, (double)calib_K_data[1][1]));
00158         }
00159         
00160         void SetSimpleCalib(int _x_res, int _y_res, double f_fac=1.);
00161 
00168         bool SetCalib(const char *calibfile, int _x_res, int _y_res, 
00169                       FILE_FORMAT format = FILE_FORMAT_DEFAULT);
00170 
00175         bool SaveCalib(const char *calibfile, FILE_FORMAT format = FILE_FORMAT_DEFAULT);
00176 
00178         void Calibrate(ProjPoints &pp);
00179 
00181         void SetRes(int _x_res, int _y_res);
00182 
00202         void GetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height, const float far_clip = 1000.0f, const float near_clip = 0.1f);
00203 
00205         void SetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height);
00206 
00208         void Undistort(std::vector<PointDouble >& points);
00209 
00211         void Undistort(PointDouble &point);
00212 
00214         void Undistort(CvPoint2D32f& point);
00215 
00217         void Distort(CvPoint2D32f& point);
00218 
00220         void Distort(std::vector<PointDouble>& points);
00221 
00223         void Distort(PointDouble &point);
00224 
00226         void CalcExteriorOrientation(std::vector<CvPoint3D64f>& pw, std::vector<CvPoint2D64f>& pi, Pose *pose);
00227 
00230         void CalcExteriorOrientation(std::vector<CvPoint3D64f>& pw, std::vector<PointDouble >& pi,
00231                                                 CvMat *rodriques, CvMat *tra);
00232 
00235         void CalcExteriorOrientation(std::vector<PointDouble >& pw, std::vector<PointDouble >& pi,
00236                                                 CvMat *rodriques, CvMat *tra);
00237 
00240         void CalcExteriorOrientation(std::vector<PointDouble>& pw, std::vector<PointDouble >& pi, Pose *pose);
00241 
00243         bool CalcExteriorOrientation(const CvMat* object_points, CvMat* image_points, Pose *pose);
00244 
00246         bool CalcExteriorOrientation(const CvMat* object_points, CvMat* image_points, CvMat *rodriques, CvMat *tra);
00247 
00249         void ProjectPoint(const CvPoint3D64f pw, const Pose *pose, CvPoint2D64f &pi) const;
00250 
00252         void ProjectPoint(const CvPoint3D32f pw, const Pose *pose, CvPoint2D32f &pi) const;
00253 
00255         void ProjectPoints(std::vector<CvPoint3D64f>& pw, Pose *pose, std::vector<CvPoint2D64f>& pi) const;
00256 
00259         void ProjectPoints(const CvMat* object_points, const CvMat* rotation_vector,
00260                        const CvMat* translation_vector, CvMat* image_points) const;
00261         
00264         void ProjectPoints(const CvMat* object_points, double gl[16], CvMat* image_points) const;
00265 
00267         void ProjectPoints(const CvMat* object_points, const Pose* pose, CvMat* image_points) const;
00268 };
00269 
00273 struct ALVAR_EXPORT Homography {
00274         double H_data[3][3];
00275         CvMat H;
00276         
00278         Homography();
00279         
00281         void Find(const std::vector<PointDouble>& pw, const std::vector<PointDouble>& pi);
00282         
00284         void ProjectPoints(const std::vector<PointDouble>& from, std::vector<PointDouble>& to);
00285 };
00286 
00287 } // namespace alvar
00288 
00289 #endif


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Feb 16 2017 03:23:02