Main MRPT website > C++ reference
MRPT logo
utils.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef mrpt_vision_utils_H
00030 #define mrpt_vision_utils_H
00031 
00032 #include <mrpt/vision/CFeature.h>
00033 #include <mrpt/utils/CImage.h>
00034 #include <mrpt/math/utils.h>
00035 #include <mrpt/utils/CLoadableOptions.h>
00036 #include <mrpt/utils/TMatchingPair.h>
00037 #include <mrpt/poses/CPose3D.h>
00038 #include <mrpt/poses/CPoint2D.h>
00039 #include <mrpt/poses/CPoint3D.h>
00040 
00041 #include <mrpt/vision/types.h>
00042 #include <mrpt/vision/chessboard_camera_calib.h>
00043 
00044 #include <mrpt/vision/link_pragmas.h>
00045 
00046 namespace mrpt
00047 {
00048         namespace slam
00049         {
00050                 class CObservationStereoImages;
00051                 class CObservationBearingRange;
00052                 class CLandmarksMap;
00053                 class CObservationVisualLandmarks;
00054         }
00055 
00056         /** Classes for computer vision, detectors, features, etc.  \ingroup mrpt_vision_grp
00057          */
00058         namespace vision
00059         {
00060                 using std::vector;
00061                 using namespace mrpt::slam;
00062                 using namespace mrpt::math;
00063                 using namespace mrpt::utils;
00064 
00065                 /** \addtogroup mrpt_vision_grp
00066                   *  @{ */
00067 
00068                         /**     Computes the correlation between this image and another one, encapsulating the openCV function cvMatchTemplate
00069                         *   This implementation reduced computation time.
00070                         * \param img            [IN]    The imput image. This function supports gray-scale (1 channel only) images.
00071                         * \param patch_img      [IN]    The "patch" image, which must be equal, or smaller than "this" image. This function supports gray-scale (1 channel only) images.
00072                         * \param x_max          [OUT]   The x coordinate where it was found the maximun cross correlation value.
00073                         * \param y_max          [OUT]   The y coordinate where it was found the maximun cross correlation value.
00074                         * \param max_val        [OUT]   The maximun value of cross correlation which we can find
00075                         * \param x_search_ini   [IN]    The "x" coordinate of the search window.
00076                         * \param y_search_ini   [IN]    The "y" coordinate of the search window.
00077                         * \param x_search_size  [IN]    The width of the search window.
00078                         * \param y_search_size  [IN]    The height of the search window.
00079                         *  Note: By default, the search area is the whole (this) image.
00080                         * \sa cross_correlation
00081                         */
00082                         void VISION_IMPEXP openCV_cross_correlation(
00083                                 const CImage            & img,
00084                                                                 const CImage            & patch_img,
00085                                                                 size_t                              & x_max,
00086                                                                 size_t                              & y_max,
00087                                                                 double                              & max_val,
00088                                                                 int                                         x_search_ini=-1,
00089                                                                 int                                         y_search_ini=-1,
00090                                                                 int                                         x_search_size=-1,
00091                                                                 int                                         y_search_size=-1);
00092 
00093                         /**     Invert an image using OpenCV function
00094                         *
00095                         */
00096                         void VISION_IMPEXP flip(
00097                                 CImage                      & img);
00098 
00099                         /** Extract a UNITARY 3D vector in the direction of a 3D point, given from its (x,y) pixels coordinates, and the camera intrinsic coordinates.
00100                           *  \param xy  [IN]   Pixels coordinates, from the top-left corner of the image.
00101                           *  \param A   [IN]   The 3x3 intrinsic parameters matrix for the camera.
00102                           *  \return The TPoint3D containing the output unitary vector.
00103                           * \sa buildIntrinsicParamsMatrix, defaultIntrinsicParamsMatrix, TPixelCoordf
00104                           */
00105                         TPoint3D VISION_IMPEXP pixelTo3D(
00106                                 const TPixelCoordf      & xy,
00107                                 const CMatrixDouble33   & A);
00108 
00109                         /** Builds the intrinsic parameters matrix A from parameters:
00110                           * \param focalLengthX [IN]   The focal length, in X (horizontal) pixels
00111                           * \param focalLengthY [IN]   The focal length, in Y (vertical) pixels
00112                           * \param centerX      [IN]   The image center, horizontal, in pixels
00113                           * \param centerY      [IN]   The image center, vertical, in pixels
00114                           *
00115                           * <br>This method returns the matrix:
00116                           <table>
00117                           <tr><td>f_x</td><td>0</td><td>cX</td> </tr>
00118                           <tr><td>0</td><td>f_y</td><td>cY</td> </tr>
00119                           <tr><td>0</td><td>0</td><td>1</td> </tr>
00120                           </table>
00121                           *  See also the tutorial discussing the <a rhref="http://www.mrpt.org/Camera_Parameters">camera model parameters</a>.
00122                           * \sa defaultIntrinsicParamsMatrix, pixelTo3D
00123                           */
00124                         CMatrixDouble33 VISION_IMPEXP buildIntrinsicParamsMatrix(
00125                                 const double            focalLengthX,
00126                                 const double            focalLengthY,
00127                                 const double            centerX,
00128                                 const double            centerY);
00129 
00130                         /** Returns the stored, default intrinsic params matrix for a given camera:
00131                           * \param camIndex     [IN]   Posible values are listed next.
00132                           * \param resolutionX  [IN]   The number of pixel columns
00133                           * \param resolutionY  [IN]   The number of pixel rows
00134                           *
00135                           * The matrix is generated for the indicated camera resolution configuration.
00136                           * The following table summarizes the current supported cameras and the values as
00137                           *  ratios of the corresponding horz. or vert. resolution:<br>
00138 
00139                           <center><table>
00140                           <tr>
00141                            <td><center><b>camIndex</b></center></td>
00142                            <td><center><b>Manufacturer</b></center></td>
00143                            <td><center><b>Camera model</b></center></td>
00144                            <td><center><b>fx</b></center></td>
00145                            <td><center><b>fy</b></center></td>
00146                            <td><center><b>cx</b></center></td>
00147                            <td><center><b>cy</b></center></td>
00148                           </tr>
00149 
00150                           <tr>
00151                            <td><center>0</center></td>
00152                            <td><center>Point Grey Research</center></td>
00153                            <td><center>Bumblebee</center></td>
00154                            <td><center>0.79345</center></td>
00155                            <td><center>1.05793</center></td>
00156                            <td><center>0.55662</center></td>
00157                            <td><center>0.52692</center></td>
00158                           </tr>
00159 
00160                           <tr>
00161                            <td><center>1</center></td>
00162                            <td><center>Sony</center></td>
00163                            <td><center>???</center></td>
00164                            <td><center>0.95666094</center></td>
00165                            <td><center>1.3983423f</center></td>
00166                            <td><center>0.54626328f</center></td>
00167                            <td><center>0.4939191f</center></td>
00168                           </tr>
00169                           </table>
00170                           </center>
00171 
00172                           * \sa buildIntrinsicParamsMatrix, pixelTo3D
00173                           */
00174                         CMatrixDouble33 VISION_IMPEXP defaultIntrinsicParamsMatrix(
00175                                 unsigned int            camIndex = 0,
00176                                 unsigned int            resolutionX = 320,
00177                                 unsigned int            resolutionY = 240 );
00178 
00179                         /** Explore the feature list and removes features which are in the same coordinates
00180                           * \param list [IN] The list of features.
00181                           */
00182                         void VISION_IMPEXP deleteRepeatedFeats(
00183                                 CFeatureList            & list );
00184 
00185                         /** Search for correspondences which are not in the same row and deletes them
00186                           * \param leftList     [IN/OUT]    The left list of matched features.
00187                           * \param rightList    [IN/OUT]    The right list of matched features.
00188                           * \param threshold    [IN]        The tolerance value for the row checking: valid matched are within this threshold.
00189                           */
00190                         void VISION_IMPEXP rowChecking(
00191                                 CFeatureList            & leftList,
00192                                                                 CFeatureList            & rightList,
00193                                                                 float                   threshold = 1.0);
00194 
00195                         /** Computes the dispersion of the features in the image
00196                           * \param list [IN]    Input list of features
00197                           * \param std  [OUT]   2 element vector containing the standard deviations in the 'x' and 'y' coordinates.
00198                           * \param mean [OUT]   2 element vector containing the mean in the 'x' and 'y' coordinates.
00199                           */
00200                         void VISION_IMPEXP getDispersion(
00201                                 const CFeatureList      & list,
00202                                                                 vector_float            & std,
00203                                                                 vector_float            & mean );
00204 
00205                         /** Returns a new image where distortion has been removed.
00206                           * \param A The 3x3 intrinsic parameters matrix
00207                           * \param dist_coeffs The 1x4 (or 1x5) vector of distortion coefficients
00208                           */
00209                         inline void correctDistortion(
00210                                 const CImage            & in_img,
00211                                 CImage                          & out_img,
00212                                 const CMatrixDouble33   & A,
00213                                 const vector_double     & dist_coeffs )
00214                         {
00215                                 in_img.rectifyImage( out_img, A, dist_coeffs);
00216                         }
00217 
00218 
00219                         /** Computes the mean squared distance between a set of 3D correspondences
00220                           * ...
00221                           */
00222                         double VISION_IMPEXP computeMsd(
00223                                 const TMatchingPairList & list,
00224                                                                 const poses::CPose3D    & Rt );
00225 
00226                         /** Transform two clouds of 3D points into a matched list of points
00227                           * ...
00228                           */
00229                         void VISION_IMPEXP cloudsToMatchedList(
00230                                 const CObservationVisualLandmarks   & cloud1,
00231                                 const CObservationVisualLandmarks   & cloud2,
00232                                                                 TMatchingPairList                   & outList);
00233 
00234                         /** Computes the main orientation of a set of points with an image (for using in SIFT-based algorithms)
00235                           * \param image    [IN] The input image.
00236                           * \param x        [IN] A vector containing the 'x' coordinates of the image points.
00237                           * \param y        [IN] A vector containing the 'y' coordinates of the image points.
00238                           * \return The main orientation of the image point.
00239                           */
00240                         float VISION_IMPEXP computeMainOrientation(
00241                                 const CImage                        & image,
00242                                                                 unsigned int                        x,
00243                                                                 unsigned int                        y );
00244 
00245                         /** Normalizes the brigthness and contrast of an image by setting its mean value to zero and its standard deviation to unit.
00246                           * \param image        [IN]        The input image.
00247                           * \param nimage       [OUTPUT]    The new normalized image.
00248                           */
00249             void VISION_IMPEXP normalizeImage(
00250                                 const CImage                        & image,
00251                                 CImage                              & nimage );
00252 
00253                         /** Find the matches between two lists of features which must be of the same type.
00254                           * \param list1    [IN]    One list.
00255                           * \param list2    [IN]    Other list.
00256                           * \param matches  [OUT]   A vector of pairs of correspondences.
00257                           * \param options  [IN]    A struct containing matching options
00258                           * \return Returns the number of matched pairs of features.
00259                           */
00260                         size_t VISION_IMPEXP matchFeatures(
00261                                 const CFeatureList                  & list1,
00262                                 const CFeatureList                  & list2,
00263                                 CMatchedFeatureList                 & matches,
00264                                 const TMatchingOptions              & options = TMatchingOptions(),
00265                                 const TStereoSystemParams           & params = TStereoSystemParams() );
00266 
00267             /** Calculates the Sum of Absolutes Differences (range [0,1]) between two patches. Both patches must have the same size.
00268                           * \param mList    [IN]  The list of matched features.
00269                           * \param mask1    [OUT] The output mask for left features.
00270                           * \param mask2    [OUT] The output mask for right features.
00271                           * \param wSize    [IN] The value of the masking window for each features.
00272                           * \exception if mList.size() = 0
00273                   */
00274             void VISION_IMPEXP generateMask(
00275                                 const CMatchedFeatureList           & mList,
00276                                 CMatrixBool                         & mask1,
00277                                 CMatrixBool                         & mask2,
00278                                 int                                 wSize = 10 );
00279 
00280             /** Calculates the Sum of Absolutes Differences (range [0,1]) between two patches. Both patches must have the same size.
00281                           * \param patch1 [IN]  One patch.
00282                           * \param patch2 [IN]  The other patch.
00283                           * \return The value of computed SAD normalized to [0,1]
00284                   */
00285                 double VISION_IMPEXP computeSAD(
00286                                 const CImage                        & patch1,
00287                                 const CImage                        & patch2 );
00288 
00289                         /** Draw rectangles around each of the features on a copy of the input image.
00290                           * \param inImg    [IN]    The input image where to draw the features.
00291                           * \param theList  [IN]    The list of features.
00292                           * \param outImg   [OUT]   The copy of the input image with the marked features.
00293                           */
00294                         void VISION_IMPEXP addFeaturesToImage(
00295                                 const CImage                        & inImg,
00296                                 const CFeatureList                  & theList,
00297                                 CImage                              & outImg );
00298 
00299                         /** Computes the 3D position of a set of matched features from their coordinates in the images. The list have to be matched in order, e.g. leftList[0]<->rightList[0]
00300                           * \param leftList     [IN]    The left list of features.
00301                           * \param rightList    [IN]    The right list of features.
00302                           * \param vP3D         [OUT]   A vector of TPoint3D containing the 3D positions of the projected points.
00303                           * \param params       [IN]    The intrinsic and extrinsic parameters of the stereo pair.
00304                           */
00305             void VISION_IMPEXP projectMatchedFeatures(
00306                                 const CFeatureList                              & leftList,
00307                                 const CFeatureList                              & rightList,
00308                                 vector<TPoint3D>                    & vP3D,
00309                                 const TStereoSystemParams           & params = TStereoSystemParams() );
00310 
00311                         /** Computes the 3D position of a particular matched feature.
00312                           * \param leftList     [IN]    The left feature.
00313                           * \param rightList    [IN]    The right feature.
00314                           * \param vP3D         [OUT]   The 3D position of the projected point.
00315                           * \param params       [IN]    The intrinsic and extrinsic parameters of the stereo pair.
00316                           */
00317             void VISION_IMPEXP projectMatchedFeature(
00318                                 const CFeaturePtr                   & leftFeat,
00319                                 const CFeaturePtr                   & rightFeat,
00320                                 TPoint3D                            & p3D,
00321                                 const TStereoSystemParams           & params = TStereoSystemParams() );
00322 
00323                         /** Project a list of matched features into the 3D space, using the provided parameters of the stereo system
00324                           * \param mfList       [IN/OUT]    The list of matched features. Features which yields a 3D point outside the area defined in TStereoSystemParams are removed from the lists.
00325                           * \param param        [IN]        The parameters of the stereo system.
00326                           * \param landmarks    [OUT]       A map containing the projected landmarks.
00327                           * \sa TStereoSystemParams, CLandmarksMap
00328                           */
00329                         void VISION_IMPEXP projectMatchedFeatures(
00330                                 CMatchedFeatureList                             & mfList,
00331                                 const TStereoSystemParams               & param,
00332                                 CLandmarksMap                               & landmarks );
00333 
00334                         /** Project a pair of feature lists into the 3D space, using the provided options for the stereo system. The matches must be in order,
00335                           *     i.e. leftList[0] corresponds to rightList[0] and so on. Features which yields a 3D point outside the area defined in TStereoSystemParams are removed from the lists.
00336                           * \param leftList     [IN/OUT]    The left list of matched features.
00337                           * \param rightList    [IN/OUT]    The right list of matched features.
00338                           * \param param        [IN]        The options of the stereo system.
00339                           * \param landmarks    (OUT]       A map containing the projected landmarks.
00340                           * \sa TStereoSystemParams, CLandmarksMap
00341                           */
00342                         void VISION_IMPEXP projectMatchedFeatures(
00343                                 CFeatureList                                    & leftList,
00344                                 CFeatureList                                        & rightList,
00345                                 const TStereoSystemParams               & param,
00346                                 CLandmarksMap                               & landmarks );
00347 
00348                         /** Converts a stereo images observation into a bearing and range observation.
00349                                 \param inObs    [IN]    The input stereo images observation.
00350                                 \param sg               [IN]    The sigma of the row, col, and disparity variables involved in the feature detection.
00351                                 \param outObs   [OUT]   The output bearing and range observation (including covariances).
00352                         */
00353                         void VISION_IMPEXP StereoObs2BRObs(
00354                                 const CObservationStereoImages      & inObs,
00355                                 const vector<double>                & sg,
00356                                 CObservationBearingRange            & outObs );
00357 
00358                         /** Converts a matched feature list into a bearing and range observation (some of the stereo camera system must be provided).
00359                                 \param inMatches                [IN]    The input list of matched features.
00360                                 \param intrinsicParams  [IN]    The intrisic params of the reference (left) camera of the stereo system.
00361                                 \param baseline                 [IN]    The distance among the X axis of the right camera wrt the reference (left) camera.
00362                                 \param sg                               [IN]    The sigma of the row, col, and disparity variables involved in the feature detection.
00363                                 \param outObs                   [OUT]   The output bearing and range observation (including covariances).
00364                         */
00365                         void VISION_IMPEXP StereoObs2BRObs(
00366                                 const CMatchedFeatureList           & inMatches,
00367                                 const CMatrixDouble33               & intrinsicParams,
00368                                 const double                        & baseline,
00369                                 const CPose3D                       & sensorPose,
00370                                 const vector<double>                & sg,
00371                                 CObservationBearingRange            & outObs );
00372 
00373                         /** Converts a CObservationVisualLandmarks into a bearing and range observation (without any covariances). Fields of view are not computed.
00374                                 \param inObs                    [IN]    The input observation.
00375                                 \param sg                               [IN]    The sigma of the row, col, and disparity variables involved in the feature detection.
00376                                 \param outObs                   [OUT]   The output bearing and range observation.
00377                         */
00378             void VISION_IMPEXP StereoObs2BRObs(
00379                                 const CObservationStereoImages      & inObs,
00380                                 const vector<double>                & sg,
00381                                 CObservationBearingRange            & outObs );
00382 
00383                         /** Converts a CObservationVisualLandmarks into a bearing and range observation (without any covariances). Fields of view are not computed.
00384                                 \param inObs                    [IN]    The input observation.
00385                                 \param outObs                   [OUT]   The output bearing and range observation.
00386                         */
00387                         void VISION_IMPEXP StereoObs2BRObs(
00388                                 const CObservationVisualLandmarks   & inObs,
00389                                 CObservationBearingRange            & outObs );
00390 
00391             /** Computes a pair of x-and-y maps for stereo rectification from a pair of cameras and the relative pose of the second one wrt the first one.
00392                 \param cam1, cam2           [IN]    The pair of involved cameras
00393                 \param rightCameraPose      [IN]    The change in pose of the second camera wrt the first one
00394                 \param outMap1x,outMap1y    [OUT]   The x-and-y maps corresponding to cam1 (should be converted to *cv::Mat)
00395                 \param outMap2x,outMap2y    [OUT]   The x-and-y maps corresponding to cam2 (should be converted to *cv::Mat)
00396             */
00397             void VISION_IMPEXP computeStereoRectificationMaps(
00398                                 const TCamera                       & cam1,
00399                                 const TCamera                       & cam2,
00400                                 const poses::CPose3D                & rightCameraPose,
00401                                 void                                *outMap1x,
00402                                 void                                *outMap1y,
00403                                 void                                *outMap2x,
00404                                 void                                *outMap2y );
00405 
00406 
00407         /** @} */ // end of grouping
00408                 
00409         } // end-namespace-vision
00410 } // end-namespace-mrpt
00411 
00412 
00413 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011