cameraIntrinsicsPinhole.cpp

Go to the documentation of this file.
00001 
00016 #include <iomanip>
00017 #include <dlrCommon/inputStream.h>
00018 #include <dlrComputerVision/cameraIntrinsicsPinhole.h>
00019 
00020 using namespace dlr::numeric;
00021 using namespace dlr::geometry;
00022 
00023 namespace dlr {
00024 
00025   namespace computerVision {
00026 
00027 
00028     // The default constructor initializes the CameraIntrinsicsPinhole
00029     // instance to a consistent (but not terribly useful) state.
00030     CameraIntrinsicsPinhole::
00031     CameraIntrinsicsPinhole()
00032       : CameraIntrinsics(),
00033         m_centerU(50),
00034         m_centerV(50),
00035         m_kX(1.0),
00036         m_kY(1.0),
00037         m_numPixelsX(100),
00038         m_numPixelsY(100)
00039     {
00040       // Empty.
00041     }
00042       
00043 
00044     // This constructor allows the caller to explicitly set the
00045     // camera intrinsic parameters.
00046     CameraIntrinsicsPinhole::
00047     CameraIntrinsicsPinhole(size_t numPixelsX,
00048                             size_t numPixelsY,
00049                             double focalLength,
00050                             double pixelSizeX,
00051                             double pixelSizeY,
00052                             double centerU,
00053                             double centerV)
00054       : CameraIntrinsics(),
00055         m_centerU(centerU),
00056         m_centerV(centerV),
00057         m_kX(focalLength / pixelSizeX),
00058         m_kY(focalLength / pixelSizeY),
00059         m_numPixelsX(numPixelsX),
00060         m_numPixelsY(numPixelsY)
00061     {
00062       // Empty.
00063     }
00064     
00065 
00066 
00067     // This member function takes a point in 3D camera coordinates
00068     // and projects it into pixel coordinates.
00069     Vector2D
00070     CameraIntrinsicsPinhole::
00071     project(const dlr::numeric::Vector3D& point) const
00072     {
00073       return Vector2D(m_kX * point.x() / point.z() + m_centerU,
00074                       m_kY * point.y() / point.z() + m_centerV);
00075     }
00076     
00077 
00078     // This member function sets the calibration from an input
00079     // stream.
00080     std::istream&
00081     CameraIntrinsicsPinhole::
00082     readFromStream(std::istream& stream)
00083     {
00084       // If stream is in a bad state, we can't read from it.
00085       if (!stream){
00086         return stream;
00087       }
00088     
00089       // Construct an InputStream instance so we can use our
00090       // convenience functions.
00091       InputStream inputStream(stream, InputStream::SKIP_WHITESPACE);
00092 
00093       double centerU, centerV, kX, kY;
00094       size_t numpixelsX, numpixelsY;
00095       inputStream.expect("CameraIntrinsicsPinhole");
00096       inputStream.expect("{");
00097       stream >> centerU;
00098       inputStream.expect(",");
00099       stream >> centerV;
00100       inputStream.expect(",");
00101       stream >> kX;
00102       inputStream.expect(",");
00103       stream >> kY;
00104       inputStream.expect(",");
00105       stream >> numpixelsX;
00106       inputStream.expect(",");
00107       stream >> numpixelsY;
00108       inputStream.expect("}");
00109 
00110       if(stream) {
00111         m_centerU = centerU;
00112         m_centerV = centerV;
00113         m_kX = kX;
00114         m_kY = kY;
00115         m_numPixelsX = numpixelsX;
00116         m_numPixelsY = numpixelsY;
00117       }
00118       return stream;
00119     }
00120 
00121 
00122     // This member function takes a point in 2D pixel coordinates
00123     // and returns a ray in 3D camera coordinates passing through
00124     // all of the 3D points that project to the specified 2D
00125     // position.
00126     geometry::Ray3D
00127     CameraIntrinsicsPinhole::
00128     reverseProject(const Vector2D& pixelPosition,
00129                    bool normalize) const
00130     {
00131       // For pinhole camera model, assume 3D point [x_cam, y_cam,
00132       // z_cam]^T, projection matrix
00133       //
00134       //   [[k_x, 0, x_c, 0],
00135       //    [0, k_y, y_c, 0],
00136       //    [0,   0,   1, 0]]],
00137       //
00138       // and pixel coordinate [u, v]^T.  We can hold z_cam = 1.0 and
00139       // write:
00140       //
00141       //   x_pix = k_x * x_cam + x_c, and
00142       //   y_pix = k_y * y_cam + x_c.
00143       // 
00144       // This is trivial to invert:
00145       //
00146       //   x_cam = (x_pix - x_c) / k_x, and
00147       //   y_cam = (y_pix - y_c) / k_y.
00148       //   z_cam = 1.0
00149       return Ray3D(Vector3D(0.0, 0.0, 0.0),
00150                    Vector3D((pixelPosition.x() - m_centerU) / m_kX,
00151                             (pixelPosition.y() - m_centerV) / m_kY,
00152                             1.0, normalize));
00153     }
00154 
00155 
00156     // This member function writes the calibration to an
00157     // outputstream in a format which is compatible with member
00158     // function readFromStream().
00159     std::ostream&
00160     CameraIntrinsicsPinhole::
00161     writeToStream(std::ostream& stream) const
00162     {
00163       stream << "CameraIntrinsicsPinhole {"
00164              << std::fixed << std::setw(14)
00165              << m_centerU << ", "
00166              << m_centerV << ", "
00167              << m_kX << ", "
00168              << m_kY << ", "
00169              << m_numPixelsX << ", "
00170              << m_numPixelsY << "}";
00171       return stream;
00172     }
00173 
00174     
00175   } // namespace computerVision
00176   
00177 } // namespace dlr

Generated on Wed Jun 25 14:34:09 2008 for dlrComputerVision Utility Library by  doxygen 1.5.5