The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

integralimage.h

Go to the documentation of this file.
00001 #ifndef _INTEGRALIMAGE_H_
00002 #define _INTEGRALIMAGE_H_
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 
00007 #ifndef ASSERT
00008 #ifdef _DEBUG
00009 #include <assert.h>
00010 #define ASSERT(x) assert(x)
00011 #else
00012 #define ASSERT(x) 
00013 #endif
00014 #endif
00015 
00016 /* ================================================================ */
00017 
00018 template <class T>
00019 class TImage 
00020 {
00021 protected:
00022         T *array;
00023         int width;
00024         int height;
00025         int numpixels;
00026         int my_memory;
00027 
00028 public: 
00029         TImage (const T *array, const int, const int);
00030         TImage (const int, const int);
00031         TImage (const int, const int, T);
00032         TImage (const TImage &other){
00033                 width = other.width;
00034                 height = other.height;
00035                 numpixels = other.numpixels;
00036                 array = other.array;
00037         }
00038         TImage (){};
00039         inline void setSize(const int width_, const int height_){
00040                 width = width_;
00041                 height = height_;
00042                 numpixels = width * height;
00043                 array = new T[numpixels];
00044                 memset(array,0,sizeof(T)*numpixels);
00045                 my_memory = 1;
00046         }
00047         inline void deleteArray(){
00048                 delete [] array;
00049         }
00050         
00051         virtual ~TImage ( );
00052         
00053         virtual T* getArray(){return array;}
00054         
00055         void print ( ) const;
00056         void print ( const int m ) const;
00057 };
00058 
00059 /* ================================================================ */
00060 
00061 template <class T>
00062 inline TImage<T>::TImage (const T *array_, const int width_, const int height_ )
00063 {
00064         width = width_;
00065         height = height_;
00066         numpixels = width * height;
00067         array = const_cast<T *>(array_);
00068         my_memory = 0;
00069 }
00070 
00071 /* ================================================================ */
00072 
00073 template <class T>
00074 inline TImage<T>::TImage ( const int width_, const int height_ )
00075 {
00076         setSize(width_,height_);
00077 }
00078 
00079 /* ================================================================ */
00080 
00081 template <class T>
00082 inline TImage<T>::TImage ( const int width_, const int height_ , const T val)
00083 {
00084         setSize(width_,height_);
00085         T *ptr = array;
00086         for(int y=0; y <height_; y++)
00087                 for(int x=0; x <width_; x++)    
00088                         *(ptr++) = val; 
00089 }
00090 
00091 /* ================================================================ */
00092 
00093 template <class T>
00094 inline TImage<T>::~TImage ()
00095 {
00096         if(my_memory)
00097                 delete [] array;
00098 }
00099 
00100 /* ================================================================ */
00101 
00102 template <class T>
00103 void TImage<T>::print ( ) const 
00104 {
00105         for(int y = 0; y < height; y++) {
00106                 for(int x = 0; x < width; x++) {
00107                         cout << getPixel(x,y) << " ";
00108                 }
00109                 cout << endl;
00110         }
00111 }
00112 
00113 /* ================================================================ */
00114 
00115 template <class T>
00116 void TImage<T>::print ( const int m ) const 
00117 {
00118         for(int y = 0; y < min(m,height); y++) {
00119                 for(int x = 0; x < min(m,width); x++) {
00120                         cout << getPixel(x,y) << " ";
00121                 }
00122                 cout << endl;
00123         }
00124 }
00125 
00126 /* ================================================================ */
00127 
00128 template <class T>
00129 class TIntegral : public TImage<T>
00130 {
00131         
00132 public:
00133         const int getImWidth() const {return width -1;  }
00134         const int getImHeight() const {return height -1;        }
00135         const int getIntWidth() const {return width;    }
00136         const int getIntHeight() const {return height;  }
00137         
00138         const int getImNumPix() const { return getImWidth()*getImHeight();}
00139         const int getIntNumPix() const { return getIntWidth()*getIntHeight();}
00140         
00141         
00142         TIntegral(const int width, const int height){
00143                 setSize(width,height);
00144         }
00145   void setSize(const int width, const int height){
00146     TImage<T>::setSize(width+1,height+1);
00147         }
00148         TIntegral() : TImage<T>() {
00149         }
00150 
00151         /* integrate in place */
00152         void integrate() { 
00153                 int x, y;
00154                 T * p = array;
00155 //              for(int x = 0; x < width; ++x) setIntPixel(x,0,0);   /* create top row of zeros */
00156 //              for(int y = 0; y < width; ++y) setIntPixel(0,y,0);   /* create left column of zeros */
00157 
00158                 for(x = 1; x < width; ++x) {
00159                         double v0 = getIntPixel(x-1,0);
00160                         double v1 = getIntPixel(x,0);
00161                         setIntPixel(x,0,v0+v1);   /* integrate top row */
00162                 }
00163 
00164                 for(y = 1; y < height; ++y) {
00165                         double v0 = getIntPixel(0,y-1);
00166                         double v1 = getIntPixel(0,y);
00167                         setIntPixel(0,y,v0+v1);   /* create left column of zeros */
00168     }
00169 
00170                 for(y = 1; y < height; ++y){
00171                         for(x = 1; x < width; ++x){
00172                                 T val = getIntPixel(x,y) + getIntPixel(x-1, y) +
00173                                         getIntPixel(x, y-1) - getIntPixel(x-1, y-1);
00174                           setIntPixel(x,y,val);
00175                         }
00176                 }
00177         }
00178         
00179         inline  T getIntPixel (int x, int y) const{
00180                 int lx = x, ly = y;
00181                 if(lx < 0 || ly < 0) return 0;
00182                 
00183                 if(lx >= width) lx = width - 1;
00184                 if(ly >= height) ly = height - 1;
00185                 
00186                 int index = width * ly + lx;
00187                 ASSERT (index >= 0 && index < numpixels);
00188                 return array[index];
00189         };
00190         
00191         
00192         inline  void setIntPixel ( const int x, const int y, const T &value )
00193         {
00194                 int lx = x, ly = y;
00195                 
00196                 ASSERT( lx >= 0 && ly >=0 && lx < width && ly < height); 
00197                 if (lx < 0) lx = 0;
00198                 if (ly < 0) ly = 0;
00199                 if(lx >= width) lx = width - 1;
00200                 if(ly >= height) ly = height - 1;
00201 
00202                 int index = width * ly + lx;
00203                 ASSERT (index >= 0 && index < numpixels);
00204                 array[index] = value;
00205                 
00206         }
00207         inline  T getImPixel (int x, int y) const{
00208                 return getIntPixel(x+1,y+1);
00209         };
00210         
00211         
00212         inline  void setImPixel ( const int x, const int y, const T &value )
00213         {
00214                 setIntPixel(x+1,y+1,value);
00215         };
00216         
00217         void printIm(const char *filename) {
00218                 FILE *fid = fopen(filename, "w");
00219                 int z= 12003;
00220                 for (int y = 0; y < getImHeight(); y++) {
00221                         for (int x = 0; x < getImWidth(); x++)
00222                                 fprintf(fid, "%g\t", getImPixel(x, y));
00223                         fprintf(fid, "\n");
00224                 }
00225                 fclose(fid);
00226         }
00227 
00228         void printInt(const char *filename) {
00229                 FILE *fid = fopen(filename, "w");
00230                 for (int y = 0; y < getIntHeight(); y++) {
00231                         for (int x = 0; x < getIntWidth(); x++)
00232                                 fprintf(fid, "%g\t", getIntPixel(x, y));
00233                         fprintf(fid, "\n");
00234                 }
00235                 fclose(fid);
00236         }
00237 
00238         void printImBin(const char *filename, bool flag) {
00239                 std::ofstream os(filename, ios::binary | ios::app);
00240                 if (flag) {
00241                         os.flags(ios::binary);
00242                         int width = getImWidth();
00243                         int height = getImHeight();
00244                         os.write((char *)&width, sizeof(width));
00245                         os.write((char *)&height, sizeof(height));
00246                 }
00247                 for (int y = 0; y < getImHeight(); y++) {
00248                         for (int x = 0; x < getImWidth(); x++) {
00249                                 T val = (T)(getImPixel(x, y));
00250                                 os.write((char *)&val, sizeof(val));
00251                         }
00252                 }
00253                 os.close();
00254         }
00255 
00256         void printIntBin(const char *filename, bool flag) {
00257                 std::ofstream os(filename, ios::binary | ios::app);
00258                 if (flag) {
00259                         os.flags(ios::binary);
00260                         int width = getIntWidth();
00261                         int height = getIntHeight();
00262                         os.write((char *)&width, sizeof(width));
00263                         os.write((char *)&height, sizeof(height));
00264                 }
00265                 for (int y = 0; y < getIntHeight(); y++) {
00266                         for (int x = 0; x < getIntWidth(); x++) {
00267                                 T val = (T)(getIntPixel(x, y));
00268                                 os.write((char *)&val, sizeof(val));
00269                         }
00270                 }
00271                 os.close();
00272         }
00273 
00274         void setToMinLog () {
00275           static TImage<T> cache(getIntWidth(), getIntHeight(), -1000);
00276                 T *ptr = array;
00277                 memcpy(ptr, cache.getArray(), sizeof(T)*getIntNumPix());
00278         }
00279 
00280 };
00281 
00282 /* ================================================================ */
00283 
00284 #endif  _INTEGRALIMAGE_H_

Generated on Mon Nov 8 17:07:36 2004 for MPT by  doxygen 1.3.9.1