CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
image_pyramid.hpp
Go to the documentation of this file.
1 //*LB*
2 // Copyright (c) 2010, University of Bonn, Institute for Computer Science VI
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the University of Bonn
14 // nor the names of its contributors may be used to endorse or promote
15 // products derived from this software without specific prior written
16 // permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //*LE*
29 
38 #ifndef __IMAGE_PYRAMID_HPP__
39 #define __IMAGE_PYRAMID_HPP__
40 
41 #include <cuv/tensor_ops/tensor_ops.hpp>
43 
44 namespace cuv{
45 
54  template <class __matrix_type>
56  {
57  public:
58  typedef __matrix_type matrix_type;
59  typedef typename matrix_type::value_type value_type;
60  typedef typename matrix_type::memory_layout_type memory_layout;
61  typedef typename matrix_type::index_type index_type;
62 
71  image_pyramid( int img_h, int img_w, int depth, int dim );
77  matrix_type* get(int depth, int channel);
78  matrix_type* get_all_channels(int depth);
79  inline int dim(){return m_dim;}
80  inline unsigned int base_h(){return m_base_height;}
81  inline unsigned int base_w(){return m_base_width;}
82  inline unsigned int depth(){return m_matrices.size();}
83 
90  template<class __arg_matrix_type>
91  void build(const __arg_matrix_type& src, const unsigned int interleaved_channels){
92  typedef typename __arg_matrix_type::value_type argval_type;
93  typedef typename __arg_matrix_type::memory_space_type argmemspace_type;
94  typedef typename __arg_matrix_type::index_type argindex_type;
96 
98  // create base image at level 0
100  if( src.shape()[0] == m_base_height*m_dim // the image dimensions match the input --> just copy.
101  && src.shape()[1] == m_base_width
102  ){
103  //std::cout << "Copycase"<<std::endl;
104  *m_matrices[0]=src;
105  }
106  else if( interleaved_channels == 4
107  && m_dim == 3
108  ){
109  //std::cout << "Colorcase"<<std::endl;
110  argca_type cpy(src,4);
111  matrix_type& dst = *m_matrices[0];
112  gaussian_pyramid_downsample(dst, cpy, interleaved_channels);
113  }
114  else if(src.shape()[0] > m_base_height*m_dim // the image dimensions are too large: downsample to 1st level of pyramid
115  && src.shape()[1] > m_base_width
116  ){
117  //std::cout << "Multichannel case"<<std::endl;
118  for(int i=0;i<m_dim;i++){
119  const __arg_matrix_type view(indices[index_range(0,src.shape()[0]/m_dim)][index_range(0,src.shape()[1])],(argval_type*)src.ptr());
120  argca_type cpy(view);
121  matrix_type* dstview = get(0,i);
122  gaussian_pyramid_downsample(*dstview, cpy,1);
123  delete dstview;
124  }
125  }else{
126  cuvAssert(false);
127  }
128 
130  // fill upper levels
132  for(int i=1;i<m_matrices.size();i++){
133  for(int d=0;d<m_dim;d++){
134  matrix_type* srcview = get(i-1,d);
135  argca_type cpy(*srcview);
136  matrix_type* dstview = get(i, d);
137  gaussian_pyramid_downsample(*dstview, cpy,1);
138  delete dstview;
139  delete srcview;
140  }
141  }
142 
143  }
144  private:
145  std::vector<matrix_type*> m_matrices;
146  unsigned int m_dim;
147  unsigned int m_base_width;
148  unsigned int m_base_height;
149  };
150 
151  template <class __matrix_type>
152  typename image_pyramid<__matrix_type>::matrix_type*
153  image_pyramid<__matrix_type>::get(int depth, int channel){
154  cuvAssert(depth < m_matrices.size());
155  cuvAssert(channel < m_dim);
156  matrix_type& mat = *m_matrices[depth];
157  //std::cout << "asking for channel "<<channel<<" in matrix of size "<<mat.shape()[0]<<"x"<<mat.shape()[1]<<std::endl;
158  unsigned int w = mat.shape()[1];
159  unsigned int h = mat.shape()[0];
160  return new matrix_type(indices[index_range(0,h/m_dim)][index_range(0,w)],mat.ptr()+channel*w*h/m_dim);
161  }
162 
163  template <class __matrix_type>
166  cuvAssert(depth < m_matrices.size());
167  matrix_type& mat = *m_matrices[depth];
168  return &mat;
169  }
170 
171  template <class __matrix_type>
172  image_pyramid<__matrix_type>::image_pyramid( int img_h, int img_w, int depth, int dim )
173  :m_base_height(img_h)
174  ,m_base_width(img_w)
175  ,m_dim(dim)
176  {
177  for(unsigned int i=0;i<m_matrices.size();i++)
178  delete m_matrices[i];
179  m_matrices.clear();
180  for(unsigned int i=0; i<depth;i++){
181  //std::cout << "Creating Pyramid Level: "<< img_h<<"*"<<m_dim<<"x"<<img_w<<std::endl;
182  m_matrices.push_back(new matrix_type(extents[img_h*m_dim][img_w]));
183  img_h=ceil(img_h/2.f);
184  img_w=ceil(img_w/2.f);
185  }
186  }
187 
196 template<class T,class S, class I>
199  const cuda_array<T,S,I>& src,
200  const unsigned int interleaved_channels
201 );
202 template<class T,class S, class I>
203 void gaussian_pyramid_upsample(
205  const cuda_array<T,S,I>& src
206 );
207 
208 template<class TDest, class T,class S, class I>
209 void get_pixel_classes(
211  const cuda_array<T,S,I>& src,
212  float scale_fact
213 );
214 
215 template<class T,class S, class I>
216 void gaussian(
218  const cuda_array<T,S,I>& src
219 );
220  // end group image_ops
222 
223 }
224 #endif /* __IMAGE_PYRAMID_HPP__ */