AliceVision
Photogrammetric Computer Vision Framework
laplacianCompositer.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2020 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include "compositer.hpp"
10 #include "feathering.hpp"
11 #include "laplacianPyramid.hpp"
12 
13 namespace aliceVision {
14 
16 {
17  public:
18  LaplacianCompositer(size_t outputWidth, size_t outputHeight, size_t scale)
19  : Compositer(outputWidth, outputHeight),
20  _pyramidPanorama(outputWidth, outputHeight, scale + 1),
21  _bands(scale + 1)
22  {}
23 
24  virtual ~LaplacianCompositer() {}
25 
26  virtual bool initialize(const BoundingBox& outputRoi)
27  {
28  _outputRoi = outputRoi;
29 
30  if (_outputRoi.left < 0)
31  return false;
32  if (_outputRoi.top < 0)
33  return false;
34  if (_outputRoi.getRight() >= _panoramaWidth)
35  return false;
36  if (_outputRoi.getBottom() >= _panoramaHeight)
37  return false;
38 
39  return _pyramidPanorama.initialize();
40  }
41 
42  virtual int getBorderSize() const { return _gaussianFilterRadius; }
43 
44  virtual bool append(aliceVision::image::Image<image::RGBfColor>& color,
47  int offsetX,
48  int offsetY)
49  {
50  // Fill Color images masked parts with fake but coherent info
52  if (!feathering(feathered, color, inputMask))
53  {
54  return false;
55  }
56 
58 
59  // To log space for hdr
60  for (int i = 0; i < feathered.height(); i++)
61  {
62  for (int j = 0; j < feathered.width(); j++)
63  {
64  feathered(i, j).r() = std::log(std::max(1e-8f, feathered(i, j).r()));
65  feathered(i, j).g() = std::log(std::max(1e-8f, feathered(i, j).g()));
66  feathered(i, j).b() = std::log(std::max(1e-8f, feathered(i, j).b()));
67  }
68  }
69 
70  // Convert mask to alpha layer
71  image::Image<float> maskFloat(inputMask.width(), inputMask.height());
72  for (int i = 0; i < inputMask.height(); i++)
73  {
74  for (int j = 0; j < inputMask.width(); j++)
75  {
76  if (inputMask(i, j))
77  {
78  maskFloat(i, j) = 1.0f;
79  }
80  else
81  {
82  maskFloat(i, j) = 0.0f;
83  }
84  }
85  }
86 
87  BoundingBox bb;
88  bb.left = offsetX;
89  bb.top = offsetY;
90  bb.width = feathered.width();
91  bb.height = feathered.height();
92 
93  int scale = _bands - 1;
94  BoundingBox potbb = bb.divide(scale).dilate(getBorderSize()).multiply(scale);
95 
96  BoundingBox contentbb = bb;
97  contentbb.left = bb.left - potbb.left;
98  contentbb.top = bb.top - potbb.top;
99 
100  if (!_pyramidPanorama.apply(feathered, maskFloat, inputWeights, potbb, contentbb))
101  {
102  return false;
103  }
104 
105  return true;
106  }
107 
108  virtual bool terminate()
109  {
110  _panorama = image::Image<image::RGBAfColor>(_outputRoi.width, _outputRoi.height, true, image::RGBAfColor(0.0f, 0.0f, 0.0f, 0.0f));
111 
112  if (!_pyramidPanorama.rebuild(_panorama, _outputRoi))
113  {
114  return false;
115  }
116 
117  for (int i = 0; i < _outputRoi.height; i++)
118  {
119  for (int j = 0; j < _outputRoi.width; j++)
120  {
121  image::RGBAfColor c = _panorama(i, j);
122 
123  image::RGBAfColor out;
124  out.r() = std::exp(c.r());
125  out.g() = std::exp(c.g());
126  out.b() = std::exp(c.b());
127  out.a() = c.a();
128 
129  _panorama(i, j) = out;
130  }
131  }
132 
133  return true;
134  }
135 
136  protected:
137  const int _gaussianFilterRadius = 2;
138  LaplacianPyramid _pyramidPanorama;
139  size_t _bands;
140 };
141 
142 } // namespace aliceVision
aliceVision::image::Rgba::a
const T & a() const
Get readonly Alpha channel value.
Definition: pixelTypes.hpp:271
aliceVision::BoundingBox
Definition: boundingBox.hpp:17
aliceVision::image::Rgba
RGBA templated pixel type.
Definition: pixelTypes.hpp:179
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::image::Rgba::g
const T & g() const
Get readonly Green channel value.
Definition: pixelTypes.hpp:251
aliceVision::image::Image
Definition: ImageDescriber_AKAZE_OCV.hpp:21
aliceVision::LaplacianPyramid
Definition: laplacianPyramid.hpp:15
aliceVision::image::Rgba::r
const T & r() const
Get readonly Red channel value.
Definition: pixelTypes.hpp:241
aliceVision::image::Image::width
int width() const
Retrieve the width of the image.
Definition: Image.hpp:110
aliceVision::LaplacianCompositer
Definition: laplacianCompositer.hpp:15
aliceVision::image::Image::height
int height() const
Retrieve the height of the image.
Definition: Image.hpp:116
aliceVision::Compositer
Definition: compositer.hpp:17
aliceVision::image::Rgba::b
const T & b() const
Get readonly Blue channel value.
Definition: pixelTypes.hpp:261