Open3D (C++ API)  0.12.0
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 <memory>
30 #include <string>
31 #include <vector>
32 
33 #include "open3d/core/Tensor.h"
34 #include "open3d/geometry/Image.h"
36 
37 namespace open3d {
38 namespace t {
39 namespace geometry {
40 
45 class Image : public Geometry {
46 public:
63  Image(int64_t rows = 0,
64  int64_t cols = 0,
65  int64_t channels = 1,
67  const core::Device &device = core::Device("CPU:0"));
68 
74  Image(const core::Tensor &tensor);
75 
76  virtual ~Image() override {}
77 
78 public:
81  Image &Clear() override {
82  data_ = core::Tensor({0, 0, GetChannels()}, GetDtype(), GetDevice());
83  return *this;
84  }
85 
87  bool IsEmpty() const override {
88  return GetRows() * GetCols() * GetChannels() == 0;
89  }
90 
91 public:
93  int64_t GetRows() const { return data_.GetShape()[0]; }
94 
96  int64_t GetCols() const { return data_.GetShape()[1]; }
97 
99  int64_t GetChannels() const { return data_.GetShape()[2]; }
100 
102  core::Dtype GetDtype() const { return data_.GetDtype(); }
103 
105  core::Device GetDevice() const { return data_.GetDevice(); }
106 
111  core::Tensor At(int64_t r, int64_t c) const {
112  if (GetChannels() == 1) {
113  return data_[r][c][0];
114  } else {
115  return data_[r][c];
116  }
117  }
118 
120  core::Tensor At(int64_t r, int64_t c, int64_t ch) const {
121  return data_[r][c][ch];
122  }
123 
125  void *GetDataPtr() { return data_.GetDataPtr(); }
126 
128  const void *GetDataPtr() const { return data_.GetDataPtr(); }
129 
131  core::Tensor AsTensor() const { return data_; }
132 
136  };
137 
140  return core::Tensor(std::vector<int64_t>{GetRows(), GetCols()}, {2},
142  };
143 
145  static Image FromLegacyImage(
146  const open3d::geometry::Image &image_legacy,
147  const core::Device &Device = core::Device("CPU:0"));
148 
151 
153  std::string ToString() const;
154 
155 protected:
159 };
160 
161 } // namespace geometry
162 } // namespace t
163 } // namespace open3d
int64_t GetChannels() const
Get the number of channels of the image.
Definition: Image.h:99
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:139
int64_t GetRows() const
Get the number of rows of the image.
Definition: Image.h:93
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:120
void * GetDataPtr()
Definition: Tensor.h:961
Definition: Dtype.h:39
bool IsEmpty() const override
Returns true if rows * cols * channels == 0.
Definition: Image.h:87
void * GetDataPtr()
Get raw buffer of the Image data.
Definition: Image.h:125
virtual ~Image() override
Definition: Image.h:76
Device GetDevice() const
Definition: Tensor.cpp:955
std::string ToString() const
Text description.
Definition: Image.cpp:122
Dtype GetDtype() const
Definition: Tensor.h:965
The Image class stores image with customizable rols, cols, channels, dtype and device.
Definition: Image.h:45
open3d::geometry::Image ToLegacyImage() const
Convert to legacy Image type.
Definition: Image.cpp:101
core::Tensor data_
Definition: Image.h:158
core::Device GetDevice() const
Get device of the image.
Definition: Image.h:105
Definition: Device.h:39
int64_t GetCols() const
Get the number of columns of the image.
Definition: Image.h:96
core::Tensor At(int64_t r, int64_t c) const
Definition: Image.h:111
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:182
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:38
static const Dtype Float32
Definition: Dtype.h:42
const void * GetDataPtr() const
Get raw buffer of the Image data.
Definition: Image.h:128
core::Tensor GetMinBound() const
Compute min 2D coordinates for the data (always {0, 0}).
Definition: Image.h:134
SizeVector GetShape() const
Definition: Tensor.h:945
The base geometry class.
Definition: Geometry.h:38
static const Dtype Int64
Definition: Dtype.h:45
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:48
Image & Clear() override
Definition: Image.h:81
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:72
core::Dtype GetDtype() const
Get dtype of the image.
Definition: Image.h:102
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:131