Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLGeom/src/ICLGeom/Camera.h ** 00010 ** Module : ICLGeom ** 00011 ** Authors: Erik Weitnauer, Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/Size.h> 00034 #include <ICLUtils/Point32f.h> 00035 #include <ICLUtils/Rect.h> 00036 #include <ICLUtils/Rect32f.h> 00037 #include <ICLUtils/Exception.h> 00038 #include <ICLUtils/Array2D.h> 00039 #include <ICLGeom/PlaneEquation.h> 00040 #include <ICLGeom/ViewRay.h> 00041 00042 00043 // the icl namespace 00044 namespace icl { 00045 namespace geom{ 00047 00131 class Camera { 00132 public: 00133 00134 struct RenderParams { 00135 utils::Size chipSize; 00136 float clipZNear; 00137 float clipZFar; 00138 utils::Rect viewport; 00139 float viewportZMin, viewportZMax; 00140 00141 RenderParams(const utils::Size &chipSize=utils::Size::VGA, float clipZNear=1.0, float clipZFar=10000., 00142 const utils::Rect &viewport=utils::Rect(0,0,640,480), float viewportZMin=0., 00143 float viewportZMax=1.): 00144 chipSize(chipSize), clipZNear(clipZNear), clipZFar(clipZFar), viewport(viewport), 00145 viewportZMin(viewportZMin), viewportZMax(viewportZMax) {} 00146 }; 00147 00149 struct NotEnoughDataPointsException : public utils::ICLException{ 00150 NotEnoughDataPointsException():utils::ICLException(__FUNCTION__){} 00151 }; 00152 00154 typedef math::FixedMatrix<icl32f,3,3> Mat3x3; 00155 00158 00159 Camera(const Vec &pos=Vec(0,0,10,1), 00160 const Vec &norm=Vec(0,0,-1,1), 00161 const Vec &up=Vec(1,0,0,1), 00162 float f=3, 00163 const utils::Point32f &principalPointOffset=utils::Point32f(320,240), 00164 float sampling_res_x = 200, 00165 float sampling_res_y = 200, 00166 float skew = 0, 00167 const RenderParams &renderParams = RenderParams()): m_pos(pos), m_norm(norm), m_up(up), m_f(f), 00168 m_px(principalPointOffset.x), m_py(principalPointOffset.y), 00169 m_mx(sampling_res_x), m_my(sampling_res_y), m_skew(skew), m_renderParams(renderParams) {} 00170 00172 00176 Camera(const std::string &filename, const std::string &prefix="config.") throw (utils::ParseException); 00178 00182 Camera(std::istream &configDataStream, const std::string &prefix="config.") throw (utils::ParseException); 00183 00184 00186 00187 static Camera createFromProjectionMatrix(const math::FixedMatrix<icl32f,4,3> &Q, float focalLength=1); 00188 00190 00198 static Camera calibrate(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1) 00199 throw (NotEnoughDataPointsException,math::SingularMatrixException); 00200 00202 00204 static Camera calibrate_pinv(std::vector<Vec> Xws, std::vector<utils::Point32f> xis, float focalLength=1) 00205 throw (NotEnoughDataPointsException,math::SingularMatrixException); 00206 00209 // projections normal 00211 utils::Point32f project(const Vec &Xw) const; 00213 void project(const std::vector<Vec> &Xws, std::vector<utils::Point32f> &dst) const; 00215 const std::vector<utils::Point32f> project(const std::vector<Vec> &Xws) const; 00216 00217 00218 00219 // projections OpenGL 00221 Vec projectGL(const Vec &Xw) const; 00223 void projectGL(const std::vector<Vec> &Xws, std::vector<Vec> &dst) const; 00225 const std::vector<Vec> projectGL(const std::vector<Vec> &Xws) const; 00226 00227 00228 // projection magic 00230 ViewRay getViewRay(const utils::Point32f &pixel) const; 00231 00233 00235 std::vector<ViewRay> getViewRays(const std::vector<utils::Point32f> &pixels) const; 00236 00238 00240 utils::Array2D<ViewRay> getAllViewRays() const; 00241 00243 ViewRay getViewRay(const Vec &Xw) const; 00245 Vec estimate3DPosition(const utils::Point32f &pixel, const PlaneEquation &plane) const throw (utils::ICLException); 00247 00256 static Vec getIntersection(const ViewRay &v, 00257 const PlaneEquation &plane) throw (utils::ICLException); 00258 00261 // setters and getters 00263 void setRotation(const Mat3x3 &rot); 00265 void setRotation(const Vec &rot); 00266 00268 void setTransformation(const Mat &m); 00269 00271 Mat getCSTransformationMatrix() const; 00273 Mat getCSTransformationMatrixGL() const; 00275 Mat getProjectionMatrix() const; 00277 Mat getProjectionMatrixGL() const; 00278 Mat getViewportMatrixGL() const; 00279 00281 math::FixedMatrix<icl32f,4,3> getQMatrix() const; 00282 00284 math::FixedMatrix<icl32f,3,4> getInvQMatrix() const; 00285 00287 inline void translate(const Vec &d) { m_pos += d; } 00288 00289 std::string toString() const; 00290 00293 const std::string &getName() const { return m_name; } 00294 const Vec &getPosition() const { return m_pos; } 00295 const Vec &getNorm() const { return m_norm; } 00296 const Vec &getUp() const { return m_up; } 00297 Vec getHoriz() const { return cross(m_up, m_norm); } 00298 float getFocalLength() const { return m_f; } 00299 utils::Point32f getPrincipalPointOffset() const { return utils::Point32f(m_px, m_py); } 00300 float getPrincipalPointOffsetX() const { return m_px; } 00301 float getPrincipalPointOffsetY() const { return m_py; } 00302 float getSamplingResolutionX() const { return m_mx; } 00303 float getSamplingResolutionY() const { return m_my; } 00304 float getSkew() const { return m_skew; } 00305 const RenderParams &getRenderParams() const { return m_renderParams; } 00306 RenderParams &getRenderParams() { return m_renderParams; } 00307 00310 void setName(const std::string &name) { m_name = name; } 00311 void setPosition(const Vec &pos) { m_pos = pos; } 00312 void setNorm(const Vec &norm) { m_norm = norm; m_norm[3] = 0; m_norm.normalize(); m_norm[3] = 1; } 00313 void setUp(const Vec &up) { m_up = up; m_up[3] = 0; m_up.normalize(); m_up[3] = 1; } 00314 void setFocalLength(float value) { m_f = value; } 00315 void setPrincipalPointOffset(float px, float py) { m_px = px; m_py = py; } 00316 void setPrincipalPointOffset(const utils::Point32f &p) { m_px = p.x; m_py = p.y; } 00317 void setSamplingResolutionX(float value) { m_mx = value; } 00318 void setSamplingResolutionY(float value) { m_my = value; } 00319 void setSamplingResolution(float x, float y) { m_mx = x; m_my = y; } 00320 void setSkew(float value) { m_skew = value; } 00321 void setRenderParams(const RenderParams &rp) { m_renderParams = rp; } 00322 00323 00326 00327 00331 void setResolution(const utils::Size &newScreenSize); 00332 00334 00337 void setResolution(const utils::Size &newScreenSize, const utils::Point &newPrincipalPointOffset); 00338 00340 inline const utils::Size &getResolution() const { return m_renderParams.chipSize; } 00343 00344 00422 static Vec estimate_3D(const std::vector<Camera*> cams, 00423 const std::vector<utils::Point32f> &UVs, 00424 bool removeInvalidPoints=false) throw (utils::ICLException); 00425 00427 00429 static Vec estimate_3D_svd(const std::vector<Camera*> cams, 00430 const std::vector<utils::Point32f> &UVs); 00431 00434 protected: 00435 static Mat createTransformationMatrix(const Vec &norm, const Vec &up, const Vec &pos); 00436 00437 private: 00438 // General Parameters 00439 std::string m_name; 00440 00441 // External Parameters 00442 Vec m_pos; 00443 Vec m_norm; 00444 Vec m_up; 00445 00446 // Internal Parameters 00447 float m_f; 00448 float m_px, m_py; 00449 float m_mx, m_my; 00450 float m_skew; 00451 00452 // Rendering Parameters 00453 RenderParams m_renderParams; 00454 00457 00459 static void checkAndFixPoints(std::vector<Vec> &worldPoints, std::vector<utils::Point32f> &imagePoints) 00460 throw (NotEnoughDataPointsException); 00462 static void load_camera_from_stream(std::istream &is, 00463 const std::string &prefix, 00464 Camera &cam); 00465 }; 00466 00468 std::ostream &operator<<(std::ostream &os, const Camera &cam); 00469 00471 std::istream &operator>>(std::istream &is, Camera &cam) throw (utils::ParseException); 00472 00473 } // namespace geom 00474 } // namespace icl 00475