Open3D (C++ API)  0.13.0
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 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,
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,
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 
161  Image CPU() const { return To(core::Device("CPU:0")); }
162 
167  Image CUDA(int device_id = 0) const {
168  return To(core::Device(core::Device::DeviceType::CUDA, device_id));
169  }
170 
179  Image To(core::Dtype dtype,
180  bool copy = false,
182  double offset = 0.0) const;
183 
193  Image &LinearTransform(double scale = 1.0, double offset = 0.0) {
194  To(GetDtype(), false, scale, offset);
195  return *this;
196  }
197 
201  Image RGBToGray() const;
202 
204  enum class InterpType {
205  Nearest = 0,
206  Linear = 1,
207  Cubic = 2,
208  Lanczos = 3,
209  Super = 4
210  };
216  Image Resize(float sampling_rate = 0.5f,
217  InterpType interp_type = InterpType::Nearest) const;
218 
226  Image Dilate(int kernel_size = 3) const;
227 
229  Image Filter(const core::Tensor &kernel) const;
230 
241  Image FilterBilateral(int kernel_size = 3,
242  float value_sigma = 20.0f,
243  float distance_sigma = 10.0f) const;
244 
249  Image FilterGaussian(int kernel_size = 3, float sigma = 1.0f) const;
250 
255  std::pair<Image, Image> FilterSobel(int kernel_size = 3) const;
256 
263  Image PyrDown() const;
264 
278  Image PyrDownDepth(float diff_threshold, float invalid_fill = 0.f) const;
279 
292  Image ClipTransform(float scale,
293  float min_value,
294  float max_value,
295  float clip_fill = 0.0f) const;
296 
309  Image CreateVertexMap(const core::Tensor &intrinsics,
310  float invalid_fill = 0.0f);
311 
326  Image CreateNormalMap(float invalid_fill = 0.0f);
327 
336  Image ColorizeDepth(float scale, float min_value, float max_value);
337 
341  }
342 
345  return core::Tensor(std::vector<int64_t>{GetRows(), GetCols()}, {2},
347  }
348 
350  static Image FromLegacyImage(
351  const open3d::geometry::Image &image_legacy,
352  const core::Device &Device = core::Device("CPU:0"));
353 
356 
358  std::string ToString() const;
359 
361 #ifdef WITH_IPPICV
362  static constexpr bool HAVE_IPPICV = true;
363 #else
364  static constexpr bool HAVE_IPPICV = false;
365 #endif
366 
367 protected:
372 };
373 
374 } // namespace geometry
375 } // namespace t
376 } // 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:557
Image Clone() const
Returns copy of the image on the same device.
Definition: Image.h:156
constexpr nullopt_t nullopt
Definition: Optional.h:146
Super sampling interpolation (only downsample).
Image & Reset(int64_t rows=0, int64_t cols=0, int64_t channels=1, core::Dtype dtype=core::Dtype::Float32, const core::Device &device=core::Device("CPU:0"))
Reinitialize image with new parameters.
Definition: Image.cpp:74
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:344
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:193
Definition: Dtype.h:39
int offset
Definition: FilePCD.cpp:64
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:293
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:521
Image CUDA(int device_id=0) const
Transfer the image to a CUDA device.
Definition: Image.h:167
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:169
Image PyrDown() const
Return a new downsampled image with pyramid downsampling.
Definition: Image.cpp:462
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:334
virtual ~Image() override
Definition: Image.h:79
Device GetDevice() const
Definition: Tensor.cpp:1098
std::string ToString() const
Text description.
Definition: Image.cpp:632
Dtype GetDtype() const
Definition: Tensor.h:1025
Nearest neighbors interpolation.
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
open3d::geometry::Image ToLegacyImage() const
Convert to legacy Image type.
Definition: Image.cpp:611
core::Tensor data_
Definition: Image.h:371
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:540
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:540
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
Image CPU() const
Transfer the image to CPU.
Definition: Image.h:161
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:240
Image(int64_t rows=0, int64_t cols=0, int64_t channels=1, core::Dtype dtype=core::Dtype::Float32, const core::Device &device=core::Device("CPU:0"))
Constructor for image.
Definition: Image.cpp:49
static const Dtype Float32
Definition: Dtype.h:42
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:339
Image FilterGaussian(int kernel_size=3, float sigma=1.0f) const
Return a new image after Gaussian filtering.
Definition: Image.cpp:370
Definition: Optional.h:54
SizeVector GetShape() const
Definition: Tensor.h:988
The base geometry class.
Definition: Geometry.h:38
InterpType
Image interpolation algorithms.
Definition: Image.h:204
static const Dtype Int64
Definition: Dtype.h:47
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:411
Definition: Tensor.h:50
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:486
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:467
T * GetDataPtr()
Definition: Tensor.h:1005
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
static Image FromLegacyImage(const open3d::geometry::Image &image_legacy, const core::Device &Device=core::Device("CPU:0"))
Create from a legacy Open3D Image.
Definition: Image.cpp:582
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:206
static constexpr bool HAVE_IPPICV
Do we use IPP ICV for accelerating image processing operations?
Definition: Image.h:364
Image Dilate(int kernel_size=3) const
Return a new image after performing morphological dilation.
Definition: Image.cpp:252
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
Retuns the underlying Tensor of the Image.
Definition: Image.h:143