initial camera implementation (camera2), fix bug (zeros method)
authorozantonkal <ozantonkal@gmail.com>
Fri, 2 Aug 2013 14:33:30 +0000 (16:33 +0200)
committerozantonkal <ozantonkal@gmail.com>
Sat, 3 Aug 2013 13:24:15 +0000 (15:24 +0200)
modules/viz/include/opencv2/viz/types.hpp
modules/viz/src/types.cpp

index b0e3efda3d7f51d23d21d0c48b662238f63fb7ad..4ef05a68a2955cddb6fae59196aa9cb7d16fdb38 100644 (file)
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <string>
+#include <boost/concept_check.hpp>
 #include <opencv2/core.hpp>
 #include <opencv2/core/affine.hpp>
 
@@ -93,6 +94,30 @@ namespace cv
             Point pointer;
             unsigned int key_state;
         };
+        
+        class CV_EXPORTS Camera2
+        {
+        public:
+            Camera2(float f_x, float f_y, float c_x, float c_y, const Size &window_size);
+            Camera2(const Vec2f &fov, const Size &window_size);
+            Camera2(const cv::Mat &K, const Size &window_size);
+            
+            inline const Vec2d & getClip() const { return clip_; }
+            inline void setClip(const Vec2d &clip) { clip_ = clip; }
+            
+            inline const Size & getWindowSize() const { return window_size_; }
+            void setWindowSize(const Size &window_size);
+            
+            inline const Vec2f & getFov() const { return fov_; }
+            inline void setFov(const Vec2f & fov) { fov_ = fov; }
+            
+        private:
+            Vec2d clip_;
+            Vec2f fov_;
+            Size window_size_;
+            Vec2f principal_point_;
+            Vec2f focal_;
+        };
 
     } /* namespace viz */
 } /* namespace cv */
index 871c2dbcb6e8ef18671b5f741b2ccd9bd6b4405b..4ea1b32d37a491f7dc3a5f4a11d4b58a058b6157 100644 (file)
@@ -142,3 +142,57 @@ cv::viz::Mesh3d cv::viz::Mesh3d::loadMesh(const String& file)
 {
     return loadMeshImpl::loadMesh(file);
 }
+
+////////////////////////////////////////////////////////////////////
+/// Camera implementation
+
+cv::viz::Camera2::Camera2(float f_x, float f_y, float c_x, float c_y, const Size &window_size)
+{
+    CV_Assert(window_size.width > 0 && window_size.height > 0);
+    setClip(Vec2d(0.01, 1000.01));// Default clipping
+    
+    fov_[0] = (atan2(c_x,f_x) + atan2(window_size.width-c_x,f_x)) * 180 / CV_PI;
+    fov_[1] = (atan2(c_y,f_y) + atan2(window_size.height-c_y,f_y)) * 180 / CV_PI;
+    
+    principal_point_[0] = c_x;
+    principal_point_[1] = c_y;
+    
+    focal_[0] = f_x;
+    focal_[1] = f_y;
+}
+
+cv::viz::Camera2::Camera2(const Vec2f &fov, const Size &window_size)
+{
+    CV_Assert(window_size.width > 0 && window_size.height > 0);
+    setClip(Vec2d(0.01, 1000.01)); // Default clipping
+    window_size_ = window_size;
+    fov_ = fov;
+    principal_point_ = Vec2f(-1.0f, -1.0f); // Symmetric lens
+    focal_ = Vec2f(-1.0f, -1.0f);
+}
+
+cv::viz::Camera2::Camera2(const cv::Mat & K, const Size &window_size)
+{
+    CV_Assert(K.rows == 3 && K.cols == 3);
+    CV_Assert(window_size.width > 0 && window_size.height > 0);
+    
+    float f_x = K.at<float>(0,0);
+    float f_y = K.at<float>(1,1);
+    float c_x = K.at<float>(0,2);
+    float c_y = K.at<float>(1,2);
+    Camera2(f_x, f_y, c_x, c_y, window_size);
+}
+
+void cv::viz::Camera2::setWindowSize(const Size &window_size)
+{
+    CV_Assert(window_size.width > 0 && window_size.height > 0);
+    
+    // Vertical field of view is fixed! 
+    // Horizontal field of view is expandable based on the aspect ratio
+    float aspect_ratio_new = window_size.width / window_size.height;
+    
+    if (principal_point_[0] < 0.0f)
+        fov_[0] = 2 * atan2(tan(fov_[0] * 0.5), aspect_ratio_new); // This assumes that the lens is symmetric!
+    else
+        fov_[0] = (atan2(principal_point_[0],focal_[0]) + atan2(window_size.width-principal_point_[0],focal_[0])) * 180 / CV_PI;
+}