PICCANTE  0.4
The hottest HDR imaging library!
filter_warp_2d.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_GL_FILTERING_FILTER_WARP_2D_HPP
19 #define PIC_GL_FILTERING_FILTER_WARP_2D_HPP
20 
21 #include "../util/matrix_3_x_3.hpp"
22 
23 #include "../filtering/filter.hpp"
24 #include "../image_samplers/image_sampler_bilinear.hpp"
25 
26 namespace pic {
27 
31 class FilterWarp2D: public Filter
32 {
33 protected:
36  int bmin[2], bmax[2];
37  float mid[2];
39 
46  void ProcessBBox(Image *dst, ImageVec src, BBox *box)
47  {
48  int channels = src[0]->channels;
49 
50  float pos[2], pos_out[2];
51 
52  for(int j = box->y0; j < box->y1; j++) {
53  pos[1] = float(j + bmin[1]) - mid[1];
54 
55  for(int i = box->x0; i < box->x1; i++) {
56  float *tmp_dst = (*dst)(i, j);
57 
58  pos[0] = float(i + bmin[0]) - mid[0];
59 
60  h_inv.projection(pos, pos_out);
61 
62  pos_out[0] += mid[0];
63  pos_out[1] += mid[1];
64 
65  if(pos_out[0] >= 0.0f && pos_out[0] <= src[0]->width1f &&
66  pos_out[1] >= 0.0f && pos_out[1] <= src[0]->height1f) {
67  isb.SampleImageUC(src[0], pos_out[0], pos_out[1], tmp_dst);
68  } else {
69  Arrayf::assign(0.0f, tmp_dst, channels);
70  }
71  }
72  }
73  }
74 
76 
77 public:
78 
83  {
84  this->bComputeBoundingBox = true;
85  this->bCentroid = false;
86  this->bSameSize = false;
87 
88  h.getIdentity();
90  }
91 
98  FilterWarp2D(Matrix3x3 h, bool bSameSize = false, bool bCentroid = false) : Filter()
99  {
100  this->bComputeBoundingBox = true;
102  }
103 
109  {
110  return bCentroid;
111  }
112 
123  float width, float height,
124  int *bmin, int *bmax ) {
125  float bbox[4][2];
126  float bbox_out[4][2];
127 
128  bbox[0][0] = 0.0f;
129  bbox[0][1] = 0.0f;
130 
131  bbox[1][0] = 0.0f;
132  bbox[1][1] = height;
133 
134  bbox[2][0] = width;
135  bbox[2][1] = 0.0f;
136 
137  bbox[3][0] = width;
138  bbox[3][1] = height;
139 
140  float mid[2];
141 
142  if(bCentroid) {
143  mid[0] = width * 0.5f;
144  mid[1] = height * 0.5f;
145  } else {
146  mid[0] = 0.0f;
147  mid[1] = 0.0f;
148  }
149 
150  //compute the bounding box
151  bmin[0] = 1 << 24;
152  bmin[1] = 1 << 24;
153 
154  bmax[0] = -1;
155  bmax[1] = -1;
156 
157  for(int i = 0; i < 4; i++) {
158 
159  bbox[i][0] -= mid[0];
160  bbox[i][1] -= mid[1];
161 
162  h.projection(&bbox[i][0], &bbox_out[i][0]);
163 
164  bbox_out[i][0] += mid[0];
165  bbox_out[i][1] += mid[1];
166 
167  int x = int(bbox_out[i][0]);
168  int y = int(bbox_out[i][1]);
169 
170  //min point
171  bmin[0] = (x < bmin[0]) ? x : bmin[0];
172  bmin[1] = (y < bmin[1]) ? y : bmin[1];
173 
174  bmax[0] = (x > bmax[0]) ? x : bmax[0];
175  bmax[1] = (y > bmax[1]) ? y : bmax[1];
176  }
177 
178  printf("%d %d\n", bmax[0], bmin[0], bmax[1], bmin[1]);
179  }
180 
186  void setBoundingBox(int *bmin, int *bmax)
187  {
188  memcpy(this->bmin, bmin, sizeof(int) * 2);
189  memcpy(this->bmax, bmax, sizeof(int) * 2);
190  bComputeBoundingBox = false;
191  }
192 
199  void update(Matrix3x3 h, bool bSameSize, bool bCentroid = false)
200  {
201  this->bComputeBoundingBox = true;
202 
203  this->bSameSize = bSameSize;
204  this->bCentroid = bCentroid;
205 
206  this->h = h;
207  h.inverse(&h_inv);
208  }
209 
218  void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames)
219  {
220  if(bCentroid) {
221  mid[0] = imgIn[0]->widthf;
222  mid[1] = imgIn[0]->heightf;
223  } else {
224  mid[0] = 0.0f;
225  mid[1] = 0.0f;
226  }
227 
228  if(!bSameSize) {
229  if(bComputeBoundingBox) {
231  imgIn[0]->widthf, imgIn[0]->heightf,
232  bmin, bmax);
233  }
234 
235  width = bmax[0] - bmin[0];
236  height = bmax[1] - bmin[1];
237  } else {
238  bmin[0] = 0;
239  bmin[1] = 0;
240 
241  bmax[0] = 0;
242  bmax[1] = 0;
243 
244  width = imgIn[0]->width;
245  height = imgIn[0]->height;
246  }
247 
248  frames = imgIn[0]->frames;
249  channels = imgIn[0]->channels;
250  }
251 
261  static Image *execute(Image *img, Image *imgOut, Matrix3x3 h, bool bSameSize = false, bool bCentroid = false)
262  {
264  imgOut = flt.Process(Single(img), imgOut);
265  return imgOut;
266  }
267 };
268 
269 } // end namespace pic
270 
271 #endif /* PIC_GL_FILTERING_FILTER_WARP_2D_HPP */
272 
void getIdentity()
getIdentity sets the matrix as an identity matrix; diag(1, 1, 1);
Definition: matrix_3_x_3.hpp:96
int bmax[2]
Definition: filter_warp_2d.hpp:36
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames)
OutputSize.
Definition: filter_warp_2d.hpp:218
void ProcessBBox(Image *dst, ImageVec src, BBox *box)
ProcessBBox.
Definition: filter_warp_2d.hpp:46
FilterWarp2D(Matrix3x3 h, bool bSameSize=false, bool bCentroid=false)
FilterWarp2D.
Definition: filter_warp_2d.hpp:98
float mid[2]
Definition: filter_warp_2d.hpp:37
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
int x0
Definition: bbox.hpp:32
void SampleImageUC(Image *img, float x, float y, float *vOut)
SampleImageUC samples an image in unnormalized coordinates [0,width-1]x[0,height-1].
Definition: image_sampler_bilinear.hpp:60
bool getBCentroid()
getBCentroid
Definition: filter_warp_2d.hpp:108
The Matrix3x3 class provides methods for managing a 3 by 3 matrix.
Definition: matrix_3_x_3.hpp:29
The Filter class.
Definition: filter.hpp:50
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
static Image * execute(Image *img, Image *imgOut, Matrix3x3 h, bool bSameSize=false, bool bCentroid=false)
execute
Definition: filter_warp_2d.hpp:261
FilterWarp2D()
FilterWarp2D.
Definition: filter_warp_2d.hpp:82
Matrix3x3 * inverse(Matrix3x3 *ret)
inverse computes the inverse of the matrix.
Definition: matrix_3_x_3.hpp:257
bool bComputeBoundingBox
Definition: filter_warp_2d.hpp:38
int y0
Definition: bbox.hpp:32
Matrix3x3 h_inv
Definition: filter_warp_2d.hpp:35
bool bSameSize
Definition: filter_warp_2d.hpp:75
float * projection(float *vec, float *ret)
projection
Definition: matrix_3_x_3.hpp:186
The Image class stores an image as buffer of float.
Definition: image.hpp:60
void setBoundingBox(int *bmin, int *bmax)
setBoundingBox
Definition: filter_warp_2d.hpp:186
void update(Matrix3x3 h, bool bSameSize, bool bCentroid=false)
update
Definition: filter_warp_2d.hpp:199
virtual void f(FilterFData *data)
f
Definition: filter_radial_basis_function.hpp:69
Matrix3x3 h
Definition: filter_warp_2d.hpp:35
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
Definition: bilateral_separation.hpp:25
static T * assign(T *data, int size, T *ret)
assign
Definition: array.hpp:464
ImageSamplerBilinear isb
Definition: filter_warp_2d.hpp:34
static void computeBoundingBox(Matrix3x3 &h, bool bCentroid, float width, float height, int *bmin, int *bmax)
computeBoundingBox
Definition: filter_warp_2d.hpp:122
int bmin[2]
Definition: filter_warp_2d.hpp:36
The ImageSamplerBilinear class.
Definition: image_sampler_bilinear.hpp:28
bool bCentroid
Definition: filter_warp_2d.hpp:75
The FilterWarp2D class.
Definition: filter_warp_2d.hpp:31