rtcImage.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: PUMA: Probablistic Unsupervised Model Acquisition
00011  * file .......: Image.h
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 10/13/2006
00015  * modified ...: $Date: 2009-03-11 10:01:02 -0700 (Wed, 11 Mar 2009) $
00016  * changed by .: $Author: wg75pal $
00017  * revision ...: $Revision: 825 $
00018  */
00019 #ifndef IMAGE_H
00020 #define IMAGE_H
00021 
00022 //== INCLUDES ==================================================================
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 //== NAMESPACES ================================================================
00031 namespace rtc {
00032 
00033 //== CLASS DEFINITION ==========================================================
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   // c'tor
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   // d'tor
00052   virtual ~Image(){}
00053 
00054   // scaling/resizing operators
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   // file i/o
00060   bool readFromFile(const char* filename);
00061   bool writeToFile(const char* filename) const;
00062 
00063   // convert to/from openCV cv::Mat
00064   bool fromOpenCV(const cv::Mat& image);
00065   bool toOpenCV(cv::Mat& image) const;
00066 
00067   // stream IO
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   // Assignment operator
00074   Image<T>& operator = (const Image<T>& img);
00075 
00076   // Image Process
00077   // resize() can do that only slower
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 // Declare a few common typedefs
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   // fill every pixel so we don't have to worry about skipping pixels later
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   A B C
00137   E F G
00138   H I J
00139 
00140   pixels A C H J are pixels from original image
00141   pixels B E G I F are interpolated pixels
00142 
00143   */
00144 
00145   // interpolate pixels B and I
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   // interpolate pixels E and G
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   // interpolate pixel F
00156   // interpolate pixels E and G
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 // stream IO
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 // Assignment operator
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 } // namespace rtc
00227 //==============================================================================
00228 #endif //IMAGE_H


rtc
Author(s): Benjamin Pitzer
autogenerated on Thu Jan 2 2014 11:04:53