00001
00002
00003 #ifndef _PYRAMID_HPP_
00004 #define _PYRAMID_HPP_
00005
00006 #include <vector>
00007 #include "image/Image.hpp"
00008 #include "image/filter.hpp"
00009
00010 namespace jafar {
00011 namespace image {
00012
00020 class Pyramid {
00021 public:
00022 typedef std::vector<jafar::image::Image>::iterator iterator;
00023 typedef std::vector<jafar::image::Image>::const_iterator const_iterator;
00024 iterator begin() { return p_pyr.begin(); };
00025 const_iterator begin() const { return p_pyr.begin(); };
00026 iterator end() { return p_pyr.end(); };
00027 const_iterator end() const { return p_pyr.end(); };
00028
00030 Pyramid(int levels=0) : p_pyr(levels) {};
00031
00033 Pyramid(jafar::image::Image const & img, int levels) {
00034 build(img,levels);
00035 }
00036
00038 Pyramid(Pyramid const & pyr) : p_pyr(pyr.size()){
00039 p_pyr = pyr.p_pyr;
00040 };
00041
00043 virtual ~Pyramid() {};
00044
00048 Pyramid clone() const {
00049 Pyramid pyr(size());
00050 for(unsigned int i=0; i<size(); i++)
00051 pyr[i] = p_pyr[i].clone();
00052 return pyr;
00053 }
00054
00058 size_t size() const {
00059 return p_pyr.size();
00060 }
00061
00065 bool check() const {
00066 if ( size() == 0 ) {
00067 JFR_DEBUG("Empty pyramid");
00068 return false;
00069 }
00070 for(size_t i=0; i<size(); i++) {
00071 JFR_DEBUG("level " << i << ":" << p_pyr[i]);
00072 if (p_pyr[i].empty())
00073 return false;
00074 }
00075 return true;
00076 }
00077
00081 jafar::image::Image & operator[] (int l) {
00082 return p_pyr[l];
00083 };
00084
00088 jafar::image::Image const & operator[] (int l) const {
00089 return p_pyr[l];
00090 };
00091
00096 template<typename type>
00097 type getPixelValue(int x, int y, int l, int ref_l=0,int channel=0) const;
00098
00103 template<typename type>
00104 double getSubPixelValue(double x, double y, int l,int ref_l=0,int channel=0) const;
00105
00109 Pyramid& operator= (Pyramid const & pyr) {
00110 p_pyr = pyr.p_pyr;
00111 return *this;
00112 };
00113
00117 bool operator==(jafar::image::Image const & img) const {
00118 return ( !p_pyr[0].empty() ? ( p_pyr[0].compHeader(img) ) : false);
00119 }
00120
00124 bool operator==(jafar::image::Pyramid const & pyr) const {
00125 return ( size() == 0 ? ( !p_pyr[0].empty() ? ( p_pyr[0].compHeader(pyr[0]) &&
00126 p_pyr.size() == pyr.size() ) : false ) : false);
00127 }
00128
00132 virtual void build(jafar::image::Image const & img, int l=0);
00133
00134
00135 protected:
00136 std::vector<jafar::image::Image> p_pyr;
00137 };
00138
00139
00140
00141
00142
00143
00144 template<typename type>
00145 type Pyramid::getPixelValue(int x, int y, int l, int ref_l, int channel) const {
00146 JFR_PRECOND( p_pyr.size() > 0, "Pyramid::getPixelValue: empty pyramid");
00147 JFR_PRECOND(l < p_pyr.size(), "Pyramid::getPixelValue: level parameter out of range");
00148 JFR_PRECOND( ( ref_l >= 0 && ref_l <= l ), "Pyramid::getPixelValue: incompatible level and reference level");
00149 if ( ref_l == l )
00150 return p_pyr[l].getPixelValue<type>(x,y,channel);
00151 int ii = x >> (l-ref_l), jj = y >> (l-ref_l);
00152 int ww = p_pyr[l].width() - 1, hh = p_pyr[l].height() - 1;
00153 if (ii > ww) ii = ww; if (jj > hh) jj = hh;
00154 return p_pyr[l].getPixelValue<type>(ii, jj, channel);
00155 }
00156
00157 template<typename type>
00158 double Pyramid::getSubPixelValue(double x, double y, int l, int ref_l, int channel) const {
00159 JFR_PRECOND( p_pyr.size() > 0, "Pyramid::getPixelValue: empty pyramid");
00160 JFR_PRECOND(l < p_pyr.size(), "Pyramid::getPixelValue: level parameter out of range");
00161 JFR_PRECOND( ( ref_l >= 0 && ref_l <= l ), "Pyramid::getSubPixelValue: incompatible level and reference level");
00162 if ( ref_l == l )
00163 return p_pyr[l].getSubPixelValue<type>(x,y,channel);
00164 double fac = double(1 << (l-ref_l));
00165 double ii = x / fac, jj = y / fac;
00166 int ww = p_pyr[l].width() - 1, hh = p_pyr[l].height() - 1;
00167 if (ii > ww) ii = ww; if (jj > hh) jj = hh;
00168 return p_pyr[l].getSubPixelValue<type>(ii, jj, channel);
00169 }
00170
00171
00175 class GaussianPyramid : public Pyramid {
00176 public:
00177 GaussianPyramid (int l=0, double s=1.2, double s0=1.0) : Pyramid(l), sigma(s), sigma0(s0) {};
00178 virtual ~GaussianPyramid () {};
00179
00180 virtual void build(jafar::image::Image const & img, int l=0);
00181
00182 private:
00183 double sigma;
00184 double sigma0;
00185 };
00186
00187
00191 class GaborPyramid : public Pyramid {
00192 public:
00193 GaborPyramid (float theta, int l=0, float major_stddev=3.92, float minor_stddev=3.92, float period=7, int size=-1) : Pyramid(l) {
00194 gabor0 = jafar::image::filter::gaborkernel(major_stddev,minor_stddev,period,0.,theta,size);
00195 gabor90 = jafar::image::filter::gaborkernel(major_stddev,minor_stddev,period,90.,theta,size);
00196 };
00197 virtual ~GaborPyramid () {};
00198
00199 virtual void build(jafar::image::Image const & img, int l=0);
00200
00201 jafar::image::Image kernel0() const;
00202 jafar::image::Image kernel90() const;
00203
00204 private:
00205 cv::Mat gabor0;
00206 cv::Mat gabor90;
00207 };
00208
00212 class LinearPyramid : public Pyramid {
00213 public:
00214 LinearPyramid( int _levels) : Pyramid(_levels )
00215 {
00216 }
00217 virtual ~LinearPyramid() {}
00218 virtual void build( jafar::image::Image const & img, int l=0);
00219 };
00220
00221 }
00222 }
00223
00224
00225 #endif