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 00039 namespace mrpt 00040 { 00041 namespace utils 00042 { 00043 /** Interpolation methods for images. 00044 * Used for OpenCV related operations with images, but also with MRPT native classes. 00045 * \sa mrpt::utils::CMappedImage, CImage::scaleImage 00046 */ 00047 enum TInterpolationMethod 00048 { 00049 IMG_INTERP_NN = 0, 00050 IMG_INTERP_LINEAR=1, 00051 IMG_INTERP_CUBIC=2, 00052 IMG_INTERP_AREA=3 00053 }; 00054 00055 00056 // This must be added to any CSerializable derived class: 00057 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CImage, mrpt::utils::CSerializable ) 00058 00059 /** A class for storing images as grayscale or RGB bitmaps. 00060 * File I/O is supported in two different ways: 00061 * - Binary dump using the CSerializable interface(<< and >> operators), just as most objects 00062 * in the MRPT library. This format is not compatible with any standarized image format. 00063 * - Saving/loading from files of different formats (bmp,jpg,png,...) using the methods CImage::loadFromFile and CImage::saveToFile. 00064 * 00065 * Additional notes: 00066 * - The OpenCV "IplImage" format is used internally for compatibility with all OpenCV functions. See CImage::getAsIplImage. 00067 * - Only the unsigned 8-bit storage format for pixels (on each channel) is supported 00068 * - 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. 00069 * - To move images from one object to the another, use CImage::copyFastFrom rather than the copy operator =. 00070 * - If you are interested in a smart pointer to an image, use: 00071 * \code 00072 * CImagePtr myImgPtr = CImagePtr( new CImage(...) ); 00073 * \endcode 00074 * - To set a CImage from an OpenCV "IPLImage*", use the methods: 00075 * - CImage::loadFromIplImage 00076 * - CImage::setFromIplImage 00077 * - CImage::CImage(void *IPL) 00078 * 00079 * 00080 * Additional implementated operators: 00081 * - getAsFloat 00082 * - getMaxAsFloat 00083 * - scaleImage 00084 * - rotateImage 00085 * - An assignment "=" operator for converting between the classes "CImage" and "CImageFloat" and CMatrixFloat / CMatrixDouble. 00086 * - operators over images: * (Images multiplication) 00087 * - etc... 00088 * 00089 * \note This class acts as a wrapper class for OpenCV functions, and an IplImage is the internal representation for compatibility. 00090 * 00091 * \sa mrpt::vision, mrpt::vision::CFeatureExtractor, CSerializable, CCanvas 00092 */ 00093 class BASE_IMPEXP CImage : public mrpt::utils::CSerializable, public CCanvas 00094 { 00095 friend class CImageFloat; 00096 00097 DEFINE_SERIALIZABLE( CImage ) 00098 00099 protected: 00100 /** Data members 00101 */ 00102 void *img; 00103 00104 /** Set to true only when using setFromIplImageReadOnly. 00105 * \sa setFromIplImageReadOnly 00106 */ 00107 bool m_imgIsReadOnly; 00108 00109 /** Set to true only when using setExternalStorage. 00110 * \sa setExternalStorage 00111 */ 00112 mutable bool m_imgIsExternalStorage; 00113 mutable std::string m_externalFile; //!< The file name of a external storage image. 00114 00115 /** Resize the buffers in "img" to accomodate a new image size and/or format. 00116 */ 00117 void changeSize( 00118 unsigned int width, 00119 unsigned int height, 00120 unsigned int nChannels, 00121 bool originTopLeft ); 00122 00123 /** Release the internal IPL image, if not NULL or read-only. */ 00124 void releaseIpl(bool thisIsExternalImgUnload = false) MRPT_NO_THROWS; 00125 00126 /** Checks if the image is of type "external storage", and if so and not loaded yet, load it. */ 00127 void makeSureImageIsLoaded() const throw (std::exception,utils::CExceptionExternalImageNotFound ); 00128 00129 public: 00130 00131 /** 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. 00132 * The default value of this variable is "false". 00133 */ 00134 static bool DISABLE_ZIP_COMPRESSION; 00135 00136 /** Changes the size of the image, erasing previous contents (does NOT scale its current content, for that, see scaleImage). 00137 * - nChannels: Can be 3 for RGB images or 1 for grayscale images. 00138 * - originTopLeft: Is true if the top-left corner is (0,0). In other case, the reference is the bottom-left corner. 00139 * \sa scaleImage 00140 */ 00141 void resize( 00142 unsigned int width, 00143 unsigned int height, 00144 unsigned int nChannels, 00145 bool originTopLeft ) 00146 { 00147 ASSERT_(img!=NULL); 00148 changeSize(width,height,nChannels,originTopLeft); 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, TInterpolationMethod 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, TInterpolationMethod 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 void swap(CImage &o); //!< Very efficient swap of two images (just swap the internal pointers) 00231 00232 /** 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. 00233 */ 00234 void* getAsIplImage() const; 00235 00236 /** Access to pixels without checking boundaries - Use normally the () operator better, which checks the coordinates. 00237 \sa CImage::operator() 00238 */ 00239 unsigned char* get_unsafe( 00240 unsigned int col, 00241 unsigned int row, 00242 unsigned int channel=0) const; 00243 00244 00245 /** Returns the contents of a given pixel at the desired channel, in float format: [0,255]->[0,1] 00246 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00247 * \exception std::exception On pixel coordinates out of bounds 00248 * \sa operator() 00249 */ 00250 float getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const; 00251 00252 /** 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] 00253 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00254 * \exception std::exception On pixel coordinates out of bounds 00255 * \sa operator() 00256 */ 00257 float getAsFloat(unsigned int col, unsigned int row) const; 00258 00259 /** Return the maximum pixel value of the image, as a float value. 00260 * \sa getAsFloat 00261 */ 00262 float getMaxAsFloat() const; 00263 00264 /** Returns the width of the image in pixels 00265 * \sa getSize 00266 */ 00267 size_t getWidth() const; 00268 00269 /** Returns the height of the image in pixels 00270 * \sa getSize 00271 */ 00272 size_t getHeight() const; 00273 00274 /** Return the size of the image 00275 * \sa getWidth, getHeight 00276 */ 00277 void getSize(TImageSize &s) const; 00278 00279 /** Return the size of the image 00280 * \sa getWidth, getHeight 00281 */ 00282 TImageSize getSize() const { 00283 TImageSize ret; 00284 getSize(ret); 00285 return ret; 00286 } 00287 00288 /** Returns true if the image is RGB, false if it is gray scale 00289 */ 00290 bool isColor() const; 00291 00292 /** Returns true if the coordinates origin is top-left, or false if it is bottom-left 00293 */ 00294 bool isOriginTopLeft() const; 00295 00296 /** Changes the property of the image stating if the top-left corner (vs. bottom-left) is the coordinate reference. 00297 */ 00298 void setOriginTopLeft(bool val); 00299 00300 /** Reads the image from raw pixels buffer in memory. 00301 */ 00302 void loadFromMemoryBuffer( unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue = false ); 00303 00304 /** Reads a color image from three raw pixels buffers in memory. 00305 * bytesPerRow is the number of bytes per row per channel, i.e. the row increment. 00306 */ 00307 void loadFromMemoryBuffer( unsigned int width, unsigned int height, unsigned int bytesPerRow, unsigned char *red, unsigned char *green, unsigned char *blue ); 00308 00309 /** Reads the image from a OpenCV IplImage object (making a copy). 00310 */ 00311 void loadFromIplImage( void* iplImage ); 00312 00313 /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy). 00314 * This method provides a fast method to grab images from a camera without making a copy of every frame. 00315 */ 00316 void setFromIplImage( void* iplImage ); 00317 00318 /** Reads the image from a OpenCV IplImage object (WITHOUT making a copy) and from now on the image cannot be modified, just read. 00319 * This method provides a fast method to grab images from a camera without making a copy of every frame. 00320 */ 00321 void setFromIplImageReadOnly( void* iplImage ); 00322 00323 /** By using this method the image is marked as referenced to an external file, which will be loaded only under demand. 00324 * A CImage with external storage does not consume memory until some method trying to access the image is invoked (e.g. getWidth(), isColor(),...) 00325 * At any moment, the image can be unloaded from memory again by invoking unload. 00326 * An image becomes of type "external storage" only through calling setExternalStorage. This property remains after serializing the object. 00327 * File names can be absolute, or relative to the CImage::IMAGES_PATH_BASE directory. Filenames staring with "X:\" or "/" are considered absolute paths. 00328 * By calling this method the current contents of the image are NOT saved to that file, because this method can be also called 00329 * 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) 00330 * the proper order of commands should be: 00331 * \code 00332 * img.saveToFile( fileName ); 00333 * img.setExternalStorage( fileName ); 00334 * \endcode 00335 * 00336 * \note Modifications to the memory copy of the image are not automatically saved to disk. 00337 * \note This feature has been added in MRPT 0.5.5. 00338 * \sa unload, isExternallyStored 00339 */ 00340 void setExternalStorage( const std::string &fileName ) MRPT_NO_THROWS; 00341 00342 static std::string IMAGES_PATH_BASE; //!< By default, "." \sa setExternalStorage 00343 00344 /** See setExternalStorage(). */ 00345 bool isExternallyStored() const MRPT_NO_THROWS { return m_imgIsExternalStorage; } 00346 00347 inline std::string getExternalStorageFile() const MRPT_NO_THROWS //!< Only if isExternallyStored() returns true. \sa getExternalStorageFileAbsolutePath 00348 { 00349 return m_externalFile; 00350 } 00351 00352 /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */ 00353 void getExternalStorageFileAbsolutePath(std::string &out_path) const; 00354 00355 /** Only if isExternallyStored() returns true. \sa getExternalStorageFile */ 00356 inline std::string getExternalStorageFileAbsolutePath() const { 00357 std::string tmp; 00358 getExternalStorageFileAbsolutePath(tmp); 00359 return tmp; 00360 } 00361 00362 /** For external storage image objects only, this method unloads the image from memory (or does nothing if already unloaded). 00363 * It does not need to be called explicitly, unless the user wants to save memory for images that will not be used often. 00364 * If called for an image without the flag "external storage", it is simply ignored. 00365 * \sa setExternalStorage 00366 */ 00367 void unload() MRPT_NO_THROWS; 00368 00369 /** Reads the image from a binary stream containing a binary jpeg file. 00370 * \exception std::exception On pixel coordinates out of bounds 00371 */ 00372 void loadFromStreamAsJPEG( CStream &in ); 00373 00374 /** Load image from a file, whose format is determined from the extension (internally uses OpenCV). 00375 * \param fileName The file to read from. 00376 * \param isColor Specifies colorness of the loaded image: 00377 * - if >0, the loaded image is forced to be color 3-channel image; 00378 * - if 0, the loaded image is forced to be grayscale; 00379 * - if <0, the loaded image will be loaded as is (with number of channels depends on the file). 00380 * The supported formats are: 00381 * 00382 * - Windows bitmaps - BMP, DIB; 00383 * - JPEG files - JPEG, JPG, JPE; 00384 * - Portable Network Graphics - PNG; 00385 * - Portable image format - PBM, PGM, PPM; 00386 * - Sun rasters - SR, RAS; 00387 * - TIFF files - TIFF, TIF. 00388 * 00389 * \return False on any error 00390 * \sa saveToFile, setExternalStorage 00391 */ 00392 bool loadFromFile( const std::string& fileName, int isColor = -1 ); 00393 00394 /** Save the image to a file, whose format is determined from the extension (internally uses OpenCV). 00395 * \param fileName The file to write to. 00396 * 00397 * The supported formats are: 00398 * 00399 * - Windows bitmaps - BMP, DIB; 00400 * - JPEG files - JPEG, JPG, JPE; 00401 * - Portable Network Graphics - PNG; 00402 * - Portable image format - PBM, PGM, PPM; 00403 * - Sun rasters - SR, RAS; 00404 * - TIFF files - TIFF, TIF. 00405 * 00406 * \param jpeg_quality Only for JPEG files, the quality of the compression in the range [0-100]. Larger is better quality but slower. 00407 * \note jpeg_quality is only effective if MRPT is compiled against OpenCV 1.1.0 or newer. 00408 * \return False on any error 00409 * \sa loadFromFile 00410 */ 00411 bool saveToFile( const std::string& fileName, int jpeg_quality = 95 ) const; 00412 00413 /** Save image to binary stream as a JPEG (.jpg) compresed format. 00414 * \exception std::exception On number of rows or cols equal to zero, or other errors. 00415 * \sa saveToJPEG 00416 */ 00417 void saveToStreamAsJPEG( CStream &out )const; 00418 00419 /** Returns a pointer to a given pixel information. 00420 * The coordinate origin is pixel(0,0)=top-left corner of the image. 00421 * \exception std::exception On pixel coordinates out of bounds 00422 */ 00423 unsigned char* operator()(unsigned int col, unsigned int row, unsigned int channel = 0) const; 00424 00425 /** Returns a grayscale version of the image, or itself if it is already a grayscale image. 00426 */ 00427 CImage grayscale() const; 00428 00429 /** Returns a grayscale version of the image, or itself if it is already a grayscale image. 00430 * \sa colorImage 00431 */ 00432 void grayscale( CImage &ret ) const; 00433 00434 /** Replaces the image with a grayscale version of it. 00435 * \sa colorImageInPlace 00436 */ 00437 void grayscaleInPlace(); 00438 00439 00440 void equalizeHistInPlace(); //!< Equalize the image histogram, replacing the original image. 00441 00442 void equalizeHist( CImage &outImg ) const; //!< Equalize the image histogram, saving the new image in the given output object. 00443 00444 00445 /** Returns a RGB version of the grayscale image, or itself if it is already a RGB image. 00446 * \sa grayscale 00447 */ 00448 void colorImage( CImage &ret ) const; 00449 00450 /** Replaces this grayscale image with a RGB version of it. 00451 * \sa grayscaleInPlace 00452 */ 00453 void colorImageInPlace(); 00454 00455 /** Returns a new image scaled down to half its original size. 00456 * \exception std::exception On odd size 00457 * \sa scaleDouble, scaleImage 00458 */ 00459 CImage scaleHalf()const; 00460 00461 /** Returns a new image scaled up to double its original size. 00462 * \exception std::exception On odd size 00463 * \sa scaleHalf, scaleImage 00464 */ 00465 CImage scaleDouble()const; 00466 00467 00468 /** Returns a string of the form "BGR" indicating the channels ordering. 00469 */ 00470 const char * getChannelsOrder()const; 00471 00472 /** Returns the number of channels (typ: 1 or 3) 00473 * \sa isColor 00474 */ 00475 unsigned int getChannelCount() const; 00476 00477 /** Update image with patch given as argument. Upper left corner of the patch 00478 * will be will be set to the pixel described by the two arguments _row, and _column. 00479 * \exception std::exception if patch pasted on the pixel (_row, _column) jut out 00480 * of the image. 00481 */ 00482 void update_patch(CImage &patch, 00483 const unsigned int col_, 00484 const unsigned int row_); 00485 00486 /** Extracts a patch of this image into another image. 00487 * (by AJOGD @ DEC-2006) 00488 */ 00489 void extract_patch( 00490 CImage &patch, 00491 const unsigned int col_=0, 00492 const unsigned int row_=0, 00493 const unsigned int col_num=1, 00494 const unsigned int row_num=1 ) const; 00495 00496 /** Computes the correlation coefficient (returned as val), between two images 00497 * This function use grayscale images only 00498 * img1, img2 must be same size 00499 * (by AJOGD @ DEC-2006) 00500 */ 00501 float correlate( const CImage &img2int, int width_init=0, int height_init=0 )const; 00502 00503 /** Computes the correlation between this image and another one, encapsulating the openCV function cvMatchTemplate 00504 * 00505 * \param patch_img The "patch" image, which must be equal, or smaller than "this" image. This function supports gray-scale (1 channel only) images. 00506 * \param u_search_ini The "x" coordinate of the search window. 00507 * \param v_search_ini The "y" coordinate of the search window. 00508 * \param u_search_size The width of the search window. 00509 * \param v_search_size The height of the search window. 00510 * \param u_max The u coordinate where find the maximun cross correlation value. 00511 * \param v_max The v coordinate where find the maximun cross correlation value 00512 * \param max_val The maximun value of cross correlation which we can find 00513 * \param out_corr_image If a !=NULL pointer is provided, it will be saved here the correlation image. The size of the output image is (this_width-patch_width+1, this_height-patch_height+1 ) 00514 * Note: By default, the search area is the whole (this) image. 00515 * (by AJOGD @ MAR-2007) 00516 */ 00517 void cross_correlation( 00518 const CImage &patch_img, 00519 size_t &u_max, 00520 size_t &v_max, 00521 double &max_val, 00522 int u_search_ini=-1, 00523 int v_search_ini=-1, 00524 int u_search_size=-1, 00525 int v_search_size=-1, 00526 CImage *out_corr_image = NULL 00527 )const; 00528 00529 /** Computes the correlation matrix between this image and another one. 00530 * This implementation uses the 2D FFT for achieving reduced computation time. 00531 * \param in_img The "patch" image, which must be equal, or smaller than "this" image. This function supports gray-scale (1 channel only) images. 00532 * \param u_search_ini The "x" coordinate of the search window. 00533 * \param v_search_ini The "y" coordinate of the search window. 00534 * \param u_search_size The width of the search window. 00535 * \param v_search_size The height of the search window. 00536 * \param out_corr The output for the correlation matrix, which will be "u_search_size" x "v_search_size" 00537 * \param biasThisImg This optional parameter is a fixed "bias" value to be substracted to the pixels of "this" image before performing correlation. 00538 * \param biasInImg This optional parameter is a fixed "bias" value to be substracted to the pixels of "in_img" image before performing correlation. 00539 * Note: By default, the search area is the whole (this) image. 00540 * (by JLBC @ JAN-2006) 00541 * \sa cross_correlation 00542 */ 00543 void cross_correlation_FFT( 00544 const CImage &in_img, 00545 math::CMatrixFloat &out_corr, 00546 int u_search_ini=-1, 00547 int v_search_ini=-1, 00548 int u_search_size=-1, 00549 int v_search_size=-1, 00550 float biasThisImg = 0, 00551 float biasInImg = 0 00552 ) const; 00553 00554 /** Returns the image as a matrix with pixel grayscale values in the range [0,1] 00555 * \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,...) 00556 * \param x_min The starting "x" coordinate to extract (default=0=the first column) 00557 * \param y_min The starting "y" coordinate to extract (default=0=the first row) 00558 * \param x_max The final "x" coordinate (inclusive) to extract (default=-1=the last column) 00559 * \param y_max The final "y" coordinate (inclusive) to extract (default=-1=the last row) 00560 * (by JLBC @ JAN-2006) 00561 * \sa setFromMatrix 00562 */ 00563 void getAsMatrix( 00564 mrpt::math::CMatrixFloat &outMatrix, 00565 bool doResize = true, 00566 int x_min = 0, 00567 int y_min = 0, 00568 int x_max = -1, 00569 int y_max = -1 00570 ) const; 00571 00572 /** Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,1] (normalized=true) or [0,255] (normalized=false) 00573 * \sa getAsMatrix 00574 */ 00575 void setFromMatrix(const mrpt::math::CMatrixFloat &m, bool matrix_is_normalized=true); 00576 00577 /** Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,1] (normalized=true) or [0,255] (normalized=false) 00578 * \sa getAsMatrix 00579 */ 00580 void setFromMatrix(const mrpt::math::CMatrixDouble &m, bool matrix_is_normalized=true); 00581 00582 /** 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. 00583 * (by JLBC @ JAN-2006) 00584 */ 00585 void getAsMatrixTiled( math::CMatrix &outMatrix ) const; 00586 00587 /** Optimize de brightness range of a image without using histogram 00588 * Only for one channel images. 00589 * (by AJOGD @ JAN-2007) 00590 */ 00591 void normalize(); 00592 00593 /** Flips vertically the image. 00594 * \sa swapRB 00595 */ 00596 void flipVertical(bool also_swapRB = false); 00597 00598 /** Swaps red and blue channels. 00599 * \sa flipVertical 00600 */ 00601 void swapRB(); 00602 00603 /** Rectify (un-distort) the image according to a certain camera matrix and vector of distortion coefficients and returns an output rectified image 00604 * \param out_img The output rectified image 00605 * \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 00606 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00607 */ 00608 void rectifyImage( CImage &out_img, const math::CMatrixDouble33 &cameraMatrix, const vector_double &distCoeff ) const; 00609 00610 /** Rectify (un-distort) the image according to a certain camera matrix and vector of distortion coefficients, replacing "this"ยท with the rectified image 00611 * \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 00612 * \param distCoeff The (input) distortion coefficients: [k1, k2, p1, p2]: k1 and k2 (radial) and p1 and p2 (tangential) 00613 */ 00614 void rectifyImageInPlace( const math::CMatrixDouble33 &cameraMatrix, const vector_double &distCoeff ); 00615 00616 /** Filter the image with a Median filter with a window size WxW, returning the filtered image in out_img */ 00617 void filterMedian( CImage &out_img, int W=3 ) const; 00618 00619 /** Filter the image with a Median filter with a window size WxH, replacing "this" image by the filtered one. */ 00620 void filterMedianInPlace( int W=3 ); 00621 00622 /** Filter the image with a Gaussian filter with a window size WxH, returning the filtered image in out_img */ 00623 void filterGaussianInPlace( int W = 3, int H = 3 ); 00624 00625 /** Filter the image with a Gaussian filter with a window size WxH, replacing "this" image by the filtered one. */ 00626 void filterGaussian( CImage &out_img, int W = 3, int H = 3) const; 00627 00628 /** Look for the corners of a chessboard in the image. 00629 * This method uses internally OpenCV functions: 00630 * - cvFindChessboardCorners 00631 * - cvFindCornerSubPix 00632 * 00633 * \param cornerCoords [OUT] The pixel coordinates of all the corners. 00634 * \param check_size_x [IN] The number of squares, in the X direction 00635 * \param check_size_y [IN] The number of squares, in the Y direction 00636 * \param normalize_image [IN] Whether to normalize the image before detection 00637 * 00638 * \return true on success 00639 * 00640 * \sa mrpt::vision::checkerBoardCameraCalibration, drawChessboardCorners 00641 */ 00642 bool findChessboardCorners( 00643 std::vector<TPixelCoordf> &cornerCoords, 00644 unsigned int check_size_x, 00645 unsigned int check_size_y, 00646 bool normalize_image = true ) const; 00647 00648 /** Draw onto this image the detected corners of a chessboard. The length of cornerCoords must be the product of the two check_sizes. 00649 * 00650 * \param cornerCoords [IN] The pixel coordinates of all the corners. 00651 * \param check_size_x [IN] The number of squares, in the X direction 00652 * \param check_size_y [IN] The number of squares, in the Y direction 00653 * 00654 * \return false if the length of cornerCoords is inconsistent (nothing is drawn then). 00655 * 00656 * \sa findChessboardCorners 00657 */ 00658 bool drawChessboardCorners( 00659 std::vector<TPixelCoordf> &cornerCoords, 00660 unsigned int check_size_x, 00661 unsigned int check_size_y ); 00662 00663 /** 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) 00664 * 00665 * \param im1 [IN] The first image. 00666 * \param im2 [IN] The other image. 00667 */ 00668 void joinImagesHorz( 00669 const CImage &im1, 00670 const CImage &im2 ); 00671 00672 }; // End of class 00673 00674 typedef CImage CMRPTImage; //!< Deprecated name. 00675 00676 00677 } // end of namespace utils 00678 00679 } // end of namespace mrpt 00680 00681 #endif
| Page generated by Doxygen 1.6.1 for MRPT 0.9.0 SVN: at Mon Jun 7 06:47:58 UTC 2010 |
