00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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