Open3D (C++ API)
Image.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <vector>
30 #include <memory>
31 #include <Eigen/Core>
32 
34 #include <Open3D/Utility/Console.h>
35 
36 namespace open3d {
37 
38 namespace camera {
39 class PinholeCameraIntrinsic;
40 }
41 
42 namespace geometry {
43 
44 class Image : public Geometry2D {
45 public:
47  Equal,
48  Weighted,
49  };
50 
51  enum class FilterType {
52  Gaussian3,
53  Gaussian5,
54  Gaussian7,
55  Sobel3Dx,
56  Sobel3Dy
57  };
58 
59 public:
61  ~Image() override{};
62 
63 public:
64  void Clear() override;
65  bool IsEmpty() const override;
66  Eigen::Vector2d GetMinBound() const override;
67  Eigen::Vector2d GetMaxBound() const override;
68  bool TestImageBoundary(double u, double v, double inner_margin = 0.0) const;
69 
70 public:
71  virtual bool HasData() const {
72  return width_ > 0 && height_ > 0 &&
73  data_.size() == height_ * BytesPerLine();
74  }
75 
76  void PrepareImage(int width,
77  int height,
78  int num_of_channels,
79  int bytes_per_channel) {
80  width_ = width;
81  height_ = height;
82  num_of_channels_ = num_of_channels;
83  bytes_per_channel_ = bytes_per_channel;
84  AllocateDataBuffer();
85  }
86 
87  int BytesPerLine() const {
88  return width_ * num_of_channels_ * bytes_per_channel_;
89  }
90 
93  std::pair<bool, double> FloatValueAt(double u, double v) const;
94 
95 protected:
97  data_.resize(width_ * height_ * num_of_channels_ * bytes_per_channel_);
98  }
99 
100 public:
101  int width_ = 0;
102  int height_ = 0;
103  int num_of_channels_ = 0;
104  int bytes_per_channel_ = 0;
105  std::vector<uint8_t> data_;
106 };
107 
115  const camera::PinholeCameraIntrinsic &intrinsic);
116 
118 std::shared_ptr<Image> CreateFloatImageFromImage(
119  const Image &image,
121  Image::ColorToIntensityConversionType::Weighted);
122 
124 template <typename T>
125 T *PointerAt(const Image &image, int u, int v);
126 
128 template <typename T>
129 T *PointerAt(const Image &image, int u, int v, int ch);
130 
131 std::shared_ptr<Image> ConvertDepthToFloatImage(const Image &depth,
132  double depth_scale = 1000.0,
133  double depth_trunc = 3.0);
134 
135 std::shared_ptr<Image> FlipImage(const Image &input);
136 
138 std::shared_ptr<Image> FilterImage(const Image &input, Image::FilterType type);
139 
141 std::shared_ptr<Image> FilterImage(const Image &input,
142  const std::vector<double> &dx,
143  const std::vector<double> &dy);
144 
145 std::shared_ptr<Image> FilterHorizontalImage(const Image &input,
146  const std::vector<double> &kernel);
147 
149 std::shared_ptr<Image> DownsampleImage(const Image &input);
150 
152 std::shared_ptr<Image> DilateImage(const Image &input,
153  int half_kernel_size = 1);
154 
157 void LinearTransformImage(Image &input,
158  double scale = 1.0,
159  double offset = 0.0);
160 
164 void ClipIntensityImage(Image &input, double min = 0.0, double max = 1.0);
165 
169 template <typename T>
170 std::shared_ptr<Image> CreateImageFromFloatImage(const Image &input);
171 
173 typedef std::vector<std::shared_ptr<Image>> ImagePyramid;
174 
176 ImagePyramid FilterImagePyramid(const ImagePyramid &input,
178 
180 ImagePyramid CreateImagePyramid(const Image &image,
181  size_t num_of_levels,
182  bool with_gaussian_filter = true);
183 
185 std::shared_ptr<Image> CreateDepthBoundaryMask(
186  const Image &depth_image_input,
187  double depth_threshold_for_discontinuity_check = 0.1,
188  int half_dilation_kernel_size_for_discontinuity_map = 3);
189 
190 } // namespace geometry
191 } // namespace open3d
std::vector< std::shared_ptr< Image > > ImagePyramid
Typedef and functions for ImagePyramid.
Definition: Image.h:173
Definition: Geometry.h:32
int offset
Definition: FilePCD.cpp:60
Definition: PinholeCameraIntrinsic.h:41
ImagePyramid FilterImagePyramid(const ImagePyramid &input, Image::FilterType type)
Function to filter image pyramid.
Definition: Image.cpp:249
T * PointerAt(const Image &image, int u, int v)
Function to access the raw data of a single-channel Image.
Definition: Image.cpp:86
~Image() override
Definition: Image.h:61
void PrepareImage(int width, int height, int num_of_channels, int bytes_per_channel)
Definition: Image.h:76
std::shared_ptr< Image > FilterHorizontalImage(const Image &input, const std::vector< double > &kernel)
Definition: Image.cpp:186
std::shared_ptr< Image > DilateImage(const Image &input, int half_kernel_size)
Function to dilate 8bit mask map.
Definition: Image.cpp:296
Definition: Geometry2D.h:35
std::shared_ptr< Image > CreateImageFromFloatImage(const Image &input)
Definition: ImageFactory.cpp:126
Image()
Definition: Image.h:60
virtual bool HasData() const
Definition: Image.h:71
std::shared_ptr< Image > FilterImage(const Image &input, Image::FilterType type)
Function to filter image with pre-defined filtering type.
Definition: Image.cpp:219
void ClipIntensityImage(Image &input, double min, double max)
Definition: Image.cpp:127
void AllocateDataBuffer()
Definition: Image.h:96
char type
Definition: FilePCD.cpp:56
std::vector< uint8_t > data_
Definition: Image.h:105
FilterType
Definition: Image.h:51
ImagePyramid CreateImagePyramid(const Image &image, size_t num_of_levels, bool with_gaussian_filter=true)
Function to create image pyramid.
Definition: ImageFactory.cpp:150
void LinearTransformImage(Image &input, double scale, double offset)
Definition: Image.cpp:144
Definition: PinholeCameraIntrinsic.cpp:33
int BytesPerLine() const
Definition: Image.h:87
std::shared_ptr< Image > DownsampleImage(const Image &input)
Function to 2x image downsample using simple 2x2 averaging.
Definition: Image.cpp:160
GeometryType
Definition: Geometry.h:34
int height
Definition: FilePCD.cpp:68
std::shared_ptr< Image > CreateFloatImageFromImage(const Image &image, Image::ColorToIntensityConversionType type=Image::ColorToIntensityConversionType::Weighted)
Return a gray scaled float type image.
Definition: ImageFactory.cpp:64
std::shared_ptr< Image > FlipImage(const Image &input)
Definition: Image.cpp:275
std::shared_ptr< Image > CreateDepthToCameraDistanceMultiplierFloatImage(const camera::PinholeCameraIntrinsic &intrinsic)
Definition: ImageFactory.cpp:34
std::shared_ptr< Image > ConvertDepthToFloatImage(const Image &depth, double depth_scale, double depth_trunc)
Definition: Image.cpp:110
std::shared_ptr< Image > CreateDepthBoundaryMask(const Image &depth_image_input, double depth_threshold_for_discontinuity_check, int half_dilation_kernel_size_for_discontinuity_map)
Function to create a depthmap boundary mask from depth image.
Definition: Image.cpp:328
Definition: Image.h:44
ColorToIntensityConversionType
Definition: Image.h:46
int width
Definition: FilePCD.cpp:67