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

Generated on Wed Nov 25 12:15:03 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8