PICCANTE  0.4
The hottest HDR imaging library!
segmentation_tmo_approx.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_ALGORITHMS_SEGMENTATION_TMO_APPROX_HPP
19 #define PIC_ALGORITHMS_SEGMENTATION_TMO_APPROX_HPP
20 
21 #include "../util/array.hpp"
22 #include "../util/std_util.hpp"
23 
24 #include "../algorithms/superpixels_slic.hpp"
25 
26 #include "../filtering/filter_luminance.hpp"
27 #include "../filtering/filter_iterative.hpp"
28 #include "../filtering/filter_bilateral_2ds.hpp"
29 
30 namespace pic {
31 
36 {
37 protected:
41 
42  float perCent;
43 
51  Image *ProcessAux(Image *imgIn, bool bBilateral, int nSuperPixels = 4096)
52  {
53  if(bBilateral) {
54  float minVal, maxVal;
55  imgIn->getMinVal(NULL, &minVal);
56  imgIn->getMaxVal(NULL, &maxVal);
57 
58  float nLevels = log10f(maxVal) - log10f(minVal) + 1.0f;
59  float nLayer = ((maxVal - minVal) / nLevels) / 4.0f;
60  float area = imgIn->widthf * imgIn->heightf * perCent;
61  int iterations = MAX(int(sqrtf(area)) / 2, 1);
62 
63  //create filters
64  if(fltIt == NULL) {
65  fltBil = new FilterBilateral2DS(1.0f, nLayer);
66  fltIt = new FilterIterative(fltBil, iterations);
67  }
68 
69  #ifdef PIC_DEBUG
70  printf("Layer: %f iterations: %d\n", nLayer, iterations);
71  #endif
72  //Iterative bilateral filtering
73  Image *imgOut = fltIt->Process(Single(imgIn), imgIn_flt);
74 
75  return imgOut;
76  } else {
77  Slic sp;
78  sp.execute(imgIn, nSuperPixels);
79  Image *imgOut = sp.getMeanImage(NULL);
80  return imgOut;
81  }
82  }
83 
84 public:
85 
90  {
91  fltBil = NULL;
92  fltIt = NULL;
93  L = NULL;
94  imgIn_flt = NULL;
95 
96  perCent = 0.005f;
97  }
98 
100  {
102  delete_s(L);
103  delete_s(fltIt);
104  delete_s(fltBil);
105  }
106 
113  Image *Process(Image *imgIn, Image *imgOut)
114  {
115  if(imgIn == NULL) {
116  return NULL;
117  }
118 
119  if(!imgIn->isValid() || (imgIn->channels != 3)) {
120  return NULL;
121  }
122 
123  if(imgOut == NULL) {
124  imgOut = new Image(1, imgIn->width, imgIn->height, 1);
125  }
126 
127  //compute luminance
129 
130  Image *imgIn_flt = ProcessAux(imgIn, true, 4096);
131 
132  //threshold
133  float *data = imgIn_flt->data;
134  int channels = imgIn_flt->channels;
135 
136  float *weights = FilterLuminance::computeWeights(LT_CIE_LUMINANCE, channels, NULL);
137 
138  #pragma omp parallel for
139  for(int i = 0; i < imgIn_flt->size(); i += channels) {
140  float L = Arrayf::dot(weights, &data[i], channels) + 1e-7f;
141  imgOut->data[i / 3] = floorf(log10f(L));
142  }
143 
144  delete_vec_s<float>(weights);
145 
146  #ifdef PIC_DEBUG
147  imgOut->Write("Segmentation.pfm");
148  #endif
149 
150  return imgOut;
151  }
152 };
153 
154 } // end namespace pic
155 
156 #endif /* PIC_ALGORITHMS_SEGMENTATION_TMO_APPROX_HPP */
157 
Definition: filter_luminance.hpp:28
int size() const
size computes the number of values.
Definition: image.hpp:481
float * data
data is the main buffer where pixel values are stored.
Definition: image.hpp:91
int channels
Definition: image.hpp:80
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
The Slic class.
Definition: superpixels_slic.hpp:37
float heightf
Definition: image.hpp:84
The Segmentation class.
Definition: segmentation_tmo_approx.hpp:35
Image * L
Definition: segmentation_tmo_approx.hpp:40
float * getMinVal(BBox *box, float *ret)
getMinVal computes the minimum value for the current Image.
~Segmentation()
Definition: segmentation_tmo_approx.hpp:99
Image * ProcessAux(Image *imgIn, bool bBilateral, int nSuperPixels=4096)
ProcessAux.
Definition: segmentation_tmo_approx.hpp:51
static Image * execute(Image *imgIn, Image *imgOut, LUMINANCE_TYPE type=LT_CIE_LUMINANCE)
execute
Definition: filter_luminance.hpp:166
Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter_npasses.hpp:310
Segmentation()
Segmentation.
Definition: segmentation_tmo_approx.hpp:89
FilterBilateral2DS * fltBil
Definition: segmentation_tmo_approx.hpp:39
float widthf
Definition: image.hpp:84
static T dot(T *data0, T *data1, int n)
dot
Definition: array.hpp:281
The Image class stores an image as buffer of float.
Definition: image.hpp:60
void execute(Image *img, int nSuperPixels=64)
execute
Definition: superpixels_slic.hpp:302
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
The FilterIterative class.
Definition: filter_iterative.hpp:28
Image * imgIn_flt
Definition: segmentation_tmo_approx.hpp:40
Definition: bilateral_separation.hpp:25
bool isValid()
isValid checks if the current image is valid, which means if they have an allocated buffer or not...
static float * computeWeights(LUMINANCE_TYPE type, int channels, float *weights)
computeWeights
Definition: filter_luminance.hpp:83
#define MAX(a, b)
Definition: math.hpp:73
float * getMaxVal(BBox *box, float *ret)
getMaxVal computes the maximum value for the current Image.
int width
Definition: image.hpp:80
int height
Definition: image.hpp:80
FilterIterative * fltIt
Definition: segmentation_tmo_approx.hpp:38
bool Write(std::string nameFile, LDR_type typeWrite, int writerCounter)
Write saves an Image into a file on the disk.
Image * getMeanImage(Image *imgOut)
getMeanImage
Definition: superpixels_slic.hpp:437
Image * Process(Image *imgIn, Image *imgOut)
Process.
Definition: segmentation_tmo_approx.hpp:113
The FilterBilateral2DS class.
Definition: filter_bilateral_2ds.hpp:36
float perCent
Definition: segmentation_tmo_approx.hpp:42