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 .......: ltiNearestNeighborInterpolator.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 12.6.2001 00030 * revisions ..: $Id: ltiNearestNeighborInterpolator.h,v 1.6 2010/04/10 01:30:23 alvarado Exp $ 00031 */ 00032 00033 #ifndef _LTI_NEAREST_NEIGHBOR_INTERPOLATOR_H_ 00034 #define _LTI_NEAREST_NEIGHBOR_INTERPOLATOR_H_ 00035 00036 #include "ltiImage.h" 00037 #include "ltiVector.h" 00038 #include "ltiScalarValuedInterpolation.h" 00039 00040 namespace lti { 00041 /** 00042 * This functor use nearestNeighbor 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 nearestNeighborInterpolator : public scalarValuedInterpolation<T> { 00050 public: 00051 typedef typename scalarValuedInterpolation<T>::parameters parameters; 00052 00053 /** 00054 * default constructor 00055 */ 00056 nearestNeighborInterpolator(); 00057 00058 /** 00059 * Construct an interpolator with the given boundary type. 00060 */ 00061 nearestNeighborInterpolator(const eBoundaryType boundaryType); 00062 00063 /** 00064 * copy constructor 00065 * @param other the object to be copied 00066 */ 00067 nearestNeighborInterpolator(const nearestNeighborInterpolator<T>& other); 00068 00069 /** 00070 * destructor 00071 */ 00072 virtual ~nearestNeighborInterpolator(); 00073 00074 /** 00075 * returns the name of this type ("nearestNeighborInterpolator") 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 inline T interpolate(const matrix<T>& src, 00148 const float row, 00149 const float col) const; 00150 00151 /** 00152 * copy data of "other" functor. 00153 * @param other the functor to be copied 00154 * @return a reference to this functor object 00155 */ 00156 nearestNeighborInterpolator& copy(const nearestNeighborInterpolator& other); 00157 00158 /** 00159 * returns a pointer to a clone of this functor. 00160 */ 00161 virtual functor* clone() const; 00162 00163 /** 00164 * returns used parameters 00165 */ 00166 const parameters& getParameters() const; 00167 }; 00168 00169 // -------------------------------------- 00170 // implementation of public inline method 00171 // -------------------------------------- 00172 00173 /* 00174 * Returns the interpolated value of the matrix at the real valued 00175 * position (row,col). 00176 * 00177 * @param src matrix<T> with the source data. 00178 * @param row which row 00179 * @param col which column 00180 * @return the interpolated value of the matrix. 00181 */ 00182 template<class T> 00183 inline T nearestNeighborInterpolator<T>::interpolate(const matrix<T>& src, 00184 const float row, 00185 const float col) const { 00186 00187 00188 const int truncY = iround(row); 00189 const int truncX = iround(col); 00190 00191 // the interpolation: 00192 if ((static_cast<unsigned int>(truncX)< 00193 static_cast<unsigned int>(src.columns())) && 00194 (static_cast<unsigned int>(truncY)< 00195 static_cast<unsigned int>(src.rows()))) { 00196 // normal interpolation within matrix range 00197 return src.at(truncY,truncX); 00198 } else { 00199 switch (getParameters().boundaryType) { 00200 case lti::NoBoundary: 00201 case lti::Zero:{ 00202 return static_cast<T>(0); 00203 } break; 00204 case lti::Constant: { 00205 int x=truncX; 00206 int y=truncY; 00207 if (x<0) { 00208 x=0; 00209 } else if (x>=src.columns()) { 00210 x=src.lastColumn(); 00211 } 00212 if (y<0) { 00213 y=0; 00214 } else if (y>=src.rows()) { 00215 y=src.lastRow(); 00216 } 00217 return src.at(y,x); 00218 } break; 00219 case lti::Mirror: { 00220 int p,xp,yp; 00221 00222 // fix x 00223 p = truncX/src.columns(); 00224 xp = truncY%src.columns(); 00225 00226 if (truncX < 0) { 00227 p = 1-p; 00228 if (xp < 0) { 00229 xp += src.columns(); 00230 } 00231 } 00232 00233 if ((p & 01) != 0) { // odd number 00234 xp = src.lastColumn()-xp; 00235 } 00236 00237 // fix y 00238 p = truncY/src.rows(); 00239 yp = truncY%src.rows(); 00240 00241 if (truncY < 0) { 00242 p = 1-p; 00243 if (yp < 0) { 00244 yp += src.rows(); 00245 } 00246 } 00247 00248 if ((p & 01) != 0) { // odd number 00249 yp = src.lastRow()-yp; 00250 } 00251 00252 return src.at(yp,xp); 00253 } break; 00254 case lti::Periodic: { 00255 int x = truncX%src.columns(); 00256 00257 if (x < 0) { 00258 x += src.columns(); 00259 } 00260 00261 int y = truncY%src.rows(); 00262 00263 if (y < 0) { 00264 y += src.rows(); 00265 } 00266 00267 return src.at(y,x); 00268 } break; 00269 default: 00270 return T(0); 00271 } 00272 } 00273 return T(); 00274 } 00275 00276 } 00277 00278 #endif