cvd/haar.h

00001 /*                       
00002     This file is part of the CVD Library.
00003 
00004     Copyright (C) 2005 The Authors
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc., 
00019     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 #ifndef CVD_HAAR_H
00022 #define CVD_HAAR_H
00023 
00024 #include <vector>
00025 #include <cmath>
00026 #include <cvd/image.h>
00027 
00028 namespace CVD {
00029 
00030 namespace Internal {
00031     template<class It, class TempIt>
00032     inline void haar1D(It from, int w, TempIt store){
00033         for(int i = 0; i < w; ++i){
00034             store[i] = (from[2*i] + from[2*i+1]) * M_SQRT1_2;
00035             store[i+w] = (from[2*i] - from[2*i+1]) * M_SQRT1_2;
00036         }
00037         std::copy(store, store+2*w, from);
00038     }
00039 }
00040 
00041 
00048 template<class It>
00049 inline void haar1D(It from, It to){
00050     std::vector<typename std::iterator_traits<It>::value_type> store(std::distance(from,to), typename std::iterator_traits<It>::value_type());
00051     int w = std::distance(from,to);
00052     while(w>1){
00053         w /= 2;
00054         Internal::haar1D(from, w, store.begin());
00055     }
00056 }
00057 
00063 template<class It>
00064 inline void haar1D(It from, int size){
00065     haar1D(from, from + size);
00066 }
00067 
00075 template<class It>
00076 inline void haar2D(It from, const int width, const int height, int stride = -1){
00077     if(stride < 0) stride = width;
00078     typedef typename std::iterator_traits<It>::value_type T;
00079     std::vector<T> column(height, T());
00080     std::vector<T> store(std::max(width,height), T());
00081     int w = width;
00082     int h = height;
00083     while(w > 1 || h > 1){
00084         if(w > 1){
00085             for(int i = 0; i < h; ++i){
00086                 Internal::haar1D(from + stride * i, w/2, store.begin());
00087             }
00088         }
00089         if(h > 1){
00090             for(int i = 0; i < w; ++i){
00091                 for(int j = 0; j < h; ++j)
00092                     column[j] = from[stride * j + i];
00093                 Internal::haar1D(column.begin(), h/2, store.begin());
00094                 for(int j = 0; j < h; ++j)
00095                     from[stride * j + i] = column[j];
00096             }
00097         }
00098         if(w>1) w/=2;
00099         if(h>1) h/=2;
00100     }
00101 }
00102 
00107 template<class T>
00108 inline void haar2D( SubImage<T> & I ){
00109     haar2D(I.data(), I.size().x, I.size().y, I.row_stride());
00110 }
00111 
00112 }
00113 
00114 #endif // CVD_HAAR_H

Generated on Wed Feb 18 10:23:01 2009 for CVD by  doxygen 1.5.3