00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 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 #ifndef CImage_H 00029 #define CImage_H 00030 00031 #include <mrpt/utils/utils_defs.h> 00032 #include <mrpt/utils/CSerializable.h> 00033 #include <mrpt/math/CMatrix.h> 00034 #include <mrpt/utils/CCanvas.h> 00035 #include <mrpt/system/os.h> 00036 #include <mrpt/utils/exceptions.h> 00037 00038 namespace mrpt 00039 { 00040 namespace utils 00041 { 00042 /** Different resize methods used in CImage::scaleImage */ 00043 enum TInterpMethod 00044 { 00045 IMG_INTERP_NN = 0, 00046 IMG_INTERP_LINEAR=1, 00047 IMG_INTERP_CUBIC=2, 00048 IMG_INTERP_AREA=3 00049 }; 00050 00051 00052 // This must be added to any CSerializable derived class: 00053 DEFINE_SERIALIZABLE_PRE( CImage ) 00054 00055 /** A class for storing images as grayscale or RGB bitmaps. 00056 * File I/O is supported in two different ways: 00057 * - Binary dump using the CSerializable interface(<< and >> operators), just as most objects 00058 * in the MRPT library. This format is not compatible with any standarized image format. 00059 * - Saving/loading from files of different formats (bmp,jpg,png,...) using the methods CImage::loadFromFile and CImage::saveToFile. 00060 * 00061 * Additional notes: 00062 * - The OpenCV "IplImage" format is used internally for compatibility with all OpenCV functions. See CImage::getAsIplImage. 00063 * - Only the unsigned 8-bit storage format for pixels (on each channel) is supported 00064 * - An external storage mode can be enabled by calling CImage::setExternalStorage, useful for storing large collections of image objects in memory while loading the image data itself only for the relevant images at any time. 00065 * - To move images from one object to the another, use CImage::copyFastFrom rather than the copy operator =. 00066 * - If you are interested in a smart pointer to an image, use: 00067 * \code 00068 * CImagePtr myImgPtr = CImagePtr( new CImage(...) ); 00069 * \endcode 00070 * 00071 * Additional implementated operators: 00072 * - getAsFloat 00073 * - getMaxAsFloat 00074 * - scaleImage 00075 * - rotateImage 00076 * - An assignment "=" operator for converting between the classes "CImage" and "CImageFloat" and CMatrixFloat / CMatrixDouble. 00077 * - operators over images: * (Images multiplication) 00078 * - etc... 00079 * 00080 * \note This class acts as a wrapper class for OpenCV functions, and an IplImage is the internal representation for compatibility. 00081 * 00082 * \sa mrpt::vision, mrpt::vision::CFeatureExtractor, CSerializable, CCanvas 00083 */ 00084 class MRPTDLLIMPEXP CImage : public mrpt::utils::CSerializable, public CCanvas 00085 { 00086 friend class CImageFloat; 00087 00088 DEFINE_SERIALIZABLE( CImage ) 00089 00090 protected: 00091 /** Data members 00092 */ 00093 void *img; 00094 00095 /** Set to true only when using setFromIplImageReadOnly. 00096 * \sa setFromIplImageReadOnly 00097 */ 00098 bool m_imgIsReadOnly; 00099 00100 /** Set to true only when using setExternalStorage. 00101 * \sa setExternalStorage 00102 */ 00103 mutable bool m_imgIsExternalStorage; 00104 mutable std::string m_externalFile; //!< The file name of a external storage image. 00105 00106 /** Resize the buffers in "img" to accomodate a new image size and/or format. 00107 */ 00108 void changeSize( 00109 unsigned int width, 00110 unsigned int height, 00111 unsigned int nChannels, 00112 bool originTopLeft ); 00113 00114 /** Release the internal IPL image, if not NULL or read-only. */ 00115 void releaseIpl(bool thisIsExternalImgUnload = false) MRPT_NO_THROWS; 00116 00117 /** Checks if the image is of type "external storage", and if so and not loaded yet, load it. */ 00118 void makeSureImageIsLoaded() const throw (std::exception,utils::CExceptionExternalImageNotFound ); 00119 00120 void rectifyImage_internal( CImage &out_img, const math::CMatrixDouble &cameraMatrix, const vector_double &distCoeff ) const; 00121 void rectifyImageInPlace_internal( const math::CMatrixDouble &cameraMatrix, const vector_double &distCoeff ); 00122 00123 public: 00124 00125 /** By default, when storing images through the CSerializable interface, grayscale images will be ZIP compressed if they are larger than 16Kb: this flag can be turn on to disable ZIP compression and gain speed versus occupied space. 00126 * The default value of this variable is "false". 00127 */ 00128 static bool DISABLE_ZIP_COMPRESSION; 00129 00130 /** Changes the size of the image, erasing previous contents (does NOT scale its current content, for that, see scaleImage). 00131 * - nChannels: Can be 3 for RGB images or 1 for grayscale images. 00132 * - originTopLeft: Is true if the top-left corner is (0,0). In other case, the reference is the bottom-left corner. 00133 * \sa scaleImage 00134 */ 00135 void resize( 00136 unsigned int width, 00137 unsigned int height, 00138 unsigned int nChannels, 00139 bool originTopLeft ) 00140 { 00141 ASSERT_(img!=NULL); 00142 changeSize(width,height,nChannels,originTopLeft); 00143 } 00144 00145 /** Scales the image to a new size, interpolating as needed. 00146 * \sa resize, rotateImage 00147 */ 00148 void scaleImage( unsigned int width, unsigned int height, TInterpMethod interp = IMG_INTERP_CUBIC ); 00149 00150 /** Rotates the image by the given angle around the given center point, with an optional scale factor. 00151 * \sa resize, scaleImage 00152 */ 00153 void rotateImage( double angle_radians, unsigned int center_x, unsigned int center_y, double scale = 1.0 ); 00154 00155 /** Changes the value of the pixel (x,y). 00156 * Pixel coordinates starts at the left-top corner of the image, and start in (0,0). 00157 * The meaning of the parameter "color" depends on the implementation: it will usually 00158 * be a 24bit RGB value (0x00RRGGBB), but it can also be just a 8bit gray level. 00159 * This method must support (x,y) values OUT of the actual image size without neither 00160 * raising exceptions, nor leading to memory access errors. 00161 */ 00162 void setPixel(int x, int y, size_t color); 00163 00164 /** Draws a circle of a given radius. 00165 * \param x The center - x coordinate in pixels. 00166 * \param y The center - y coordinate in pixels. 00167 * \param radius The radius - in pixels. 00168 * \param color The color of the circle. 00169 * \param width The desired width of the line 00170 */ 00171 void drawCircle( 00172 int x, 00173 int y, 00174 int radius, 00175 const mrpt::utils::TColor &color = mrpt::utils::TColor(255,255,255), 00176 unsigned int width = 1); 00177 00178 /** Default constructor: 00179 */ 00180 CImage( ); 00181 /** Constructor: 00182 */ 00183 CImage( unsigned int width, 00184 unsigned int height, 00185 unsigned int nChannels = 3, 00186 bool originTopLeft = true 00187 ); 00188 /** Copy constructor: 00189 */ 00190 CImage( const CImage &o ); 00191 00192 /** Copy constructor: 00193 */ 00194 CImage( const CImageFloat &o ); 00195 00196 /** Copy operator 00197 * \sa copyFastFrom 00198 */ 00199 CImage& operator = (const CImage& o); 00200 00201 /** Copy operator from a gray-scale, float image: 00202 * \sa copyFastFrom 00203 */ 00204 CImage& operator = (const CImageFloat& o); 00205 00206 /** Moves an image from another object, erasing the origin image in the process (this is much faster than copying) 00207 * \sa operator = 00208 */ 00209 void copyFastFrom( CImage &o ); 00210 00211 /** Constructor from IplImage 00212 */ 00213 CImage( void *iplImage ); 00214 00215 /** Destructor: 00216 */ 00217 virtual ~CImage( ); 00218 00219 /** Returns a pointer to an "OpenCv" IplImage struct containing the image, which is linked to this class: free neigther that pointer nor this class until they are not required anymore, since this class is in charge of freeing the memory buffers inside of the returned image. 00220 */ 00221 void* getAsIplImage() const; 00222 00223 /** Access to pixels without checking boundaries - Use normally the () operator better, which checks the coordinates. 00224 \sa CImage::operator() 00225 */ 00226 unsigned char* get_unsafe( 00227 unsigned int col, 00228 unsigned int row, 00229 unsigned int channel=0) const; 00230 00231 00232 /** Returns the contents of a given pixel at the desired channel, in float format: [0,255]->[0,1] 00233 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00234 * \exception std::exception On pixel coordinates out of bounds 00235 * \sa operator() 00236 */ 00237 float getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const; 00238 00239 /** Returns the contents of a given pixel (for gray-scale images, in color images the gray scale equivalent is computed for the pixel), in float format: [0,255]->[0,1] 00240 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00241 * \exception std::exception On pixel coordinates out of bounds 00242 * \sa operator() 00243 */ 00244 float getAsFloat(unsigned int col, unsigned int row) const; 00245 00246 /** Return the maximum pixel value of the image, as a float value. 00247 * \sa getAsFloat 00248 */ 00249 float getMaxAsFloat() const; 00250 00251 /** Returns the width of the image in pixels 00252 * \sa getSize 00253 */ 00254 size_t getWidth() const; 00255 00256 /** Returns the height of the image in pixels 00257 * \sa getSize 00258 */ 00259 size_t getHeight() const; 00260 00261 /** Return the size of the image 00262 * \sa getWidth, getHeight 00263 */ 00264 void getSize(TImageSize &s) const; 00265 00266 /** Return the size of the image 00267 * \sa getWidth, getHeight 00268 */ 00269 TImageSize getSize() const { 00270 TImageSize ret; 00271 getSize(ret); 00272 return ret; 00273 } 00274 00275 /** Returns true if the image is RGB, false if it is gray scale 00276 */ 00277 bool isColor() const; 00278 00279 /** Returns true if the coordinates origin is top-left, or false if it is bottom-left 00280 */ 00281 bool isOriginTopLeft() const; 00282 00283 /** Changes the property of the image stating if the top-left corner (vs. bottom-left) is the coordinate reference. 00284 */ 00285 void setOriginTopLeft(bool val); 00286 00287 /** Reads the image from raw pixels buffer in memory. 00288 */ 00289 void loadFromMemoryBuffer( unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue = false ); 00290 00291 /** Reads a color image from three raw pixels buffers in memory. 00292 * bytesPerRow is the number of bytes per row per channel, i.e. the row increment. 00293 */ 00294 void loadFromMemoryBuffer( unsigned int width, unsigned int height, unsigned int bytesPerRow, unsigned char *red, unsigned char *green, unsigned char *blue ); 00295 00296 /** Reads the image from a OpenCV IplImage object (making a copy). 00297 */ 00298 void loadFromIplImage( void* iplImage ); 00299 00300 /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy). 00301 * This method provides a fast method to grab images from a camera without making a copy of every frame. 00302 */ 00303 void setFromIplImage( void* iplImage ); 00304 00305 /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy) and from now on the image cannot be modified, just read. 00306 * This method provides a fast method to grab images from a camera without making a copy of every frame. 00307 */ 00308 void setFromIplImageReadOnly( void* iplImage ); 00309 00310 /** By using this method the image is marked as referenced to an external file, which will be loaded only under demand. 00311 * A CImage with external storage does not consume memory until some method trying to access the image is invoked (e.g. getWidth(), isColor(),...) 00312 * At any moment, the image can be unloaded from memory again by invoking unload. 00313 * An image becomes of type "external storage" only through calling setExternalStorage. This property remains after serializing the object. 00314 * File names can be absolute, or relative to the CImage::IMAGES_PATH_BASE directory. Filenames staring with "X:\" or "/" are considered absolute paths. 00315 * By calling this method the current contents of the image are NOT saved to that file, because this method can be also called 00316 * to let the object know where to load the image in case its contents are required. Thus, for saving images in this format (not when loading) 00317 * the proper order of commands should be: 00318 * \code 00319 * img.saveToFile( fileName ); 00320 * img.setExternalStorage( fileName ); 00321 * \endcode 00322 * 00323 * \note Modifications to the memory copy of the image are not automatically saved to disk. 00324 * \note This feature has been added in MRPT 0.5.5. 00325 * \sa unload, isExternallyStored 00326 */ 00327 void setExternalStorage( const std::string &fileName ) MRPT_NO_THROWS; 00328 00329 static std::string IMAGES_PATH_BASE; //!< By default, "." \sa setExternalStorage 00330 00331 /** See setExternalStorage(). */ 00332 bool isExternallyStored() const MRPT_NO_THROWS { return m_imgIsExternalStorage; } 00333 00334 std::string getExternalStorageFile() const MRPT_NO_THROWS //!< Only if isExternallyStored() returns true. \sa getExternalStorageFileAbsolutePath 00335 { 00336 return m_externalFile; 00337 } 00338 00339 void getExternalStorageFileAbsolutePath(std::string &out_path) const; //!< Only if isExternallyStored() returns true. \sa getExternalStorageFile 00340 00341 /** For external storage image objects only, this method unloads the image from memory (or does nothing if already unloaded). 00342 * It does not need to be called explicitly, unless the user wants to save memory for images that will not be used often. 00343 * If called for an image without the flag "external storage", it is simply ignored. 00344 * \sa setExternalStorage 00345 */ 00346 void unload() MRPT_NO_THROWS; 00347 00348 /** Reads the image from a binary stream containing a binary jpeg file. 00349 * \exception std::exception On pixel coordinates out of bounds 00350 */ 00351 void loadFromStreamAsJPEG( CStream &in ); 00352 00353 /** Load image from a file, whose format is determined from the extension (internally uses OpenCV). 00354 * \param fileName The file to read from. 00355 * \param isColor Specifies colorness of the loaded image: 00356 * - if >0, the loaded image is forced to be color 3-channel image; 00357 * - if 0, the loaded image is forced to be grayscale; 00358 * - if <0, the loaded image will be loaded as is (with number of channels depends on the file). 00359 * The supported formats are: 00360 * 00361 * - Windows bitmaps - BMP, DIB; 00362 * - JPEG files - JPEG, JPG, JPE; 00363 * - Portable Network Graphics - PNG; 00364 * - Portable image format - PBM, PGM, PPM; 00365 * - Sun rasters - SR, RAS; 00366 * - TIFF files - TIFF, TIF. 00367 * 00368 * \return False on any error 00369 * \sa saveToFile, setExternalStorage 00370 */ 00371 bool loadFromFile( const std::string& fileName, int isColor = -1 ); 00372 00373 /** Save the image to a file, whose format is determined from the extension (internally uses OpenCV). 00374 * \param fileName The file to write to. 00375 * 00376 * The supported formats are: 00377 * 00378 * - Windows bitmaps - BMP, DIB; 00379 * - JPEG files - JPEG, JPG, JPE; 00380 * - Portable Network Graphics - PNG; 00381 * - Portable image format - PBM, PGM, PPM; 00382 * - Sun rasters - SR, RAS; 00383 * - TIFF files - TIFF, TIF. 00384 * 00385 * \return False on any error 00386 * \sa loadFromFile 00387 */ 00388 bool saveToFile( const std::string& fileName ) const; 00389 00390 /** Save image to binary stream as a JPEG (.jpg) compresed format. 00391 * \exception std::exception On number of rows or cols equal to zero, or other errors. 00392 * \sa saveToJPEG 00393 */ 00394 void saveToStreamAsJPEG( CStream &out )const; 00395 00396 /** Returns a pointer to a given pixel information. 00397 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00398 * \exception std::exception On pixel coordinates out of bounds 00399 */ 00400 unsigned char* operator()(unsigned int col, unsigned int row, unsigned int channel = 0) const; 00401 00402 /** Returns a grayscale version of the image, or itself if it is already a grayscale image. 00403 */ 00404 CImage grayscale() const; 00405 00406 /** Returns a grayscale version of the image, or itself if it is already a grayscale image. 00407 * \sa colorImage 00408 */ 00409 void grayscale( CImage &ret ) const; 00410 00411 /** Replaces the image with a grayscale version of it. 00412 * \sa colorImageInPlace 00413 */ 00414 void grayscaleInPlace(); 00415 00416 /** Returns a RGB version of the grayscale image, or itself if it is already a RGB image. 00417 * \sa grayscale 00418 */ 00419 void colorImage( CImage &ret ) const; 00420 00421 /** Replaces this grayscale image with a RGB version of it. 00422 * \sa grayscaleInPlace 00423 */ 00424 void colorImageInPlace(); 00425 00426 /** Returns a new image scaled down to half its original size. 00427 * \exception std::exception On odd size 00428 * \sa scaleDouble, scaleImage 00429 */ 00430 CImage scaleHalf()const; 00431 00432 /** Returns a new image scaled up to double its original size. 00433 * \exception std::exception On odd size 00434 * \sa scaleHalf, scaleImage 00435 */ 00436 CImage scaleDouble()const; 00437 00438 00439 /** Returns a string of the form "BGR" indicating the channels ordering. 00440 */ 00441 const char * getChannelsOrder()const; 00442 00443 /** Returns the number of channels (typ: 1 or 3) 00444 * \sa isColor 00445 */ 00446 unsigned int getChannelCount() const; 00447 00448 /** Extracts a patch of this image into another image. 00449 * (by AJOGD @ DEC-2006) 00450 */ 00451 void extract_patch( 00452 CImage &patch, 00453 const unsigned int col_=0, 00454 const unsigned int row_=0, 00455 const unsigned int col_num=1, 00456 const unsigned int row_num=1 ) const; 00457 00458 /** Computes the correlation coefficient (returned as val), between two images 00459 * This function use grayscale images only 00460 * img1, img2 must be same size 00461 * (by AJOGD @ DEC-2006) 00462 */ 00463 float correlate( const CImage &img2int, int width_init=0, int height_init=0 )const; 00464 00465 /** Computes the cross_correlation between two images and return a matrix of correlation coeficients 00466 * This function use grayscale images only 00467 * (by AJOGD @ DEC-2006) 00468 * \sa cross_correlation_FFT 00469 */ 00470 void cross_correlation( 00471 CImage &img2, 00472 math::CMatrixFloat &M, 00473 const int &u_search_ini, const int &v_search_ini, 00474 const int &u_search_size, const int &v_search_size)const; 00475 00476 /** Computes the correlation matrix between this image and another one. 00477 * This implementation uses the 2D FFT for achieving reduced computation time. 00478 * \param in_img The "patch" image, which must be equal, or smaller than "this" image. This function supports gray-scale (1 channel only) images. 00479 * \param u_search_ini The "x" coordinate of the search window. 00480 * \param v_search_ini The "y" coordinate of the search window. 00481 * \param u_search_size The width of the search window. 00482 * \param v_search_size The height of the search window. 00483 * \param out_corr The output for the correlation matrix, which will be "u_search_size" x "v_search_size" 00484 * \param biasThisImg This optional parameter is a fixed "bias" value to be substracted to the pixels of "this" image before performing correlation. 00485 * \param biasInImg This optional parameter is a fixed "bias" value to be substracted to the pixels of "in_img" image before performing correlation. 00486 * Note: By default, the search area is the whole (this) image. 00487 * (by JLBC @ JAN-2006) 00488 * \sa cross_correlation 00489 */ 00490 void cross_correlation_FFT( 00491 const CImage &in_img, 00492 math::CMatrixFloat &out_corr, 00493 int u_search_ini=-1, 00494 int v_search_ini=-1, 00495 int u_search_size=-1, 00496 int v_search_size=-1, 00497 float biasThisImg = 0, 00498 float biasInImg = 0 00499 ) const; 00500 00501 /** Returns the image as a matrix with pixel grayscale values in the range [0,1] 00502 * \param doResize If set to true (default), the output matrix will be always the size of the image at output. If set to false, the matrix will be enlarged to the size of the image, but it will not be cropped if it has room enough (useful for FFT2D,...) 00503 * \param x_min The starting "x" coordinate to extract (default=0=the first column) 00504 * \param y_min The starting "y" coordinate to extract (default=0=the first row) 00505 * \param x_max The final "x" coordinate (inclusive) to extract (default=-1=the last column) 00506 * \param y_max The final "y" coordinate (inclusive) to extract (default=-1=the last row) 00507 * (by JLBC @ JAN-2006) 00508 * \sa setFromMatrix 00509 */ 00510 void getAsMatrix( 00511 mrpt::math::CMatrixFloat &outMatrix, 00512 bool doResize = true, 00513 int x_min = 0, 00514 int y_min = 0, 00515 int x_max = -1, 00516 int y_max = -1 00517 ) const; 00518 00519 /** Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,1] (normalized=true) or [0,255] (normalized=false) 00520 * \sa getAsMatrix 00521 */ 00522 void setFromMatrix(const mrpt::math::CMatrixFloat &m, bool matrix_is_normalized=true); 00523 00524 /** Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,1] (normalized=true) or [0,255] (normalized=false) 00525 * \sa getAsMatrix 00526 */ 00527 void setFromMatrix(const mrpt::math::CMatrixDouble &m, bool matrix_is_normalized=true); 00528 00529 /** Returns the image as a matrix, where the image is "tiled" (repeated) the required number of times to fill the entire size of the matrix on input. 00530 * (by JLBC @ JAN-2006) 00531 */ 00532 void getAsMatrixTiled( math::CMatrix &outMatrix ) const; 00533 00534 /** Optimize de brightness range of a image without using histogram 00535 * Only for one channel images. 00536 * (by AJOGD @ JAN-2007) 00537 */ 00538 void normalize(); 00539 00540 /** Computes the correlation between this image and another one, encapsulating the openCV function cvMatchTemplate 00541 * This implementation reduced computation time. 00542 * \param patch_img The "patch" image, which must be equal, or smaller than "this" image. This function supports gray-scale (1 channel only) images. 00543 * \param u_search_ini The "x" coordinate of the search window. 00544 * \param v_search_ini The "y" coordinate of the search window. 00545 * \param u_search_size The width of the search window. 00546 * \param v_search_size The height of the search window. 00547 * \param u_max The u coordinate where find the maximun cross correlation value. 00548 * \param v_max The v coordinate where find the maximun cross correlation value 00549 * \param max_val The maximun value of cross correlation which we can find 00550 * Note: By default, the search area is the whole (this) image. 00551 * (by AJOGD @ MAR-2007) 00552 * \sa cross_correlation 00553 */ 00554 void openCV_cross_correlation( 00555 const CImage &patch_img, 00556 size_t &u_max, 00557 size_t &v_max, 00558 double &max_val, 00559 int u_search_ini=-1, 00560 int v_search_ini=-1, 00561 int u_search_size=-1, 00562 int v_search_size=-1)const; 00563 00564 /** Flips vertically the image. 00565 * \sa swapRB 00566 */ 00567 void flipVertical(bool also_swapRB = false); 00568 00569 /** Swaps red and blue channels. 00570 * \sa flipVertical 00571 */ 00572 void swapRB(); 00573 00574 /** Rectifies the image according to a certain camera matrix and vector of distortion coefficients and returns an output rectified image 00575 * \param out_img The output rectified image 00576 * \param cameraMatrix The input camera matrix (containing the intrinsic parameters of the camera): [fx 0 cx; 0 fy cy; 0 0 1]: (fx,fy) focal length and (cx,cy) principal point coordinates 00577 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00578 */ 00579 template <class T1, class T2> 00580 void rectifyImage( CImage &out_img, const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const math::CMatrixTemplateNumeric<T2> &distCoeff ) const 00581 { 00582 math::CMatrixDouble K( cameraMatrix ); 00583 vector_double D; 00584 distCoeff.extractRow(0,D); 00585 rectifyImage_internal(out_img,K,D); 00586 } 00587 00588 /** Rectifies the image according to a certain camera matrix and vector of distortion coefficients, replacing "this"· with the rectified image 00589 * \param cameraMatrix The input camera matrix (containing the intrinsic parameters of the camera): [fx 0 cx; 0 fy cy; 0 0 1]: (fx,fy) focal length and (cx,cy) principal point coordinates 00590 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00591 */ 00592 template <class T1, class T2> 00593 void rectifyImageInPlace( const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const math::CMatrixTemplateNumeric<T2> &distCoeff ) 00594 { 00595 math::CMatrixDouble K( cameraMatrix ); 00596 vector_double D; 00597 distCoeff.extractRow(0,D); 00598 rectifyImageInPlace_internal(K,D); 00599 } 00600 00601 /** Rectifies the image according to a certain camera matrix and vector of distortion coefficients and returns an output rectified image 00602 * \param out_img The output rectified image 00603 * \param cameraMatrix The input camera matrix (containing the intrinsic parameters of the camera): [fx 0 cx; 0 fy cy; 0 0 1]: (fx,fy) focal length and (cx,cy) principal point coordinates 00604 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00605 */ 00606 template <class T1, class T2> 00607 void rectifyImage( CImage &out_img, const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const std::vector<T2> &distCoeff ) const 00608 { 00609 math::CMatrixDouble K( cameraMatrix ); 00610 vector_double D(distCoeff.size()); 00611 for (size_t i=0;i<distCoeff.size();i++) D[i]=static_cast<double>(distCoeff[i]); 00612 rectifyImage_internal(out_img,K,D); 00613 } 00614 00615 /** Rectifies the image according to a certain camera matrix and vector of distortion coefficients, replacing "this"· with the rectified image 00616 * \param cameraMatrix The input camera matrix (containing the intrinsic parameters of the camera): [fx 0 cx; 0 fy cy; 0 0 1]: (fx,fy) focal length and (cx,cy) principal point coordinates 00617 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00618 */ 00619 template <class T1, class T2> 00620 void rectifyImageInPlace( const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const std::vector<T2> &distCoeff ) 00621 { 00622 math::CMatrixDouble K( cameraMatrix ); 00623 vector_double D(distCoeff.size()); 00624 for (size_t i=0;i<distCoeff.size();i++) D[i]=static_cast<double>(distCoeff[i]); 00625 rectifyImageInPlace_internal(K,D); 00626 } 00627 00628 /** Filter the image with a Median filter with a window size WxW, returning the filtered image in out_img */ 00629 void filterMedian( CImage &out_img, int W=3 ) const; 00630 00631 /** Filter the image with a Median filter with a window size WxH, replacing "this" image by the filtered one. */ 00632 void filterMedianInPlace( int W=3 ); 00633 00634 /** Filter the image with a Gaussian filter with a window size WxH, returning the filtered image in out_img */ 00635 void filterGaussianInPlace( int W = 3, int H = 3 ); 00636 00637 /** Filter the image with a Gaussian filter with a window size WxH, replacing "this" image by the filtered one. */ 00638 void filterGaussian( CImage &out_img, int W = 3, int H = 3) const; 00639 00640 /** Look for the corners of a chessboard in the image. 00641 * This method uses internally OpenCV functions: 00642 * - cvFindChessboardCorners 00643 * - cvFindCornerSubPix 00644 * 00645 * \param cornerCoords [OUT] The pixel coordinates of all the corners. 00646 * \param check_size_x [IN] The number of squares, in the X direction 00647 * \param check_size_y [IN] The number of squares, in the Y direction 00648 * \param normalize_image [IN] Whether to normalize the image before detection 00649 * 00650 * \return true on success 00651 * 00652 * \sa mrpt::vision::checkerBoardCameraCalibration, drawChessboardCorners 00653 */ 00654 bool findChessboardCorners( 00655 std::vector<TPixelCoordf> &cornerCoords, 00656 unsigned int check_size_x, 00657 unsigned int check_size_y, 00658 bool normalize_image = true ) const; 00659 00660 /** Draw onto this image the detected corners of a chessboard. The length of cornerCoords must be the product of the two check_sizes. 00661 * 00662 * \param cornerCoords [IN] The pixel coordinates of all the corners. 00663 * \param check_size_x [IN] The number of squares, in the X direction 00664 * \param check_size_y [IN] The number of squares, in the Y direction 00665 * 00666 * \return false if the length of cornerCoords is inconsistent (nothing is drawn then). 00667 * 00668 * \sa findChessboardCorners 00669 */ 00670 bool drawChessboardCorners( 00671 std::vector<TPixelCoordf> &cornerCoords, 00672 unsigned int check_size_x, 00673 unsigned int check_size_y ); 00674 00675 /** Joins two images side-by-side horizontally. Both images must have the same number of rows and be of the same type (i.e. depth and color mode) 00676 * 00677 * \param im1 [IN] The first image. 00678 * \param im2 [IN] The other image. 00679 */ 00680 void joinImagesHorz( 00681 const CImage &im1, 00682 const CImage &im2 ); 00683 00684 }; // End of class 00685 00686 typedef CImage CMRPTImage; //!< Deprecated name. 00687 00688 00689 } // end of namespace utils 00690 00691 } // end of namespace mrpt 00692 00693 #endif
Page generated by Doxygen 1.6.1 for MRPT 0.7.1 SVN: at Tue Dec 22 08:29:35 CET 2009 |