00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef IMAGE_H
00020 #define IMAGE_H
00021
00022
00023 #include <opencv2/core/core.hpp>
00024 #include <opencv2/highgui/highgui.hpp>
00025 #include <cstdio>
00026 #include <rtc/rtcArray2.h>
00027 #include <rtc/rtcVec2.h>
00028 #include <rtc/rtcVec3.h>
00029 #include <rtc/rtcVec4.h>
00030
00031 namespace rtc {
00032
00033
00037 template<typename T>
00038 class Image : public Array2<T> {
00039 public:
00040 typedef T Pixel;
00041 using Array2<T>::columns;
00042 using Array2<T>::rows;
00043 using Array2<T>::at;
00044 using Array2<T>::set;
00045 using Array2<T>::x;
00046
00047
00048 Image():Array2<Pixel>(1,1){}
00049 Image(int _rows, int _columns):Array2<Pixel>(_rows,_columns){}
00050 Image(const Image& other):Array2<Pixel>(other){}
00051
00052 virtual ~Image(){}
00053
00054
00055 Pixel interpolate(const float row, const float col) const;
00056 bool resize(int rows, int columns);
00057 bool resized(Image& dest) const;
00058
00059
00060 bool readFromFile(const char* filename);
00061 bool writeToFile(const char* filename) const;
00062
00063
00064 bool fromOpenCV(const cv::Mat& image);
00065 bool toOpenCV(cv::Mat& image) const;
00066
00067
00068 virtual bool write(OutputHandler& oh) const;
00069 virtual bool read(InputHandler& ih);
00070 void write(FILE *fp) const;
00071 void read(FILE *fp);
00072
00073
00074 Image<T>& operator = (const Image<T>& img);
00075
00076
00077
00078 void halfImageTo(Image<T> &other);
00079 void halfImage(){Image<T> img;halfImageTo(img);*this=img;}
00080 void doubleImageTo(Image<T> &other);
00081 void doubleImage(){Image<T> img;doubleImageTo(img);*this=img;}
00082 void doubleImage2To(Image<T> &other);
00083 void doubleImage2(){Image<T> img;doubleImage2To(img);*this=img;}
00084 void operator-=(const Image<T> &other){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)-=other.at(i,j);}
00085 void operator*=(const Image<T> &other){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)*=other.at(i,j);}
00086 void operator+=(const Image<T> &other){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)+=other.at(i,j);}
00087 void operator/=(const Image<T> &other){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)/=other.at(i,j);}
00088 void operator*=(const T &v){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)*=v;}
00089 void operator-=(const T &v){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)-=v;}
00090 void operator+=(const T &v){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)+=v;}
00091 void operator/=(const T &v){for (int i=0;i<rows();i++) for (int j=0;j<columns();j++) at(i,j)/=v;}
00092 };
00093
00094
00095 typedef Image<Vec4uc> Image4uc;
00096 typedef Image<Vec3uc> Image3uc;
00097 typedef Image<Vec3f> Image3f;
00098 typedef Image<unsigned char> Imageuc;
00099
00100 template<typename T>
00101 inline void Image<T>::halfImageTo( Image<T> &other )
00102 {
00103 int row=rows()/2;
00104 int column=columns()/2;
00105
00106 other.setSize(row,column);
00107
00108 for (int i=0;i<row;i++) for (int j=0;j<column;j++)
00109 other.at(i,j)=at(i*2,j*2);
00110 }
00111
00112 template<typename T>
00113 inline void Image<T>::doubleImageTo( Image<T> &other )
00114 {
00115 int row=rows()*2;
00116 int column=columns()*2;
00117
00118 other.setSize(row,column);
00119
00120 for (int i=0;i<row;i++) for (int j=0;j<column;j++)
00121 other.at(i,j)=at(i/2,j/2);
00122 }
00123
00124 template<typename T>
00125 inline void Image<T>::doubleImage2To( Image<T> &other )
00126 {
00127 int row=rows()*2;
00128 int column=columns()*2;
00129
00130 other.setSize(row,column);
00131
00132
00133 for (int i = 0; i < row; i++) for (int j = 0; j < column; j++)
00134 other.at(i, j)= at(i/2, j/2);
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 for (int i = 0; i < row; i += 2) for (int j = 1; j < column - 1; j += 2)
00147 other.at(i, j)= (at(i/2, j/2) + at(i/2 + 1, j/2)) / 2.0;
00148
00149
00150
00151 for (int i = 1; i < row - 1; i += 2) for (int j = 0; j < column; j += 2)
00152 other.at(i, j)= (at(i/2, j/2) + at(i/2, j/2 + 1)) / 2.0;
00153
00154
00155
00156
00157 for (int i = 1; i < row - 1; i += 2) for (int j = 1; j < column - 1; j += 2)
00158 other.at(i, j)= (at(i/2, j/2) + at(i/2, j/2 + 1) + at(i/2 + 1, j/2) + at(i/2 + 1, j/2 + 1)) / 4.0;
00159 }
00160
00161 template<typename T>
00162 inline bool Image<T>::resize(int _rows, int _columns)
00163 {
00164 Image dest(_rows,_columns);
00165 resized(dest);
00166 set(dest);
00167 return true;
00168 }
00169
00170 template<typename T>
00171 inline bool Image<T>::resized(Image& dest) const
00172 {
00173 int x,y;
00174 float fx,fy;
00175 const float dx = static_cast<float>(columns()-1)/(dest.columns()-1);
00176 const float dy = static_cast<float>(rows()-1)/(dest.rows()-1);
00177 for (fy=0.0,y=0;y<dest.rows();++y,fy+=dy) for (fx=0.0,x=0;x<dest.columns();++x,fx+=dx)
00178 dest.at(y,x) = interpolate(fy,fx);
00179 return true;
00180 }
00181
00182
00183 template<typename T>
00184 inline bool Image<T>::write(OutputHandler& oh) const
00185 {
00186 Array2<Pixel>::write(oh);
00187 return true;
00188 }
00189
00190 template<typename T>
00191 inline bool Image<T>::read(InputHandler& ih)
00192 {
00193 Array2<Pixel>::read(ih);
00194 return true;
00195 }
00196
00197 template<typename T>
00198 inline void Image<T>::write(FILE *fp) const
00199 {
00200 int nrow=rows(),ncol=columns();
00201 fwrite(&nrow,sizeof(nrow),1,fp);
00202 fwrite(&ncol,sizeof(ncol),1,fp);
00203 fwrite(x,sizeof(Pixel),nrow*ncol,fp);
00204 }
00205
00206 template<typename T>
00207 inline void Image<T>::read(FILE *fp)
00208 {
00209 size_t res = 0;
00210 int nrow,ncol;
00211 res+=fread(&nrow,sizeof(nrow),1,fp);
00212 res+=fread(&ncol,sizeof(ncol),1,fp);
00213 this->setSize(nrow,ncol);
00214 res+=fread(x,sizeof(Pixel),nrow*ncol,fp);
00215 }
00216
00217
00218 template<typename T>
00219 inline Image<T>& Image<T>::operator = (const Image<T>& img)
00220 {
00221 set(img);
00222 return *this;
00223 }
00224
00225
00226 }
00227
00228 #endif //IMAGE_H