PICCANTE  0.4
The hottest HDR imaging library!
exposure_fusion.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_TONE_MAPPING_EXPOSURE_FUSION_HPP
19 #define PIC_TONE_MAPPING_EXPOSURE_FUSION_HPP
20 
21 #include "../base.hpp"
22 #include "../util/std_util.hpp"
23 #include "../util/array.hpp"
24 #include "../colors/saturation.hpp"
25 #include "../filtering/filter_luminance.hpp"
26 #include "../filtering/filter_laplacian.hpp"
27 #include "../filtering/filter_exposure_fusion_weights.hpp"
28 
29 #include "../algorithms/pyramid.hpp"
30 
31 #include "../tone_mapping/get_all_exposures.hpp"
32 #include "../tone_mapping/tone_mapping_operator.hpp"
33 
34 namespace pic {
35 
40 {
41 protected:
44 
45  Pyramid *pW, *pI, *pOut;
46 
52  static float removeNegative(float x)
53  {
54  return MAX(x, 0.0f);
55  }
56 
63  Image *ProcessAux(ImageVec imgIn, Image *imgOut)
64  {
65  if(imgIn.size() > 1) {
66  return ProcessAuxStack(imgIn, imgOut);
67  } else {
68  pic::ImageVec stack = getAllExposuresImages(imgIn[0]);
69 
70  imgOut = ProcessAuxStack(stack, imgOut);
71 
72  stdVectorClear<Image>(stack);
73 
74  return imgOut;
75  }
76  }
77 
85  {
86  auto n = imgIn.size();
87 
88  if(n < 2 || !ImageVecCheck(imgIn, -1)) {
89  return imgOut;
90  }
91 
92  //compute weights values
93  int channels = imgIn[0]->channels;
94  int width = imgIn[0]->width;
95  int height = imgIn[0]->height;
96 
97  updateImage(imgIn[0]);
98 
99  if(images[2] == NULL) {//images[2] --> acc
100  images[2] = new Image(1, width, height, 1);
101  }
102 
103  //compute weights values
104  *images[2] = 0.0f;
105  for(auto j = 0; j < n; j++) {
106  images[0] = flt_lum.Process(Single(imgIn[j]), images[0]);
107  images[1] = flt_weights.Process(Double(images[0], imgIn[j]), images[1]);
108 
109  *images[2] += *images[1];
110  }
111 
112  //accumulate into a Pyramid
113 
114  releaseAux();
115 
116  int limitLevel = 2;
117  pW = new Pyramid(width, height, 1, false, limitLevel);
118  pI = new Pyramid(width, height, channels, true, limitLevel);
119  pOut = new Pyramid(width, height, channels, true, limitLevel);
120 
121  pOut->setValue(0.0f);
122 
123  for(auto j = 0; j < n; j++) {
124  images[0] = flt_lum.Process(Single(imgIn[j]), images[0]);
125  images[1] = flt_weights.Process(Double(images[0], imgIn[j]), images[1]);
126 
127  //normalization
128  *images[1] /= *images[2];
129 
130  pW->update(images[1]);
131  pI->update(imgIn[j]);
132 
133  pI->mul(pW);
134  pOut->add(pI);
135  }
136 
137  //final result
138  imgOut = pOut->reconstruct(imgOut);
139 
140  float *minVal = imgOut->getMinVal(NULL, NULL);
141  float *maxVal = imgOut->getMaxVal(NULL, NULL);
142 
143  int ind;
144  float minV = Arrayf::getMin(minVal, imgOut->channels, ind);
145  float maxV = Arrayf::getMax(maxVal, imgOut->channels, ind);
146  *imgOut -= minV;
147  *imgOut /= (maxV- minV);
148 
149  imgOut->applyFunction(removeNegative);
150 
151  return imgOut;
152  }
153 
157  void releaseAux()
158  {
159  pW = delete_s(pW);
160  pI = delete_s(pI);
161  pOut = delete_s(pOut);
162  }
163 
164 public:
165 
172  ExposureFusion(float wC = 1.0f, float wE = 1.0f,
173  float wS = 1.0f)
174  {
175  pW = NULL;
176  pI = NULL;
177  pOut = NULL;
178 
180  setToANullVector<Image>(images, 3);
181 
182  update(wC, wE, wS);
183  }
184 
186  {
187  release();
188  }
189 
196  void update(float wC = 1.0f, float wE = 1.0f,
197  float wS = 1.0f)
198  {
199  flt_weights.update(wC, wE, wS);
200  }
201 
208  static Image* execute(Image *imgIn, Image *imgOut)
209  {
210  ExposureFusion ef(1.0f, 1.0f, 1.0f);
211  return ef.Process(Single(imgIn), imgOut);
212  }
213 
220  static Image* executeStack(ImageVec imgIn, Image *imgOut)
221  {
222  ExposureFusion ef(1.0f, 1.0f, 1.0f);
223  return ef.Process(imgIn, imgOut);
224  }
225 };
226 
227 
228 } // end namespace pic
229 
230 #endif /* PIC_TONE_MAPPING_EXPOSURE_FUSION_HPP */
231 
PIC_INLINE bool ImageVecCheck(ImageVec &imgIn, int minInputImages)
ImageVecCheck.
Definition: image_vec.hpp:147
static T getMax(T *data, int size, int &ind)
getMax
Definition: array.hpp:516
int size() const
size computes the number of values.
Definition: image.hpp:481
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: filter_radial_basis_function.hpp:29
Image * Process(ImageVec imgIn, Image *imgOut=NULL)
Process.
Definition: tone_mapping_operator.hpp:120
void add(const Pyramid *pyr)
add is the add operator ( += ) between pyramids.
Definition: pyramid.hpp:368
int channels
Definition: image.hpp:80
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
Definition: filter_luminance.hpp:28
void update(LUMINANCE_TYPE type=LT_CIE_LUMINANCE)
update
Definition: filter_luminance.hpp:135
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
void setValue(float value)
SetValue.
Definition: pyramid.hpp:350
ImageVec images
Definition: tone_mapping_operator.hpp:35
void update(float wC=1.0f, float wE=1.0f, float wS=1.0f)
update
Definition: filter_exposure_fusion_weights.hpp:99
FilterLuminance flt_lum
Definition: exposure_fusion.hpp:42
The ToneMappingOperator class.
Definition: tone_mapping_operator.hpp:31
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
The FilterLuminance class.
Definition: filter_luminance.hpp:33
Image * ProcessAux(ImageVec imgIn, Image *imgOut)
ProcessAux.
Definition: exposure_fusion.hpp:63
float * getMinVal(BBox *box, float *ret)
getMinVal computes the minimum value for the current Image.
ExposureFusion(float wC=1.0f, float wE=1.0f, float wS=1.0f)
ExposureFusion.
Definition: exposure_fusion.hpp:172
The ExposureFusion class.
Definition: exposure_fusion.hpp:39
void updateImage(Image *imgIn)
updateImage
Definition: tone_mapping_operator.hpp:78
Pyramid * pOut
Definition: exposure_fusion.hpp:45
~ExposureFusion()
Definition: exposure_fusion.hpp:185
void release()
release
Definition: tone_mapping_operator.hpp:68
Image * ProcessAuxStack(ImageVec imgIn, Image *imgOut)
ProcessAuxStack.
Definition: exposure_fusion.hpp:84
PIC_INLINE ImageVec Double(Image *img1, Image *img2)
Double creates an std::vector which contains img1 and img2; this is for filters input.
Definition: image_vec.hpp:49
void releaseAux()
releaseAux
Definition: exposure_fusion.hpp:157
Pyramid * pW
Definition: exposure_fusion.hpp:45
void update(Image *img)
update recomputes the pyramid given a compatible image, img.
Definition: pyramid.hpp:280
The FilterExposureFusionWeights class.
Definition: filter_exposure_fusion_weights.hpp:30
Image * reconstruct(Image *imgOut)
reconstruct evaluates a Gaussian/Laplacian pyramid.
Definition: pyramid.hpp:320
static float removeNegative(float x)
removeNegative
Definition: exposure_fusion.hpp:52
static T getMin(T *data, int size, int &ind)
getMin
Definition: array.hpp:542
void applyFunction(float(*func)(float))
applyFunction is an operator that applies an input function to all values in data.
The Image class stores an image as buffer of float.
Definition: image.hpp:60
PIC_INLINE ImageVec getAllExposuresImages(Image *imgIn, std::vector< float > &fstops, float gamma=2.2f)
getAllExposuresImages converts an image into a stack of images.
Definition: get_all_exposures.hpp:144
static Image * execute(Image *imgIn, Image *imgOut)
execute
Definition: exposure_fusion.hpp:208
The Pyramid class.
Definition: pyramid.hpp:36
FilterExposureFusionWeights flt_weights
Definition: exposure_fusion.hpp:43
void update(float wC=1.0f, float wE=1.0f, float wS=1.0f)
update
Definition: exposure_fusion.hpp:196
static Image * executeStack(ImageVec imgIn, Image *imgOut)
executeStack
Definition: exposure_fusion.hpp:220
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
void mul(const Pyramid *pyr)
mul is the mul operator ( *= ) between pyramids.
Definition: pyramid.hpp:357
#define MAX(a, b)
Definition: math.hpp:73
float * getMaxVal(BBox *box, float *ret)
getMaxVal computes the maximum value for the current Image.
Pyramid * pI
Definition: exposure_fusion.hpp:45