Open3D (C++ API)  0.17.0
Image.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <limits>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "open3d/core/Dtype.h"
16 #include "open3d/core/Tensor.h"
18 #include "open3d/geometry/Image.h"
20 
21 namespace open3d {
22 namespace t {
23 namespace geometry {
24 
29 class Image : public Geometry {
30 public:
47  Image(int64_t rows = 0,
48  int64_t cols = 0,
49  int64_t channels = 1,
50  core::Dtype dtype = core::Float32,
51  const core::Device &device = core::Device("CPU:0"));
52 
58  Image(const core::Tensor &tensor);
59 
60  virtual ~Image() override {}
61 
62 public:
65  Image &Clear() override {
66  data_ = core::Tensor({0, 0, GetChannels()}, GetDtype(), GetDevice());
67  return *this;
68  }
69 
71  bool IsEmpty() const override {
72  return GetRows() * GetCols() * GetChannels() == 0;
73  }
74 
76  Image &Reset(int64_t rows = 0,
77  int64_t cols = 0,
78  int64_t channels = 1,
79  core::Dtype dtype = core::Float32,
80  const core::Device &device = core::Device("CPU:0"));
81 
82 public:
84  int64_t GetRows() const { return data_.GetShape()[0]; }
85 
87  int64_t GetCols() const { return data_.GetShape()[1]; }
88 
90  int64_t GetChannels() const { return data_.GetShape()[2]; }
91 
93  core::Dtype GetDtype() const { return data_.GetDtype(); }
94 
96  core::Device GetDevice() const override { return data_.GetDevice(); }
97 
104  core::Tensor At(int64_t r, int64_t c) const {
105  if (GetChannels() == 1) {
106  return data_[r][c][0];
107  } else {
108  return data_[r][c];
109  }
110  }
111 
113  core::Tensor At(int64_t r, int64_t c, int64_t ch) const {
114  return data_[r][c][ch];
115  }
116 
118  void *GetDataPtr() { return data_.GetDataPtr(); }
119 
121  const void *GetDataPtr() const { return data_.GetDataPtr(); }
122 
124  core::Tensor AsTensor() const { return data_; }
125 
132  Image To(const core::Device &device, bool copy = false) const {
133  return Image(data_.To(device, copy));
134  }
135 
137  Image Clone() const { return To(GetDevice(), /*copy=*/true); }
138 
147  Image To(core::Dtype dtype,
148  bool copy = false,
150  double offset = 0.0) const;
151 
161  Image &LinearTransform(double scale = 1.0, double offset = 0.0) {
162  To(GetDtype(), false, scale, offset);
163  return *this;
164  }
165 
169  Image RGBToGray() const;
170 
172  enum class InterpType {
173  Nearest = 0,
174  Linear = 1,
175  Cubic = 2,
176  Lanczos = 3,
177  Super = 4
178  };
179 
185  Image Resize(float sampling_rate = 0.5f,
186 
187  InterpType interp_type = InterpType::Nearest) const;
188 
196  Image Dilate(int kernel_size = 3) const;
197 
199  Image Filter(const core::Tensor &kernel) const;
200 
211  Image FilterBilateral(int kernel_size = 3,
212  float value_sigma = 20.0f,
213  float distance_sigma = 10.0f) const;
214 
219  Image FilterGaussian(int kernel_size = 3, float sigma = 1.0f) const;
220 
225  std::pair<Image, Image> FilterSobel(int kernel_size = 3) const;
226 
233  Image PyrDown() const;
234 
248  Image PyrDownDepth(float diff_threshold, float invalid_fill = 0.f) const;
249 
262  Image ClipTransform(float scale,
263  float min_value,
264  float max_value,
265  float clip_fill = 0.0f) const;
266 
279  Image CreateVertexMap(const core::Tensor &intrinsics,
280  float invalid_fill = 0.0f);
281 
296  Image CreateNormalMap(float invalid_fill = 0.0f);
297 
306  Image ColorizeDepth(float scale, float min_value, float max_value);
307 
310  return core::Tensor::Zeros({2}, core::Int64);
311  }
312 
315  return core::Tensor(std::vector<int64_t>{GetRows(), GetCols()}, {2},
316  core::Int64);
317  }
318 
320  static Image FromLegacy(const open3d::geometry::Image &image_legacy,
321  const core::Device &Device = core::Device("CPU:0"));
322 
325 
327  std::string ToString() const;
328 
330 #ifdef WITH_IPPICV
331  static constexpr bool HAVE_IPPICV = true;
332 #else
333  static constexpr bool HAVE_IPPICV = false;
334 #endif
335 
336 protected:
341 };
342 
343 } // namespace geometry
344 } // namespace t
345 } // namespace open3d
bool copy
Definition: VtkUtils.cpp:73
Definition: Device.h:18
Definition: Dtype.h:20
Definition: Tensor.h:32
T * GetDataPtr()
Definition: Tensor.h:1133
SizeVector GetShape() const
Definition: Tensor.h:1116
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:373
Device GetDevice() const override
Definition: Tensor.cpp:1365
Dtype GetDtype() const
Definition: Tensor.h:1153
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:706
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:34
The base geometry class.
Definition: Geometry.h:21
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
Image Clone() const
Returns copy of the image on the same device.
Definition: Image.h:137
void * GetDataPtr()
Get raw buffer of the Image data.
Definition: Image.h:118
core::Tensor AsTensor() const
Returns the underlying Tensor of the Image.
Definition: Image.h:124
core::Device GetDevice() const override
Get device of the image.
Definition: Image.h:96
int64_t GetChannels() const
Get the number of channels of the image.
Definition: Image.h:90
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:113
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:232
Image & LinearTransform(double scale=1.0, double offset=0.0)
Function to linearly transform pixel intensities in place.
Definition: Image.h:161
int64_t GetRows() const
Get the number of rows of the image.
Definition: Image.h:84
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:30
Image ColorizeDepth(float scale, float min_value, float max_value)
Colorize an input depth image (with Dtype UInt16 or Float32).
Definition: Image.cpp:475
InterpType
Image interpolation algorithms.
Definition: Image.h:172
@ Super
Super sampling interpolation (only downsample).
@ Lanczos
Lanczos filter interpolation.
@ Linear
Bilinear interpolation.
@ Nearest
Nearest neighbors interpolation.
static constexpr bool HAVE_IPPICV
Do we use IPP ICV for accelerating image processing operations?
Definition: Image.h:333
core::Tensor GetMinBound() const
Compute min 2D coordinates for the data (always {0, 0}).
Definition: Image.h:309
Image PyrDown() const
Return a new downsampled image with pyramid downsampling.
Definition: Image.cpp:388
bool IsEmpty() const override
Returns true if rows * cols * channels == 0.
Definition: Image.h:71
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:393
Image RGBToGray() const
Converts a 3-channel RGB image to a new 1-channel Grayscale image.
Definition: Image.cpp:119
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:498
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:410
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:338
int64_t GetCols() const
Get the number of columns of the image.
Definition: Image.h:87
Image CreateVertexMap(const core::Tensor &intrinsics, float invalid_fill=0.0f)
Create a vertex map from a depth image using unprojection.
Definition: Image.cpp:442
Image Dilate(int kernel_size=3) const
Return a new image after performing morphological dilation.
Definition: Image.cpp:196
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:155
Image FilterGaussian(int kernel_size=3, float sigma=1.0f) const
Return a new image after Gaussian filtering.
Definition: Image.cpp:302
const void * GetDataPtr() const
Get raw buffer of the Image data.
Definition: Image.h:121
virtual ~Image() override
Definition: Image.h:60
Image & Clear() override
Clear image contents by resetting the rows and cols to 0, while keeping channels, dtype and device un...
Definition: Image.h:65
open3d::geometry::Image ToLegacy() const
Convert to legacy Image type.
Definition: Image.cpp:527
Image Filter(const core::Tensor &kernel) const
Return a new image after filtering with the given kernel.
Definition: Image.cpp:271
Image CreateNormalMap(float invalid_fill=0.0f)
Create a normal map from a vertex map.
Definition: Image.cpp:460
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:55
Image To(const core::Device &device, bool copy=false) const
Transfer the image to a specified device.
Definition: Image.h:132
core::Tensor data_
Definition: Image.h:340
core::Tensor At(int64_t r, int64_t c) const
Get pixel(s) in the image.
Definition: Image.h:104
std::string ToString() const
Text description.
Definition: Image.cpp:548
core::Dtype GetDtype() const
Get dtype of the image.
Definition: Image.h:93
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:314
Definition: Optional.h:259
int offset
Definition: FilePCD.cpp:45
const Dtype Int64
Definition: Dtype.cpp:47
const Dtype Float32
Definition: Dtype.cpp:42
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:649
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:647
constexpr nullopt_t nullopt
Definition: Optional.h:152
Definition: PinholeCameraIntrinsic.cpp:16