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