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 #include <mrpt/utils/exceptions.h>
00037
00038 namespace mrpt
00039 {
00040 namespace utils
00041 {
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
00053 DEFINE_SERIALIZABLE_PRE( CMRPTImage )
00054
00055
00082 class MRPTDLLIMPEXP CMRPTImage : public mrpt::utils::CSerializable, public CMRPTCanvas
00083 {
00084 friend class CMRPTImageFloat;
00085
00086 DEFINE_SERIALIZABLE( CMRPTImage )
00087
00088 protected:
00091 void *img;
00092
00096 bool m_imgIsReadOnly;
00097
00101 mutable bool m_imgIsExternalStorage;
00102 std::string m_externalFile;
00103
00106 void changeSize(
00107 unsigned int width,
00108 unsigned int height,
00109 unsigned int nChannels,
00110 bool originTopLeft );
00111
00113 void releaseIpl() MRPT_NO_THROWS;
00114
00116 void makeSureImageIsLoaded() const throw (std::exception,utils::CExceptionExternalImageNotFound );
00117
00118 void rectifyImage_internal( CMRPTImage &out_img, const math::CMatrixDouble &cameraMatrix, const vector_double &distCoeff ) const;
00119 void rectifyImageInPlace_internal( const math::CMatrixDouble &cameraMatrix, const vector_double &distCoeff );
00120
00121 public:
00122
00126 static bool DISABLE_ZIP_COMPRESSION;
00127
00133 void resize(
00134 unsigned int width,
00135 unsigned int height,
00136 unsigned int nChannels,
00137 bool originTopLeft )
00138 {
00139 ASSERT_(img!=NULL);
00140 changeSize(width,height,nChannels,originTopLeft);
00141 }
00142
00143
00147 void scaleImage( unsigned int width, unsigned int height, TInterpMethod interp = IMG_INTERP_CUBIC );
00148
00156 void setPixel(int x, int y, size_t color);
00157
00160 CMRPTImage( );
00163 CMRPTImage( unsigned int width,
00164 unsigned int height,
00165 unsigned int nChannels = 3,
00166 bool originTopLeft = true
00167 );
00170 CMRPTImage( const CMRPTImage &o );
00171
00174 CMRPTImage( const CMRPTImageFloat &o );
00175
00179 void operator = (const CMRPTImage& o);
00180
00184 void operator = (const CMRPTImageFloat& o);
00185
00189 void copyFastFrom( CMRPTImage &o );
00190
00193 CMRPTImage( void *iplImage );
00194
00197 virtual ~CMRPTImage( );
00198
00201 void* getAsIplImage() const;
00202
00206 unsigned char* get_unsafe(
00207 unsigned int col,
00208 unsigned int row,
00209 unsigned int channel=0) const;
00210
00211
00217 float getAsFloat(unsigned int col, unsigned int row, unsigned int channel) const;
00218
00224 float getAsFloat(unsigned int col, unsigned int row) const;
00225
00229 float getMaxAsFloat() const;
00230
00233 size_t getWidth() const;
00234
00237 size_t getHeight() const;
00238
00241 bool isColor() const;
00242
00245 bool isOriginTopLeft() const;
00246
00249 void setOriginTopLeft(bool val);
00250
00253 void loadFromMemoryBuffer( unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue = false );
00254
00258 void loadFromMemoryBuffer( unsigned int width, unsigned int height, unsigned int bytesPerRow, unsigned char *red, unsigned char *green, unsigned char *blue );
00259
00262 void loadFromIplImage( void* iplImage );
00263
00267 void setFromIplImageReadOnly( void* iplImage );
00268
00286 void setExternalStorage( const std::string &fileName ) MRPT_NO_THROWS;
00287
00288 static std::string IMAGES_PATH_BASE;
00289
00291 bool isExternallyStored() const MRPT_NO_THROWS { return m_imgIsExternalStorage; }
00292
00293 std::string getExternalStorageFile() const MRPT_NO_THROWS
00294 {
00295 return m_externalFile;
00296 }
00297
00298 void getExternalStorageFileAbsolutePath(std::string &out_path) const;
00299
00305 void unload() MRPT_NO_THROWS;
00306
00311 MRPT_DEPRECATED_PRE bool loadFromBMP( const std::string& fileName ) MRPT_DEPRECATED_POST;
00312
00317 MRPT_DEPRECATED_PRE bool loadFromJPEG( const std::string& fileName ) MRPT_DEPRECATED_POST;
00318
00322 void loadFromStreamAsJPEG( CStream &in );
00323
00328 MRPT_DEPRECATED_PRE bool saveToBMP( const std::string& fileName ) const MRPT_DEPRECATED_POST;
00329
00335 MRPT_DEPRECATED_PRE bool saveToJPEG( const std::string& fileName )const MRPT_DEPRECATED_POST;
00336
00355 bool loadFromFile( const std::string& fileName, int isColor = 1 );
00356
00372 bool saveToFile( const std::string& fileName ) const;
00373
00378 void saveToStreamAsJPEG( CStream &out )const;
00379
00384 unsigned char* operator()(unsigned int col, unsigned int row, unsigned int channel = 0) const;
00385
00388 CMRPTImage grayscale() const;
00389
00392 void grayscale( CMRPTImage &ret ) const;
00393
00398 CMRPTImage scaleHalf()const;
00399
00404 CMRPTImage scaleDouble()const;
00405
00406
00409 const char * getChannelsOrder()const;
00410
00414 void extract_patch(
00415 CMRPTImage &patch,
00416 const unsigned int col_=0,
00417 const unsigned int row_=0,
00418 const unsigned int col_num=1,
00419 const unsigned int row_num=1 ) const;
00420
00426 float correlate( const CMRPTImage &img2int, int width_init=0, int height_init=0 )const;
00427
00433 void cross_correlation(
00434 CMRPTImage &img2,
00435 math::CMatrixFloat &M,
00436 const int &u_search_ini, const int &v_search_ini,
00437 const int &u_search_size, const int &v_search_size)const;
00438
00453 void cross_correlation_FFT(
00454 const CMRPTImage &in_img,
00455 math::CMatrixFloat &out_corr,
00456 int u_search_ini=-1,
00457 int v_search_ini=-1,
00458 int u_search_size=-1,
00459 int v_search_size=-1,
00460 float biasThisImg = 0,
00461 float biasInImg = 0
00462 ) const;
00463
00472 void getAsMatrix(
00473 math::CMatrix &outMatrix,
00474 bool doResize = true,
00475 int x_min = 0,
00476 int y_min = 0,
00477 int x_max = -1,
00478 int y_max = -1
00479 ) const;
00480
00484 void getAsMatrixTiled( math::CMatrix &outMatrix ) const;
00485
00490 void normalize();
00491
00506 void openCV_cross_correlation(
00507 const CMRPTImage &patch_img,
00508 size_t &u_max,
00509 size_t &v_max,
00510 double &max_val,
00511 int u_search_ini=-1,
00512 int v_search_ini=-1,
00513 int u_search_size=-1,
00514 int v_search_size=-1)const;
00515
00519 void flipVertical(bool also_swapRB = false);
00520
00524 void swapRB();
00525
00531 template <class T1, class T2>
00532 void rectifyImage( CMRPTImage &out_img, const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const math::CMatrixTemplateNumeric<T2> &distCoeff ) const
00533 {
00534 math::CMatrixDouble K( cameraMatrix );
00535 vector_double D;
00536 distCoeff.extractRow(0,D);
00537 rectifyImage_internal(out_img,K,D);
00538 }
00539
00544 template <class T1, class T2>
00545 void rectifyImageInPlace( const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const math::CMatrixTemplateNumeric<T2> &distCoeff )
00546 {
00547 math::CMatrixDouble K( cameraMatrix );
00548 vector_double D;
00549 distCoeff.extractRow(0,D);
00550 rectifyImageInPlace_internal(K,D);
00551 }
00552
00558 template <class T1, class T2>
00559 void rectifyImage( CMRPTImage &out_img, const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const std::vector<T2> &distCoeff ) const
00560 {
00561 math::CMatrixDouble K( cameraMatrix );
00562 vector_double D(distCoeff.size());
00563 for (size_t i=0;i<distCoeff.size();i++) D[i]=static_cast<double>(distCoeff[i]);
00564 rectifyImage_internal(out_img,K,D);
00565 }
00566
00571 template <class T1, class T2>
00572 void rectifyImageInPlace( const math::CMatrixTemplateNumeric<T1> &cameraMatrix, const std::vector<T2> &distCoeff )
00573 {
00574 math::CMatrixDouble K( cameraMatrix );
00575 vector_double D(distCoeff.size());
00576 for (size_t i=0;i<distCoeff.size();i++) D[i]=static_cast<double>(distCoeff[i]);
00577 rectifyImageInPlace_internal(K,D);
00578 }
00579
00581 void filterMedian( CMRPTImage &out_img, int W=3 ) const;
00582
00584 void filterMedianInPlace( int W=3 );
00585
00587 void filterGaussianInPlace( int W = 3, int H = 3 );
00588
00590 void filterGaussian( CMRPTImage &out_img, int W = 3, int H = 3) const;
00591
00592 };
00593
00594
00595 }
00596
00597 }
00598
00599 #endif