00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CMRPTImage_H
00029 #define CMRPTImage_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/CMRPTCanvas.h>
00035 #include <mrpt/system/os.h>
00036
00037 namespace mrpt
00038 {
00039 namespace utils
00040 {
00042 class CExceptionExternalImageNotFound : public std::logic_error
00043 {
00044 public:
00045 CExceptionExternalImageNotFound(const std::string &s) : std::logic_error(s.c_str()) { }
00046 };
00047
00048
00049 DEFINE_SERIALIZABLE_PRE( CMRPTImage )
00050
00051
00077 class MRPTDLLIMPEXP CMRPTImage : public mrpt::utils::CSerializable, public CMRPTCanvas
00078 {
00079 friend class CMRPTImageFloat;
00080
00081 DEFINE_SERIALIZABLE( CMRPTImage )
00082
00083 protected:
00086 void *img;
00087
00091 bool m_imgIsReadOnly;
00092
00096 mutable bool m_imgIsExternalStorage;
00097 std::string m_externalFile;
00098
00101 void changeSize(
00102 unsigned int width,
00103 unsigned int height,
00104 unsigned int nChannels,
00105 bool originTopLeft );
00106
00108 void releaseIpl() MRPT_NO_THROWS;
00109
00111 void makeSureImageIsLoaded() const throw (std::exception,utils::CExceptionExternalImageNotFound );
00112
00113 public:
00114
00118 static bool DISABLE_ZIP_COMPRESSION;
00119
00124 void resize(
00125 unsigned int width,
00126 unsigned int height,
00127 unsigned int nChannels,
00128 bool originTopLeft )
00129 {
00130 ASSERT_(img!=NULL);
00131 changeSize(width,height,nChannels,originTopLeft);
00132 }
00133
00141 void setPixel(int x, int y, size_t color);
00142
00145 CMRPTImage( );
00148 CMRPTImage( unsigned int width,
00149 unsigned int height,
00150 unsigned int nChannels = 3,
00151 bool originTopLeft = true
00152 );
00155 CMRPTImage( const CMRPTImage &o );
00156
00159 CMRPTImage( const CMRPTImageFloat &o );
00160
00164 void operator = (const CMRPTImage& o);
00165
00169 void operator = (const CMRPTImageFloat& o);
00170
00174 void copyFastFrom( CMRPTImage &o );
00175
00178 CMRPTImage( void *iplImage );
00179
00182 virtual ~CMRPTImage( );
00183
00186 void* getAsIplImage() const;
00187
00191 unsigned char* get_unsafe(
00192 unsigned int col,
00193 unsigned int row,
00194 unsigned int channel=0) const;
00195
00196
00202 float getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const;
00203
00209 float getAsFloat(unsigned int col, unsigned int row) const;
00210
00214 float getMaxAsFloat() const;
00215
00218 size_t getWidth() const;
00219
00222 size_t getHeight() const;
00223
00226 bool isColor() const;
00227
00230 bool isOriginTopLeft() const;
00231
00234 void setOriginTopLeft(bool val);
00235
00238 void loadFromMemoryBuffer( unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue = false );
00239
00243 void loadFromMemoryBuffer( unsigned int width, unsigned int height, unsigned int bytesPerRow, unsigned char *red, unsigned char *green, unsigned char *blue );
00244
00247 void loadFromIplImage( void* iplImage );
00248
00252 void setFromIplImageReadOnly( void* iplImage );
00253
00271 void setExternalStorage( const std::string &fileName ) MRPT_NO_THROWS;
00272
00273 static std::string IMAGES_PATH_BASE;
00274
00276 bool isExternallyStored() const MRPT_NO_THROWS { return m_imgIsExternalStorage; }
00277
00278 std::string getExternalStorageFile() const MRPT_NO_THROWS
00279 {
00280 return m_externalFile;
00281 }
00282
00283 void getExternalStorageFileAbsolutePath(std::string &out_path) const;
00284
00290 void unload() MRPT_NO_THROWS;
00291
00296 MRPT_DEPRECATED_PRE bool loadFromBMP( const std::string& fileName ) MRPT_DEPRECATED_POST;
00297
00302 MRPT_DEPRECATED_PRE bool loadFromJPEG( const std::string& fileName ) MRPT_DEPRECATED_POST;
00303
00307 void loadFromStreamAsJPEG( CStream &in );
00308
00313 MRPT_DEPRECATED_PRE bool saveToBMP( const std::string& fileName ) const MRPT_DEPRECATED_POST;
00314
00320 MRPT_DEPRECATED_PRE bool saveToJPEG( const std::string& fileName )const MRPT_DEPRECATED_POST;
00321
00340 bool loadFromFile( const std::string& fileName, int isColor = 1 );
00341
00357 bool saveToFile( const std::string& fileName ) const;
00358
00363 void saveToStreamAsJPEG( CStream &out )const;
00364
00369 unsigned char* operator()(unsigned int col, unsigned int row, unsigned int channel = 0) const;
00370
00373 CMRPTImage grayscale() const;
00374
00377 void grayscale( CMRPTImage &ret ) const;
00378
00383 CMRPTImage scaleHalf()const;
00384
00389 CMRPTImage scaleDouble()const;
00390
00391
00394 const char * getChannelsOrder()const;
00395
00399 void extract_patch(
00400 CMRPTImage &patch,
00401 const unsigned int col_=0,
00402 const unsigned int row_=0,
00403 const unsigned int col_num=1,
00404 const unsigned int row_num=1 ) const;
00405
00411 float correlate( const CMRPTImage &img2int, int width_init=0, int height_init=0 )const;
00412
00418 void cross_correlation(
00419 CMRPTImage &img2,
00420 math::CMatrixFloat &M,
00421 const int &u_search_ini, const int &v_search_ini,
00422 const int &u_search_size, const int &v_search_size)const;
00423
00438 void cross_correlation_FFT(
00439 const CMRPTImage &in_img,
00440 math::CMatrixFloat &out_corr,
00441 int u_search_ini=-1,
00442 int v_search_ini=-1,
00443 int u_search_size=-1,
00444 int v_search_size=-1,
00445 float biasThisImg = 0,
00446 float biasInImg = 0
00447 ) const;
00448
00457 void getAsMatrix(
00458 math::CMatrix &outMatrix,
00459 bool doResize = true,
00460 int x_min = 0,
00461 int y_min = 0,
00462 int x_max = -1,
00463 int y_max = -1
00464 ) const;
00465
00469 void getAsMatrixTiled( math::CMatrix &outMatrix ) const;
00470
00475 void normalize();
00476
00491 void openCV_cross_correlation(
00492 const CMRPTImage &patch_img,
00493 size_t &u_max,
00494 size_t &v_max,
00495 double &max_val,
00496 int u_search_ini=-1,
00497 int v_search_ini=-1,
00498 int u_search_size=-1,
00499 int v_search_size=-1)const;
00500
00504 void flipVertical(bool also_swapRB = false);
00505
00509 void swapRB();
00510
00516 void rectifyImage( CMRPTImage &out_img, const math::CMatrixFloat &cameraMatrix, const math::CMatrixFloat &distCoeff );
00517
00522 void rectifyImageInPlace( const math::CMatrixFloat &cameraMatrix, const math::CMatrixFloat &distCoeff );
00523
00525 void filterMedian( CMRPTImage &out_img, int W=3 ) const;
00526
00528 void filterMedianInPlace( int W=3 );
00529
00531 void filterGaussianInPlace( int W = 3, int H = 3 );
00532
00534 void filterGaussian( CMRPTImage &out_img, int W = 3, int H = 3) const;
00535
00536 };
00537
00538
00539 }
00540
00541 }
00542
00543 #endif