latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiBilinearInterpolator.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 12.6.2001 00030 * revisions ..: $Id: ltiBilinearInterpolator.h,v 1.9 2006/02/07 18:30:10 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_BILINEAR_INTERPOLATOR_H_ 00034 #define _LTI_BILINEAR_INTERPOLATOR_H_ 00035 00036 #include "ltiImage.h" 00037 #include "ltiVector.h" 00038 #include "ltiScalarValuedInterpolation.h" 00039 00040 namespace lti { 00041 /** 00042 * This functor use bilinear interpolation to approximate values 00043 * between the pixels or elements of vectors and matrices. 00044 * 00045 * The type T of the template is the type of the elements of the vector 00046 * or matrix used. 00047 */ 00048 template <class T> 00049 class bilinearInterpolator : public scalarValuedInterpolation<T> { 00050 public: 00051 typedef typename scalarValuedInterpolation<T>::parameters parameters; 00052 00053 /** 00054 * default constructor 00055 */ 00056 bilinearInterpolator(); 00057 00058 /** 00059 * Construct an interpolator with the given boundary type 00060 */ 00061 bilinearInterpolator(const eBoundaryType boundaryType); 00062 00063 /** 00064 * copy constructor 00065 * @param other the object to be copied 00066 */ 00067 bilinearInterpolator(const bilinearInterpolator<T>& other); 00068 00069 /** 00070 * destructor 00071 */ 00072 virtual ~bilinearInterpolator(); 00073 00074 /** 00075 * returns the name of this type ("bilinearInterpolator") 00076 */ 00077 virtual const char* getTypeName() const; 00078 00079 /** 00080 * Returns the interpolated value of the vector at the real valued 00081 * position x. 00082 * @param src vector<T> with the source data. 00083 * @param x the real valued position to be interpolated. 00084 * @return the interpolated value of the vector. 00085 */ 00086 T apply(const vector<T>& src,const float& x) const; 00087 00088 /** 00089 * Returns the interpolated value of the vector specified with 00090 * use() at the real valued position x. 00091 * @param x the real valued position to be interpolated. 00092 * @return the interpolated value of the vector. */ 00093 T apply(const float& x) const; 00094 00095 /** 00096 * Returns the interpolated value of the matrix at the real valued 00097 * position (row,col). 00098 * 00099 * @param src matrix<T> with the source data. 00100 * @param row which row 00101 * @param col which column 00102 * @return the interpolated value of the matrix. 00103 */ 00104 T apply(const matrix<T>& src, 00105 const float& row, 00106 const float& col) const; 00107 00108 /** 00109 * Returns the interpolated value of the matrix at the real valued 00110 * position p. 00111 * 00112 * @param src matrix<T> with the source data. 00113 * @param p the real valued position to be interpolated. 00114 * @return the interpolated value of the matrix. 00115 */ 00116 T apply(const matrix<T>& src,const tpoint<float>& p) const; 00117 00118 /** 00119 * Returns the interpolated value of the matrix specified with 00120 * use() at the real valued position (row,col). 00121 * 00122 * @param row which row 00123 * @param col which column 00124 * @return the interpolated value of the matrix. */ 00125 T apply(const float& row, const float& col) const; 00126 00127 /** 00128 * Returns the interpolated value of the matrix specified with 00129 * use() at the real valued position p. 00130 * 00131 * @param p the real valued position to be interpolated. 00132 * @return the interpolated value of the matrix. 00133 */ 00134 T apply(const tpoint<float>& p) const; 00135 00136 /** 00137 * Returns the interpolated value of the matrix at the real valued 00138 * position (row,col). This method is not virtual and can be used 00139 * if this interpolation type is used as template parameter in time 00140 * critical situations 00141 * 00142 * @param src matrix<T> with the source data. 00143 * @param row which row 00144 * @param col which column 00145 * @return the interpolated value of the matrix. 00146 */ 00147 T interpolate(const matrix<T>& src, 00148 const float row, 00149 const float col) const; 00150 00151 /** 00152 * Returns the interpolated value of the matrix specified with 00153 * use() at the real valued position (row,col). This method is 00154 * not virtual and can be used if this interpolation type is used 00155 * as template parameter in time critical situations 00156 * 00157 * @param row which row 00158 * @param col which column 00159 * @return the interpolated value of the matrix. 00160 */ 00161 inline T interpolate(const float row, 00162 const float col) const { 00163 return interpolate(*this->theMatrix,row,col); 00164 }; 00165 00166 /** 00167 * Returns the interpolated value of the matrix at the real valued 00168 * position (row,col). 00169 * 00170 * This method does not check if the given coordinates and the rest of 00171 * used points in the src matrix lie within the valid range. This is 00172 * left to you. Please consider that for the bilinear interpolation 00173 * not only the point(trunc(col),trunc(row)) is used, but also its three 00174 * "next" neighbors ((col,row+1),(col+1,row),(col+1,row+1)). 00175 * 00176 * This method is not virtual and can be used in time critical situations, 00177 * where you prefer to take the boundary control by yourself. 00178 * 00179 * @param src matrix<T> with the source data. 00180 * @param row which row 00181 * @param col which column 00182 * @return the interpolated value of the matrix. 00183 */ 00184 inline T interpolateUnchk(const matrix<T>& src, 00185 const float row, 00186 const float col) const; 00187 00188 /** 00189 * Returns the interpolated value of the matrix specified with 00190 * use() at the real valued position (row,col). This method is 00191 * not virtual and can be used if this interpolation type is used 00192 * as template parameter in time critical situations 00193 * 00194 * @param row which row 00195 * @param col which column 00196 * @return the interpolated value of the matrix. 00197 */ 00198 inline T interpolateUnchk(const float row, 00199 const float col) const; 00200 00201 /** 00202 * copy data of "other" functor. 00203 * @param other the functor to be copied 00204 * @return a reference to this functor object 00205 */ 00206 bilinearInterpolator& copy(const bilinearInterpolator& other); 00207 00208 /** 00209 * returns a pointer to a clone of this functor. 00210 */ 00211 virtual functor* clone() const; 00212 00213 /** 00214 * returns used parameters 00215 */ 00216 const parameters& getParameters() const; 00217 00218 /** 00219 * Compute the bilinear interpolated value for the given coefficients 00220 * and values. 00221 * 00222 * This method is provided for convenience only. Use at your own 00223 * risk. 00224 * 00225 * @param fy fractional term between 0 and 1 00226 * @param fx fractional term between 0 and 1 00227 * @param syx value for fx==0 fy==0 00228 * @param syx1 value for fx==1 fy==0 00229 * @param sy1x value for fx==0 fy==1 00230 * @param sy1x1 value for fx==1 fy==1 00231 * 00232 * @return interpolated value between the four corners 00233 */ 00234 inline T compute(const float fy, 00235 const float fx, 00236 const T syx, const T syx1, 00237 const T sy1x, const T sy1x1) const; 00238 00239 /** 00240 * Compute the linear interpolated value for the given coefficients 00241 * and values 00242 * 00243 * This method is provided for convenience only. Use at your own 00244 * risk. 00245 * 00246 * @param fx fractional term between 0 and 1 00247 * @param sx value for sx==0 00248 * @param sx1 value for sx==1 00249 * 00250 * @return interpolated value between the two extremes. 00251 */ 00252 inline T compute(const float fx, 00253 const T sx, const T sx1) const; 00254 }; 00255 00256 00257 // implementation of compute() inline methods 00258 00259 template<class T> 00260 inline T bilinearInterpolator<T>::compute(const float fx, 00261 const T sx, const T sx1) const { 00262 return static_cast<T>(sx + fx*(sx1-sx)); 00263 } 00264 00265 // specialization for pixels 00266 template<> 00267 inline rgbPixel bilinearInterpolator<rgbPixel>::compute(const float fx, 00268 const rgbPixel sx, 00269 const rgbPixel sx1 00270 ) const { 00271 const trgbPixel<float> sxf(sx); 00272 const trgbPixel<float> sx1f(sx1); 00273 return ((sxf + (sx1f-sxf)*fx).getRGBPixel()); 00274 } 00275 00276 template<class T> 00277 inline T bilinearInterpolator<T>::compute(const float fy, 00278 const float fx, 00279 const T syx, const T syx1, 00280 const T sy1x, const T sy1x1 00281 ) const { 00282 const float tmp1 = static_cast<float>(syx + (syx1-syx)*fx); 00283 const float tmp2 = static_cast<float>(sy1x + (sy1x1-sy1x)*fx); 00284 return static_cast<T>(tmp1 + (tmp2-tmp1)*fy); 00285 } 00286 00287 template<> 00288 inline rgbPixel 00289 bilinearInterpolator<rgbPixel>::compute(const float fy, 00290 const float fx, 00291 const rgbPixel syx, 00292 const rgbPixel syx1, 00293 const rgbPixel sy1x, 00294 const rgbPixel sy1x1) const { 00295 00296 const trgbPixel<float> syxf(syx); 00297 const trgbPixel<float> syx1f(syx1); 00298 const trgbPixel<float> sy1xf(sy1x); 00299 const trgbPixel<float> sy1x1f(sy1x1); 00300 00301 const trgbPixel<float> tmp1 = syxf + (syx1f-syxf)*fx; 00302 const trgbPixel<float> tmp2 = sy1xf + (sy1x1f-sy1xf)*fx; 00303 00304 return (tmp1 + (tmp2-tmp1)*fy).getRGBPixel(); 00305 } 00306 00307 00308 template<class T> 00309 inline T bilinearInterpolator<T>::interpolateUnchk(const matrix<T>& src, 00310 const float row, 00311 const float col) const { 00312 const int truncY = static_cast<int>(row); 00313 const float fractY = row - static_cast<float>(truncY); 00314 00315 const int truncX = static_cast<int>(col); 00316 const float fractX = col - static_cast<float>(truncX); 00317 00318 const T *const row1 = &src.at(truncY,truncX); 00319 const T *const row2 = &src.at(truncY+1,truncX); 00320 00321 // the interpolation: 00322 return compute(fractY,fractX,row1[0],row1[1],row2[0],row2[1]); 00323 } 00324 00325 template<class T> 00326 inline T bilinearInterpolator<T>::interpolateUnchk(const float row, 00327 const float col) const { 00328 return interpolateUnchk(*this->theMatrix,row,col); 00329 }; 00330 00331 00332 } 00333 00334 #endif