AliceVision
Photogrammetric Computer Vision Framework
Image.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include <aliceVision/numeric/numeric.hpp>
11 #include <aliceVision/image/pixelTypes.hpp>
12 
13 //---------------------------------
14 // Universal Image Processing Algorithm
15 //-- Container for a 2D image
16 //-- This class ensure that the image have a width and a height
17 //-- and a 2D array of T data.
18 //-
19 //-- Data is saved in row major format
20 //-- Pixel access is done with operator(y,x)
21 // [2/3/2011 pierre MOULON]
22 //---------------------------
23 namespace aliceVision {
24 namespace image {
25 template<typename T>
26 class Image : public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
27 {
28  public:
29  typedef T Tpixel; //-- Pixel data type
30  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> Base;
31 
36  inline Image() { Base::resize(0, 0); }
37 
45  inline Image(int width, int height, bool fInit = false, const T val = T())
46  {
47  Base::resize(height, width);
48  if (fInit)
49  {
50  Base::fill(val);
51  }
52  };
53 
58  inline Image(const Base& I)
59  : Base(I)
60  {}
61 
66  inline Image(Base&& src)
67  : Base(std::move(src))
68  {}
69 
75  inline Image& operator=(const Base& I)
76  {
77  Base::operator=(I);
78  return *this;
79  }
80 
84  virtual ~Image() = default;
85  //-- Image construction method
86  //------------------------------
87 
95  inline void resize(int width, int height, bool fInit = true, const T val = T())
96  {
97  Base::resize(height, width);
98  if (fInit)
99  {
100  Base::fill(val);
101  }
102  }
103 
104  //------------------------------
105  //-- accessors/getters methods
110  inline int width() const { return static_cast<int>(Base::cols()); }
111 
116  inline int height() const { return static_cast<int>(Base::rows()); }
117 
123  inline int depth() const { return sizeof(Tpixel); }
124 
130  inline unsigned long long int memorySize() const
131  {
132  return static_cast<unsigned long long int>(width()) * static_cast<unsigned long long int>(height()) *
133  static_cast<unsigned long long int>(depth());
134  }
135 
140  inline int channels() const { return NbChannels<Tpixel>::size; }
141 
148  inline const T& operator()(int y, int x) const { return Base::operator()(y, x); }
149 
156  inline T& operator()(int y, int x) { return Base::operator()(y, x); }
157 
158  inline const T& operator()(int i) const { return Base::operator()(i); }
159  inline T& operator()(int i) { return Base::operator()(i); }
160 
165  inline const Base& getMat() const { return (*this); }
166  inline Base& getMat() { return (*this); }
167 
168  //-- accessors/getters methods
169  //------------------------------
170 
178  inline bool contains(int y, int x) const { return 0 <= x && x < Base::cols() && 0 <= y && y < Base::rows(); }
179 
187  template<typename T1>
188  friend Image<T1> operator+(const Image<T1>& imgA, const Image<T1>& imgB);
189 
197  template<typename T1>
198  friend Image<T1> operator-(const Image<T1>& imgA, const Image<T1>& imgB);
199 
200  template<class UnaryFunction>
201  bool perPixelOperation(UnaryFunction f)
202  {
203  for (auto row : this->rowwise())
204  {
205  std::transform(row.begin(), row.end(), row.begin(), f);
206  }
207 
208  return true;
209  }
210 
211  protected:
212  //-- Image data are stored by inheritance of a matrix
213 };
214 
222 template<typename T1>
223 Image<T1> operator+(const Image<T1>& imgA, const Image<T1>& imgB)
224 {
225  return Image<T1>(imgA.Image<T1>::operator+(imgB));
226 }
227 
235 template<typename T1>
236 Image<T1> operator-(const Image<T1>& imgA, const Image<T1>& imgB)
237 {
238  return Image<T1>(imgA.Image<T1>::operator-(imgB));
239 }
240 
241 template<typename T>
242 T getInterpolateColor(const Image<T>& img, double y, double x)
243 {
244  const int xp = std::min(static_cast<int>(x), img.width() - 2);
245  const int yp = std::min(static_cast<int>(y), img.height() - 2);
246 
247  // precision to 4 decimal places
248  const float ui = x - static_cast<float>(xp);
249  const float vi = y - static_cast<float>(yp);
250 
251  const T lu = img(yp, xp);
252  const T ru = img(yp, (xp + 1));
253  const T rd = img((yp + 1), (xp + 1));
254  const T ld = img((yp + 1), xp);
255 
256  // bilinear interpolation of the pixel intensity value
257  const T u = lu + (ru - lu) * ui;
258  const T d = ld + (rd - ld) * ui;
259  const T out = u + (d - u) * vi;
260  return out;
261 }
262 } // namespace image
263 } // namespace aliceVision
aliceVision::image::Image::~Image
virtual ~Image()=default
destructor
aliceVision::image::Image::Image
Image(const Base &I)
Copy constructor.
Definition: Image.hpp:58
aliceVision::image::Rgba
RGBA templated pixel type.
Definition: pixelTypes.hpp:179
aliceVision::image::Image::Image
Image(int width, int height, bool fInit=false, const T val=T())
Full constructor.
Definition: Image.hpp:45
aliceVision::image::Image::getMat
const Base & getMat() const
Get low level access to the internal pixel data.
Definition: Image.hpp:165
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::image::Image::operator=
Image & operator=(const Base &I)
Assignment operator.
Definition: Image.hpp:75
aliceVision::image::Image::operator()
T & operator()(int y, int x)
random pixel access
Definition: Image.hpp:156
aliceVision::image::Image::Image
Image(Base &&src)
Move constructor.
Definition: Image.hpp:66
aliceVision::image::Image
Definition: ImageDescriber_AKAZE_OCV.hpp:21
aliceVision::image::Image::operator+
friend Image< T1 > operator+(const Image< T1 > &imgA, const Image< T1 > &imgB)
Pixelwise addition of two images.
Definition: Image.hpp:223
aliceVision::image::Image::width
int width() const
Retrieve the width of the image.
Definition: Image.hpp:110
aliceVision::image::NbChannels
Definition: pixelTypes.hpp:359
aliceVision::image::Image::operator-
friend Image< T1 > operator-(const Image< T1 > &imgA, const Image< T1 > &imgB)
Pixelwise subtraction of two images.
Definition: Image.hpp:236
aliceVision::image::Image::height
int height() const
Retrieve the height of the image.
Definition: Image.hpp:116
aliceVision::image::Image::contains
bool contains(int y, int x) const
Tell if a point is inside the image.
Definition: Image.hpp:178
aliceVision::image::Image::resize
void resize(int width, int height, bool fInit=true, const T val=T())
Change geometry of image.
Definition: Image.hpp:95
aliceVision::image::Image::depth
int depth() const
Return the depth in byte of the pixel.
Definition: Image.hpp:123
aliceVision::image::Image::Image
Image()
Default constructor.
Definition: Image.hpp:36
aliceVision::image::Image::channels
int channels() const
Return the number of channels.
Definition: Image.hpp:140
aliceVision::image::Image::operator()
const T & operator()(int y, int x) const
constant random pixel access
Definition: Image.hpp:148
aliceVision::image::Image::memorySize
unsigned long long int memorySize() const
Retrieve the size in byte of the image.
Definition: Image.hpp:130