Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2021 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 <limits>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 #include "open3d/core/Dtype.h"
35 #include "open3d/core/Tensor.h"
37 #include "open3d/geometry/Image.h"
39 
40 namespace open3d {
41 namespace t {
42 namespace geometry {
43 
48 class Image : public Geometry {
49 public:
66  Image(int64_t rows = 0,
67  int64_t cols = 0,
68  int64_t channels = 1,
69  core::Dtype dtype = core::Float32,
70  const core::Device &device = core::Device("CPU:0"));
71 
77  Image(const core::Tensor &tensor);
78 
79  virtual ~Image() override {}
80 
81 public:
84  Image &Clear() override {
85  data_ = core::Tensor({0, 0, GetChannels()}, GetDtype(), GetDevice());
86  return *this;
87  }
88 
90  bool IsEmpty() const override {
91  return GetRows() * GetCols() * GetChannels() == 0;
92  }
93 
95  Image &Reset(int64_t rows = 0,
96  int64_t cols = 0,
97  int64_t channels = 1,
98  core::Dtype dtype = core::Float32,
99  const core::Device &device = core::Device("CPU:0"));
100 
101 public:
103  int64_t GetRows() const { return data_.GetShape()[0]; }
104 
106  int64_t GetCols() const { return data_.GetShape()[1]; }
107 
109  int64_t GetChannels() const { return data_.GetShape()[2]; }
110 
112  core::Dtype GetDtype() const { return data_.GetDtype(); }
113 
115  core::Device GetDevice() const { return data_.GetDevice(); }
116 
123  core::Tensor At(int64_t r, int64_t c) const {
124  if (GetChannels() == 1) {
125  return data_[r][c][0];
126  } else {
127  return data_[r][c];
128  }
129  }
130 
132  core::Tensor At(int64_t r, int64_t c, int64_t ch) const {
133  return data_[r][c][ch];
134  }
135 
137  void *GetDataPtr() { return data_.GetDataPtr(); }
138 
140  const void *GetDataPtr() const { return data_.GetDataPtr(); }
141 
143  core::Tensor AsTensor() const { return data_; }
144 
151  Image To(const core::Device &device, bool copy = false) const {
152  return Image(data_.To(device, copy));
153  }
154 
156  Image Clone() const { return To(GetDevice(), /*copy=*/true); }
157 
166  Image To(core::Dtype dtype,
167  bool copy = false,
169  double offset = 0.0) const;
170 
180  Image &LinearTransform(double scale = 1.0, double offset = 0.0) {
181  To(GetDtype(), false, scale, offset);
182  return *this;
183  }
184 
188  Image RGBToGray() const;
189 
191  enum class InterpType {
192  Nearest = 0,
193  Linear = 1,
194  Cubic = 2,
195  Lanczos = 3,
196  Super = 4
197  };
203  Image Resize(float sampling_rate = 0.5f,
204  InterpType interp_type = InterpType::Nearest) const;
205 
213  Image Dilate(int kernel_size = 3) const;
214 
216  Image Filter(const core::Tensor &kernel) const;
217 
228  Image FilterBilateral(int kernel_size = 3,
229  float value_sigma = 20.0f,
230  float distance_sigma = 10.0f) const;
231 
236  Image FilterGaussian(int kernel_size = 3, float sigma = 1.0f) const;
237 
242  std::pair<Image, Image> FilterSobel(int kernel_size = 3) const;
243 
250  Image PyrDown() const;
251 
265  Image PyrDownDepth(float diff_threshold, float invalid_fill = 0.f) const;
266 
279  Image ClipTransform(float scale,
280  float min_value,
281  float max_value,
282  float clip_fill = 0.0f) const;
283 
296  Image CreateVertexMap(const core::Tensor &intrinsics,
297  float invalid_fill = 0.0f);
298 
313  Image CreateNormalMap(float invalid_fill = 0.0f);
314 
323  Image ColorizeDepth(float scale, float min_value, float max_value);
324 
327  return core::Tensor::Zeros({2}, core::Int64);
328  }
329 
332  return core::Tensor(std::vector<int64_t>{GetRows(), GetCols()}, {2},
333  core::Int64);
334  }
335 
337  static Image FromLegacy(const open3d::geometry::Image &image_legacy,
338  const core::Device &Device = core::Device("CPU:0"));
339 
342 
344  std::string ToString() const;
345 
347 #ifdef WITH_IPPICV
348  static constexpr bool HAVE_IPPICV = true;
349 #else
350  static constexpr bool HAVE_IPPICV = false;
351 #endif
352 
353 protected:
358 };
359 
360 } // namespace geometry
361 } // namespace t
362 } // namespace open3d
int64_t GetChannels() const
Get the number of channels of the image.
Definition: Image.h:109
Image ColorizeDepth(float scale, float min_value, float max_value)
Colorize an input depth image (with Dtype UInt16 or Float32).
Definition: Image.cpp:531
Image & Reset(int64_t rows=0, int64_t cols=0, int64_t channels=1, core::Dtype dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Reinitialize image with new parameters.
Definition: Image.cpp:74
Image Clone() const
Returns copy of the image on the same device.
Definition: Image.h:156
constexpr nullopt_t nullopt
Definition: Optional.h:171
Super sampling interpolation (only downsample).
static Image FromLegacy(const open3d::geometry::Image &image_legacy, const core::Device &Device=core::Device("CPU:0"))
Create from a legacy Open3D Image.
Definition: Image.cpp:554
const Dtype Int64
Definition: Dtype.cpp:66
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:331
int64_t GetRows() const
Get the number of rows of the image.
Definition: Image.h:103
core::Tensor At(int64_t r, int64_t c, int64_t ch) const
Get pixel(s) in the image. Returns a tensor with shape {}.
Definition: Image.h:132
Image & LinearTransform(double scale=1.0, double offset=0.0)
Function to linearly transform pixel intensities in place.
Definition: Image.h:180
Definition: Dtype.h:39
Image FilterBilateral(int kernel_size=3, float value_sigma=20.0f, float distance_sigma=10.0f) const
Return a new image after bilateral filtering.
Definition: Image.cpp:284
bool IsEmpty() const override
Returns true if rows * cols * channels == 0.
Definition: Image.h:90
Image CreateVertexMap(const core::Tensor &intrinsics, float invalid_fill=0.0f)
Create a vertex map from a depth image using unprojection.
Definition: Image.cpp:498
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t max_value
Definition: K4aPlugin.cpp:648
Image RGBToGray() const
Converts a 3-channel RGB image to a new 1-channel Grayscale image.
Definition: Image.cpp:168
const Dtype Float32
Definition: Dtype.cpp:61
Image PyrDown() const
Return a new downsampled image with pyramid downsampling.
Definition: Image.cpp:444
Image(int64_t rows=0, int64_t cols=0, int64_t channels=1, core::Dtype dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Constructor for image.
Definition: Image.cpp:49
void * GetDataPtr()
Get raw buffer of the Image data.
Definition: Image.h:137
Image Filter(const core::Tensor &kernel) const
Return a new image after filtering with the given kernel.
Definition: Image.cpp:324
virtual ~Image() override
Definition: Image.h:79
Device GetDevice() const
Definition: Tensor.cpp:1365
std::string ToString() const
Text description.
Definition: Image.cpp:604
Dtype GetDtype() const
Definition: Tensor.h:1094
Nearest neighbors interpolation.
open3d::geometry::Image ToLegacy() const
Convert to legacy Image type.
Definition: Image.cpp:583
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
core::Tensor data_
Definition: Image.h:357
core::Device GetDevice() const
Get device of the image.
Definition: Image.h:115
Image CreateNormalMap(float invalid_fill=0.0f)
Create a normal map from a vertex map.
Definition: Image.cpp:516
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:713
Definition: Device.h:39
int64_t GetCols() const
Get the number of columns of the image.
Definition: Image.h:106
core::Tensor At(int64_t r, int64_t c) const
Get pixel(s) in the image.
Definition: Image.h:123
int offset
Definition: FilePCD.cpp:64
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:380
const void * GetDataPtr() const
Get raw buffer of the Image data.
Definition: Image.h:140
core::Tensor GetMinBound() const
Compute min 2D coordinates for the data (always {0, 0}).
Definition: Image.h:326
Image FilterGaussian(int kernel_size=3, float sigma=1.0f) const
Return a new image after Gaussian filtering.
Definition: Image.cpp:356
Definition: Optional.h:79
SizeVector GetShape() const
Definition: Tensor.h:1057
The base geometry class.
Definition: Geometry.h:38
InterpType
Image interpolation algorithms.
Definition: Image.h:191
Definition: PinholeCameraIntrinsic.cpp:35
std::pair< Image, Image > FilterSobel(int kernel_size=3) const
Return a pair of new gradient images (dx, dy) after Sobel filtering.
Definition: Image.cpp:393
Image ClipTransform(float scale, float min_value, float max_value, float clip_fill=0.0f) const
Return new image after scaling and clipping image values.
Definition: Image.cpp:466
Image PyrDownDepth(float diff_threshold, float invalid_fill=0.f) const
Edge and invalid value preserving downsampling by 2 specifically for depth images.
Definition: Image.cpp:449
T * GetDataPtr()
Definition: Tensor.h:1074
Image & Clear() override
Clear image contents by resetting the rows and cols to 0, while keeping channels, dtype and device un...
Definition: Image.h:84
Image To(const core::Device &device, bool copy=false) const
Transfer the image to a specified device.
Definition: Image.h:151
Image Resize(float sampling_rate=0.5f, InterpType interp_type=InterpType::Nearest) const
Return a new image after resizing with specified interpolation type.
Definition: Image.cpp:205
static constexpr bool HAVE_IPPICV
Do we use IPP ICV for accelerating image processing operations?
Definition: Image.h:350
Image Dilate(int kernel_size=3) const
Return a new image after performing morphological dilation.
Definition: Image.cpp:247
core::Dtype GetDtype() const
Get dtype of the image.
Definition: Image.h:112
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t min_value
Definition: K4aPlugin.cpp:648
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53
core::Tensor AsTensor() const
Returns the underlying Tensor of the Image.
Definition: Image.h:143