00001 /*!@file Image/Conversions.H convert between Image and other types 00002 (e.g. std::vector) */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: T Nathan Mundhenk <mundhenk@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Conversions.H $ 00036 // $Id: Conversions.H 4663 2005-06-23 17:47:28Z rjpeters $ 00037 // 00038 00039 #ifndef IMAGE_CONVERSIONS_H_DEFINED 00040 #define IMAGE_CONVERSIONS_H_DEFINED 00041 00042 #include "Util/Assert.H" 00043 #include "Image/Image.H" 00044 00045 #include <vector> 00046 00047 00048 //! Conversion assigment operator. 00049 /*! e.g.: 00050 \code 00051 Image<byte> im1; std::vector im2; vectorToImage(im2, im1); 00052 \endcode 00053 00054 */ 00055 00056 template <class T> 00057 inline Image<T> vectorToImage(const std::vector<std::vector<T> >& A); 00058 00059 template <class T> 00060 inline Image<T> vectorToImage(const std::vector<std::vector<T>* >& A); 00061 00062 template <class T> 00063 inline Image<T> vectorToImage(const std::vector<std::vector<T*> >& A); 00064 00065 //! return a double nested vector that represents data (see also =operator) 00066 template <class T> 00067 inline void getVector(const Image<T>& src, 00068 std::vector<std::vector<T> > *V); 00069 00070 //! return a double nested vector that represents data (see also =operator) 00071 template <class T> 00072 inline void getVector(const Image<T>& src, 00073 std::vector<std::vector<T*> > *V); 00074 00075 //! return a single row of an image as a vector 00076 template <class T> 00077 inline void getVectorRow(const Image<T>& src, 00078 std::vector<T> *V, int row); 00079 00080 //! return a single row of an image as a vector 00081 template <class T> 00082 inline void getVectorRow(const Image<T>& src, 00083 std::vector<T*> *V, int row); 00084 00085 //! return a single column of an image as a vector 00086 template <class T> 00087 inline void getVectorColumn(const Image<T>& src, 00088 std::vector<T> *V, int column); 00089 00090 //! return a single column of an image as a vector 00091 template <class T> 00092 inline void getVectorColumn(const Image<T>& src, 00093 std::vector<T*> *V, int column); 00094 00095 // ###################################################################### 00096 // ##### Inline function definitions 00097 // ###################################################################### 00098 00099 // ###################################################################### 00100 template <class T> inline 00101 Image<T> vectorToImage(const std::vector<std::vector<T> >& src) 00102 { 00103 Image<T> dst((signed)src[0].size(),(signed)src.size(), NO_INIT); 00104 00105 typename Image<T>::iterator aptr = dst.beginw(); 00106 typename std::vector<std::vector<T> >::const_iterator vptr = src.begin(); 00107 typename std::vector<std::vector<T> >::const_iterator vstp = src.end(); 00108 while(vptr != vstp) 00109 { 00110 typename std::vector<T>::const_iterator vvptr = vptr->begin(); 00111 typename std::vector<T>::const_iterator vvstp = vptr->end(); 00112 while(vvptr != vvstp) 00113 *aptr++ = *vvptr++; 00114 ++vptr; 00115 } 00116 00117 return dst; 00118 } 00119 00120 // ###################################################################### 00121 template <class T> inline 00122 Image<T> vectorToImage(const std::vector<std::vector<T>* >& src) 00123 { 00124 Image<T> dst((signed)src[0]->size(),(signed)src.size(), NO_INIT); 00125 00126 typename Image<T>::iterator aptr = dst.beginw(); 00127 for(int ii = 0; ii < (signed)src.size(); ii++) 00128 { 00129 //std::vector<T>* temp = src[ii]; 00130 typename std::vector<T>::iterator vvptr = src[ii]->begin(); 00131 typename std::vector<T>::iterator vvstp = src[ii]->end(); 00132 while(vvptr != vvstp) 00133 *aptr++ = *vvptr++; 00134 } 00135 00136 return dst; 00137 } 00138 00139 // ###################################################################### 00140 template <class T> inline 00141 Image<T> vectorToImage(const std::vector<std::vector<T*> >& src) 00142 { 00143 Image<T> dst((signed)src[0].size(),(signed)src.size(), NO_INIT); 00144 00145 typename Image<T>::iterator aptr = dst.beginw(); 00146 typename std::vector<std::vector<T*> >::const_iterator vptr = src.begin(); 00147 typename std::vector<std::vector<T*> >::const_iterator vstp = src.end(); 00148 while(vptr != vstp) 00149 { 00150 typename std::vector<T*>::iterator vvptr = vptr->begin(); 00151 typename std::vector<T*>::iterator vvstp = vptr->end(); 00152 while(vvptr != vvstp) 00153 *aptr++ = **vvptr++; 00154 ++vptr; 00155 } 00156 00157 return dst; 00158 } 00159 00160 // ###################################################################### 00161 template <class T> inline 00162 void getVector(const Image<T>& src, std::vector<std::vector<T> > *V) 00163 { 00164 ASSERT(V->size() >= (unsigned int)src.getHeight()); 00165 ASSERT(V->at(0).size() >= (unsigned int)src.getWidth()); 00166 typename Image<T>::const_iterator aptr = src.begin(); 00167 typename std::vector<std::vector<T> >::iterator vptr = V->begin(); 00168 typename std::vector<std::vector<T> >::iterator vstp = V->end(); 00169 while(vptr != vstp) 00170 { 00171 typename std::vector<T>::iterator vvptr = vptr->begin(); 00172 typename std::vector<T>::iterator vvstp = vptr->end(); 00173 while(vvptr != vvstp) 00174 *vvptr++ = *aptr++; 00175 } 00176 } 00177 00178 // ###################################################################### 00179 template <class T> inline 00180 void getVector(const Image<T>& src, std::vector<std::vector<T*> > *V) 00181 { 00182 ASSERT(V->size() >= (unsigned int)src.getHeight()); 00183 ASSERT(V->at(0).size() >= (unsigned int)src.getWidth()); 00184 typename Image<T>::const_iterator aptr = src.begin(); 00185 typename std::vector<std::vector<T*> >::iterator vptr = V->begin(); 00186 typename std::vector<std::vector<T*> >::iterator vstp = V->end(); 00187 while(vptr != vstp) 00188 { 00189 typename std::vector<T*>::iterator vvptr = vptr->begin(); 00190 typename std::vector<T*>::iterator vvstp = vptr->end(); 00191 while(vvptr != vvstp) 00192 **vvptr++ = *aptr++; 00193 } 00194 } 00195 00196 // ###################################################################### 00197 template <class T> inline 00198 void getVectorRow(const Image<T>& src, std::vector<T> *V, int row) 00199 { 00200 ASSERT(V->size() >= (unsigned int)src.getWidth()); 00201 typename Image<T>::const_iterator aptr = 00202 src.begin() + (row * src.getWidth()); 00203 typename std::vector<T>::iterator vptr = V->begin(); 00204 typename std::vector<T>::iterator vstp = V->end(); 00205 while(vptr != vstp) 00206 { 00207 *vptr++ = *aptr++; 00208 } 00209 } 00210 00211 // ###################################################################### 00212 template <class T> inline 00213 void getVectorRow(const Image<T>& src, std::vector<T*> *V, int row) 00214 { 00215 ASSERT(V->size() >= (unsigned int)src.getWidth()); 00216 typename Image<T>::const_iterator aptr = 00217 src.begin() + (row * src.getWidth()); 00218 00219 typename std::vector<T*>::iterator vptr = V->begin(); 00220 typename std::vector<T*>::iterator vstp = V->end(); 00221 while(vptr != vstp) 00222 { 00223 **vptr++ = *aptr++; 00224 } 00225 } 00226 00227 // ###################################################################### 00228 template <class T> inline 00229 void getVectorColumn(const Image<T>& src, std::vector<T> *V, int column) 00230 { 00231 ASSERT(V->size() >= (unsigned int)src.getHeight()); 00232 typename std::vector<T>::iterator vptr = V->begin(); 00233 typename std::vector<T>::iterator vstp = V->end(); 00234 int current = 0; 00235 while(vptr != vstp) 00236 { 00237 *vptr++ = src.begin()[column + current*src.getWidth()]; 00238 current++; 00239 } 00240 } 00241 00242 // ###################################################################### 00243 template <class T> inline 00244 void getVectorColumn(const Image<T>& src, std::vector<T*> *V, int column) 00245 { 00246 ASSERT(V->size() >= (unsigned int)src.getHeight()); 00247 typename std::vector<T*>::iterator vptr = V->begin(); 00248 typename std::vector<T*>::iterator vstp = V->end(); 00249 int current = 0; 00250 while(vptr != vstp) 00251 { 00252 **vptr++ = src.begin()[column + current*src.getWidth()]; 00253 current++; 00254 } 00255 } 00256 00257 00258 #endif // !IMAGE_CONVERSIONS_H_DEFINED