PICCANTE  0.4
The hottest HDR imaging library!
motion_estimation.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_FEATURES_MATCHING_MOTION_ESTIMATION_HPP
19 #define PIC_FEATURES_MATCHING_MOTION_ESTIMATION_HPP
20 
21 #include <functional>
22 
23 #include "../image.hpp"
24 #include "../util/std_util.hpp"
25 #include "../features_matching/patch_comp.hpp"
26 
27 namespace pic {
28 
33 {
34 protected:
36  int width, height;
38 
44  void processAux(TileList *tiles, Image *imgOut)
45  {
46  bool state = true;
47 
48  while(state) {
49  unsigned int currentTile = tiles->getNext();
50 
51  if(currentTile < tiles->tiles.size()) {
52  int x = tiles->tiles[currentTile].startX;
53  int y = tiles->tiles[currentTile].startY;
54 
55  int x0 = x + halfBlockSize;
56  int y0 = y + halfBlockSize;
57 
58 
59  int x_e = MAX((x + blockSize), imgOut->width);
60  int y_e = MAX((y + blockSize), imgOut->height);
61 
62  int dx = 0;
63  int dy = 0;
64  float err = FLT_MAX;
65 
66 
67  for(int k=-shift; k<=shift; k++) {
68  int y1 = y0 + k;
69 
70  for(int l=-shift; l<=shift; l++) {
71  int x1 = x0 + l;
72 
73  float tmp_err = pmc->getSSD(x0, y0, x1, y1);
74 
75  if(tmp_err < err) {
76  err = tmp_err;
77  dx = l;
78  dy = k;
79  }
80  }
81  }
82 
83  for(int k=y; k<y_e; k++) {
84  for(int l=x; l<x_e; l++) {
85 
86  float *data = (*imgOut)(l, k);
87  data[0] = float(dx);
88  data[1] = float(dy);
89  data[2] = err;
90  }
91  }
92 
93  } else {
94  state = false;
95  }
96  }
97  }
98 
99 public:
100 
108  MotionEstimation(Image *img0, Image *img1, int blockSize, int maxRadius)
109  {
110  pmc = NULL;
111 
112  setup(img0, img1, blockSize, maxRadius);
113  }
114 
116  {
117  delete_s(pmc);
118  }
119 
127  void setup(Image *img0, Image *img1, int blockSize, int maxRadius)
128  {
129  if(img0 == NULL || img1 == NULL) {
130  return;
131  }
132 
133  if(!img0->isSimilarType(img1)) {
134  return;
135  }
136 
137  if(maxRadius < 1) {
138  maxRadius = 1;
139  }
140 
141  //estimate the blockSize if not given
142  if(blockSize < 1) {
143  float nPixels = float(img0->nPixels());
144  float tmp = ceilf(log10f(nPixels));
145  blockSize = MAX(int(powf(2.0f, tmp)), 4);
146  }
147 
148  this->blockSize = blockSize;
149  this->halfBlockSize = blockSize >> 1;
150  this->shift = maxRadius * blockSize;
151 
152  this->width = img0->width;
153  this->height = img0->height;
154 
155  pmc = new PatchComp(img0, img1, blockSize);
156  }
157 
163  Image *process(Image *imgOut)
164  {
165  if(imgOut == NULL) {
166  imgOut = new Image(1, width, height, 3);
167  }
168 
170 
171  //create threads
172  int numCores = std::thread::hardware_concurrency();
173 
174  std::thread **thrd = new std::thread*[numCores];
175 
176  for(int i = 0; i < numCores; i++) {
177  thrd[i] = new std::thread(
178  std::bind(&MotionEstimation::processAux, this, &lst, imgOut));
179  }
180 
181  //threads join
182  for(int i = 0; i < numCores; i++) {
183  thrd[i]->join();
184  }
185 
186  return imgOut;
187  }
188 
198  static Image *execute(Image *img0, Image *img1, int blockSize, int maxRadius, Image *imgOut)
199  {
200  MotionEstimation me(img0, img1, blockSize, maxRadius);
201 
202  return me.process(imgOut);
203  }
204 };
205 
206 } // end namespace pic
207 
208 #endif /* PIC_FEATURES_MATCHING_MOTION_ESTIMATION_HPP */
int shift
Definition: motion_estimation.hpp:35
int halfBlockSize
Definition: motion_estimation.hpp:35
uint size()
size
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
bool isSimilarType(const Image *img)
isSimilarType checks if the current image is similar to img; i.e. if they have the same width...
~MotionEstimation()
Definition: motion_estimation.hpp:115
The PatchComp class.
Definition: patch_comp.hpp:36
int blockSize
Definition: motion_estimation.hpp:35
static Image * execute(Image *img0, Image *img1, int blockSize, int maxRadius, Image *imgOut)
Execute.
Definition: motion_estimation.hpp:198
float getSSD(int x0, int y0, int x1, int y1)
getSSD
Definition: patch_comp.hpp:177
The MotionEstimation class.
Definition: motion_estimation.hpp:32
int nPixels() const
nPixels computes the number of pixels.
Definition: image.hpp:499
MotionEstimation(Image *img0, Image *img1, int blockSize, int maxRadius)
MotionEstimation.
Definition: motion_estimation.hpp:108
The TileList class.
Definition: tile_list.hpp:33
uint getNext()
getNext returns the index of the next tile to process.
Image * process(Image *imgOut)
execute
Definition: motion_estimation.hpp:163
int width
Definition: motion_estimation.hpp:36
int height
Definition: motion_estimation.hpp:36
The Image class stores an image as buffer of float.
Definition: image.hpp:60
PatchComp * pmc
Definition: motion_estimation.hpp:37
void processAux(TileList *tiles, Image *imgOut)
processAux
Definition: motion_estimation.hpp:44
Definition: bilateral_separation.hpp:25
#define MAX(a, b)
Definition: math.hpp:73
int width
Definition: image.hpp:80
int height
Definition: image.hpp:80
void setup(Image *img0, Image *img1, int blockSize, int maxRadius)
setup
Definition: motion_estimation.hpp:127
std::vector< Tile > tiles
tiles a list of tiles
Definition: tile_list.hpp:50