Open3D (C++ API)  0.15.1
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"
39
40namespace open3d {
41namespace t {
42namespace geometry {
43
48class Image : public Geometry {
49public:
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
81public:
84 Image &Clear() override {
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
101public:
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},
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
353protected:
358};
359
360} // namespace geometry
361} // namespace t
362} // namespace open3d
Definition: Device.h:39
Definition: Dtype.h:39
Definition: Tensor.h:51
SizeVector GetShape() const
Definition: Tensor.h:1091
T * GetDataPtr()
Definition: Tensor.h:1108
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:380
Dtype GetDtype() const
Definition: Tensor.h:1128
Device GetDevice() const
Definition: Tensor.cpp:1365
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:713
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:53
The base geometry class.
Definition: Geometry.h:38
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
void * GetDataPtr()
Get raw buffer of the Image data.
Definition: Image.h:137
Image Clone() const
Returns copy of the image on the same device.
Definition: Image.h:156
core::Tensor AsTensor() const
Returns the underlying Tensor of the Image.
Definition: Image.h:143
const void * GetDataPtr() const
Get raw buffer of the Image data.
Definition: Image.h:140
int64_t GetChannels() const
Get the number of channels of the image.
Definition: Image.h:109
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 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:254
int64_t GetRows() const
Get the number of rows of the image.
Definition: Image.h:103
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
Image ColorizeDepth(float scale, float min_value, float max_value)
Colorize an input depth image (with Dtype UInt16 or Float32).
Definition: Image.cpp:501
core::Device GetDevice() const
Get device of the image.
Definition: Image.h:115
InterpType
Image interpolation algorithms.
Definition: Image.h:191
@ Nearest
Nearest neighbors interpolation.
static constexpr bool HAVE_IPPICV
Do we use IPP ICV for accelerating image processing operations?
Definition: Image.h:350
core::Tensor GetMinBound() const
Compute min 2D coordinates for the data (always {0, 0}).
Definition: Image.h:326
Image PyrDown() const
Return a new downsampled image with pyramid downsampling.
Definition: Image.cpp:414
bool IsEmpty() const override
Returns true if rows * cols * channels == 0.
Definition: Image.h:90
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:419
Image RGBToGray() const
Converts a 3-channel RGB image to a new 1-channel Grayscale image.
Definition: Image.cpp:138
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:524
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:436
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:363
int64_t GetCols() const
Get the number of columns of the image.
Definition: Image.h:106
Image CreateVertexMap(const core::Tensor &intrinsics, float invalid_fill=0.0f)
Create a vertex map from a depth image using unprojection.
Definition: Image.cpp:468
Image Dilate(int kernel_size=3) const
Return a new image after performing morphological dilation.
Definition: Image.cpp:217
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:175
Image FilterGaussian(int kernel_size=3, float sigma=1.0f) const
Return a new image after Gaussian filtering.
Definition: Image.cpp:326
virtual ~Image() override
Definition: Image.h:79
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
open3d::geometry::Image ToLegacy() const
Convert to legacy Image type.
Definition: Image.cpp:553
Image Filter(const core::Tensor &kernel) const
Return a new image after filtering with the given kernel.
Definition: Image.cpp:294
Image CreateNormalMap(float invalid_fill=0.0f)
Create a normal map from a vertex map.
Definition: Image.cpp:486
Image & LinearTransform(double scale=1.0, double offset=0.0)
Function to linearly transform pixel intensities in place.
Definition: Image.h:180
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 To(const core::Device &device, bool copy=false) const
Transfer the image to a specified device.
Definition: Image.h:151
core::Tensor data_
Definition: Image.h:357
core::Tensor At(int64_t r, int64_t c) const
Get pixel(s) in the image.
Definition: Image.h:123
std::string ToString() const
Text description.
Definition: Image.cpp:574
core::Dtype GetDtype() const
Get dtype of the image.
Definition: Image.h:112
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:331
Definition: Optional.h:278
int offset
Definition: FilePCD.cpp:64
const Dtype Int64
Definition: Dtype.cpp:66
const Dtype Float32
Definition: Dtype.cpp:61
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:668
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:666
constexpr nullopt_t nullopt
Definition: Optional.h:171
Definition: PinholeCameraIntrinsic.cpp:35