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 .......: ltiBiquadraticInterpolator.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 12.6.2001 00030 * revisions ..: $Id: ltiBiquadraticInterpolator.h,v 1.3 2006/02/07 18:30:56 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_BIQUADRATIC_INTERPOLATOR_H_ 00034 #define _LTI_BIQUADRATIC_INTERPOLATOR_H_ 00035 00036 #include "ltiImage.h" 00037 #include "ltiVector.h" 00038 #include "ltiScalarValuedInterpolation.h" 00039 00040 namespace lti { 00041 /** 00042 * This functor use biquadratic 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 biquadraticInterpolator : public scalarValuedInterpolation<T> { 00050 public: 00051 typedef typename scalarValuedInterpolation<T>::parameters parameters; 00052 00053 /** 00054 * default constructor 00055 */ 00056 biquadraticInterpolator(); 00057 00058 /** 00059 * Construct an interpolator with the given boundary type 00060 */ 00061 biquadraticInterpolator(const eBoundaryType boundaryType); 00062 00063 /** 00064 * copy constructor 00065 * @param other the object to be copied 00066 */ 00067 biquadraticInterpolator(const biquadraticInterpolator<T>& other); 00068 00069 /** 00070 * destructor 00071 */ 00072 virtual ~biquadraticInterpolator(); 00073 00074 /** 00075 * returns the name of this type ("biquadraticInterpolator") 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 biquadratic interpolation 00173 * not only the point(trunc(col),trunc(row)) is used, but a 3x3 00174 * neighborhood centered such that (col,row) is nearest to the center. 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 /** 00203 * copy data of "other" functor. 00204 * @param other the functor to be copied 00205 * @return a reference to this functor object 00206 */ 00207 biquadraticInterpolator& copy(const biquadraticInterpolator& other); 00208 00209 /** 00210 * returns a pointer to a clone of this functor. 00211 */ 00212 virtual functor* clone() const; 00213 00214 /** 00215 * returns used parameters 00216 */ 00217 const parameters& getParameters() const; 00218 00219 /** 00220 * Compute the biquadratic interpolated value for the given coefficients 00221 * and values. 00222 * 00223 * This method is provided for convenience only. Use at your own 00224 * risk. 00225 * 00226 * @param fx fractional term between -0.5 and 0.5 00227 * @param y0 value for fx==-1.0 (note that is a value beyond the valid 00228 * range of fx) 00229 * @param y1 value for fx==0.0 00230 * @param y2 value for fx==1.0 (note that is a value beyond the valid 00231 * range of fx) 00232 * 00233 * @return interpolated value between the three values 00234 */ 00235 inline T compute(const float fx,const T y0,const T y1,const T y2) const; 00236 00237 /** 00238 * Compute the biquadratic interpolated value for the given coefficients 00239 * and values. 00240 * 00241 * This method is provided for convenience only. Use at your own 00242 * risk. 00243 * 00244 * @param fy fractional term between -0.5 and 0.5 00245 * @param fx fractional term between -0.5 and 0.5 00246 * @param data array of 9 elements 00247 * 00248 * @return interpolated value between the four corners 00249 */ 00250 inline T compute(const float fy, 00251 const float fx, 00252 const T *const data) const; 00253 00254 /** 00255 * Compute the biquadratic interpolated value for the given coefficients 00256 * and values. 00257 * 00258 * This method is provided for convenience only. Use at your own 00259 * risk. 00260 * 00261 * @param fy fractional term between -0.5 and 0.5 00262 * @param fx fractional term between -0.5 and 0.5 00263 * @param y00 value for fx==-1 fy==-1 00264 * @param y01 value for fx==-1 fy== 0 00265 * @param y02 value for fx==-1 fy==+1 00266 * @param y10 value for fx== 0 fy==-1 00267 * @param y11 value for fx== 0 fy== 0 00268 * @param y12 value for fx== 0 fy==+1 00269 * @param y20 value for fx==+1 fy==-1 00270 * @param y21 value for fx==+1 fy== 0 00271 * @param y22 value for fx==+1 fy==+1 00272 * 00273 * @return interpolated value between the four corners 00274 */ 00275 inline T compute(const float fy, 00276 const float fx, 00277 const T y00,const T y01,const T y02, 00278 const T y10,const T y11,const T y12, 00279 const T y20,const T y21,const T y22) const; 00280 00281 }; 00282 00283 // -------------------------------------------- 00284 // implementation of some inline helper methods 00285 // -------------------------------------------- 00286 00287 template<class T> 00288 inline T biquadraticInterpolator<T>::compute(const float fx, 00289 const T y0, 00290 const T y1, 00291 const T y2) const { 00292 00293 const T a = (y0+y2)/2 - y1; 00294 const T b = (y2-y0)/2; 00295 const T c = y1; 00296 00297 return (static_cast<T>((a*fx + b)*fx + c)); 00298 } 00299 00300 template<> 00301 inline rgbPixel 00302 biquadraticInterpolator<rgbPixel>::compute(const float fx, 00303 const rgbPixel y0, 00304 const rgbPixel y1, 00305 const rgbPixel y2) const { 00306 const trgbPixel<float> y0f(y0); 00307 const trgbPixel<float> y1f(y1); 00308 const trgbPixel<float> y2f(y2); 00309 00310 const trgbPixel<float> a = (y0f+y2f)/2.0f - y1f; 00311 const trgbPixel<float> b = (y2f-y0f)/2.0f; 00312 const trgbPixel<float> c = y1f; 00313 00314 return ((a*fx + b)*fx + c).getClippedRGBPixel(); 00315 } 00316 00317 template<class T> 00318 inline T biquadraticInterpolator<T>::compute(const float fy, 00319 const float fx, 00320 const T *const y) const { 00321 00322 const T v1 = compute(fx,y[0],y[1],y[2]); 00323 const T v2 = compute(fx,y[3],y[4],y[5]); 00324 const T v3 = compute(fx,y[6],y[7],y[8]); 00325 00326 return compute(fy,v1,v2,v3); 00327 } 00328 00329 template<class T> 00330 inline T biquadraticInterpolator<T>::compute(const float fy, 00331 const float fx, 00332 const T y00, 00333 const T y01, 00334 const T y02, 00335 const T y10, 00336 const T y11, 00337 const T y12, 00338 const T y20, 00339 const T y21, 00340 const T y22) const { 00341 00342 const T v1 = compute(fx,y00,y01,y02); 00343 const T v2 = compute(fx,y10,y11,y12); 00344 const T v3 = compute(fx,y20,y21,y22); 00345 00346 return compute(fy,v1,v2,v3); 00347 } 00348 00349 00350 template<class T> 00351 inline T biquadraticInterpolator<T>::interpolateUnchk(const matrix<T>& src, 00352 const float row, 00353 const float col) const{ 00354 // precomputations for x 00355 int truncX = static_cast<int>(col); 00356 float fractX = col - static_cast<float>(truncX); 00357 if (fractX <= 0.5f) { 00358 --truncX; 00359 } else { 00360 fractX-=1.0f; 00361 } 00362 00363 // precomputation for y 00364 int truncY = static_cast<int>(row); 00365 float fractY = row - static_cast<float>(truncY); 00366 if (fractY <= 0.5f) { 00367 --truncY; 00368 } else { 00369 fractY-=1.0f; 00370 } 00371 00372 const T *const row1 = &src.at(truncY,truncX); 00373 const T *const row2 = &src.at(truncY+1,truncX); 00374 const T *const row3 = &src.at(truncY+2,truncX); 00375 00376 return compute(fractY,fractX, 00377 row1[0],row1[1],row1[2], 00378 row2[0],row2[1],row2[2], 00379 row3[0],row3[1],row3[2]); 00380 00381 } 00382 00383 template<class T> 00384 inline T biquadraticInterpolator<T>::interpolateUnchk(const float row, 00385 const float col) const{ 00386 return interpolateUnchk(*this->theMatrix,row,col); 00387 } 00388 00389 00390 } 00391 00392 #endif