ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Image.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.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 "cloudViewer/core/Dtype.h"
20 
21 namespace cloudViewer {
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 
136  // ---------------------------------------------------------------------
137  // Backward-compatibility helper for pre-v0.19 API
138  // ---------------------------------------------------------------------
140  Image CPU() const { return To(core::Device("CPU:0"), false); }
141 
143  Image Clone() const { return To(GetDevice(), /*copy=*/true); }
144 
153  Image To(core::Dtype dtype,
154  bool copy = false,
156  double offset = 0.0) const;
157 
167  Image &LinearTransform(double scale = 1.0, double offset = 0.0) {
168  To(GetDtype(), false, scale, offset);
169  return *this;
170  }
171 
175  Image RGBToGray() const;
176 
178  enum class InterpType {
179  Nearest = 0,
180  Linear = 1,
181  Cubic = 2,
182  Lanczos = 3,
183  Super = 4
184  };
185 
191  Image Resize(float sampling_rate = 0.5f,
192 
193  InterpType interp_type = InterpType::Nearest) const;
194 
202  Image Dilate(int kernel_size = 3) const;
203 
205  Image Filter(const core::Tensor &kernel) const;
206 
217  Image FilterBilateral(int kernel_size = 3,
218  float value_sigma = 20.0f,
219  float distance_sigma = 10.0f) const;
220 
225  Image FilterGaussian(int kernel_size = 3, float sigma = 1.0f) const;
226 
231  std::pair<Image, Image> FilterSobel(int kernel_size = 3) const;
232 
239  Image PyrDown() const;
240 
254  Image PyrDownDepth(float diff_threshold, float invalid_fill = 0.f) const;
255 
268  Image ClipTransform(float scale,
269  float min_value,
270  float max_value,
271  float clip_fill = 0.0f) const;
272 
285  Image CreateVertexMap(const core::Tensor &intrinsics,
286  float invalid_fill = 0.0f);
287 
302  Image CreateNormalMap(float invalid_fill = 0.0f);
303 
312  Image ColorizeDepth(float scale, float min_value, float max_value);
313 
316  return core::Tensor::Zeros({2}, core::Int64);
317  }
318 
321  return core::Tensor(std::vector<int64_t>{GetRows(), GetCols()}, {2},
322  core::Int64);
323  }
324 
326  static Image FromLegacy(const cloudViewer::geometry::Image &image_legacy,
327  const core::Device &Device = core::Device("CPU:0"));
328 
331 
333  std::string ToString() const;
334 
336 #ifdef WITH_IPP
337  static constexpr bool HAVE_IPP = true;
338 #else
339  static constexpr bool HAVE_IPP = false;
340 #endif
341 
342 protected:
347 };
348 
349 } // namespace geometry
350 } // namespace t
351 } // namespace cloudViewer
int offset
bool copy
Definition: VtkUtils.cpp:74
Dtype GetDtype() const
Definition: Tensor.h:1164
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:406
Device GetDevice() const override
Definition: Tensor.cpp:1435
SizeVector GetShape() const
Definition: Tensor.h:1127
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:739
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
The base geometry class.
Definition: Geometry.h:23
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
Image Dilate(int kernel_size=3) const
Return a new image after performing morphological dilation.
Definition: Image.cpp:203
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:417
InterpType
Image interpolation algorithms.
Definition: Image.h:178
@ Super
Super sampling interpolation (only downsample).
@ Lanczos
Lanczos filter interpolation.
@ Nearest
Nearest neighbors interpolation.
static constexpr bool HAVE_IPP
Do we use IPP for accelerating image processing operations?
Definition: Image.h:339
Image PyrDown() const
Return a new downsampled image with pyramid downsampling.
Definition: Image.cpp:395
std::string ToString() const
Text description.
Definition: Image.cpp:555
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:400
static Image FromLegacy(const cloudViewer::geometry::Image &image_legacy, const core::Device &Device=core::Device("CPU:0"))
Create from a legacy CloudViewer Image.
Definition: Image.cpp:505
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:156
Image CPU() const
Backward-compatible convenience method to obtain a CPU-resident copy.
Definition: Image.h:140
core::Tensor At(int64_t r, int64_t c) const
Get pixel(s) in the image.
Definition: Image.h:104
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:56
cloudViewer::geometry::Image ToLegacy() const
Convert to legacy Image type.
Definition: Image.cpp:534
Image ColorizeDepth(float scale, float min_value, float max_value)
Colorize an input depth image (with Dtype UInt16 or Float32).
Definition: Image.cpp:482
core::Tensor GetMinBound() const
Compute min 2D coordinates for the data (always {0, 0}).
Definition: Image.h:315
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 CreateVertexMap(const core::Tensor &intrinsics, float invalid_fill=0.0f)
Create a vertex map from a depth image using unprojection.
Definition: Image.cpp:449
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:31
core::Device GetDevice() const override
Get device of the image.
Definition: Image.h:96
Image FilterGaussian(int kernel_size=3, float sigma=1.0f) const
Return a new image after Gaussian filtering.
Definition: Image.cpp:309
core::Tensor GetMaxBound() const
Compute max 2D coordinates for the data ({rows, cols}).
Definition: Image.h:320
void * GetDataPtr()
Get raw buffer of the Image data.
Definition: Image.h:118
bool IsEmpty() const override
Returns true if rows * cols * channels == 0.
Definition: Image.h:71
Image & LinearTransform(double scale=1.0, double offset=0.0)
Function to linearly transform pixel intensities in place.
Definition: Image.h:167
Image RGBToGray() const
Converts a 3-channel RGB image to a new 1-channel Grayscale image.
Definition: Image.cpp:120
Image Clone() const
Returns copy of the image on the same device.
Definition: Image.h:143
Image To(const core::Device &device, bool copy=false) const
Transfer the image to a specified device.
Definition: Image.h:132
Image CreateNormalMap(float invalid_fill=0.0f)
Create a normal map from a vertex map.
Definition: Image.cpp:467
core::Dtype GetDtype() const
Get dtype of the image.
Definition: Image.h:93
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:345
core::Tensor AsTensor() const
Returns the underlying Tensor of the Image.
Definition: Image.h:124
int64_t GetChannels() const
Get the number of channels of the image.
Definition: Image.h:90
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
virtual ~Image() override
Definition: Image.h:60
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:239
int64_t GetCols() const
Get the number of columns of the image.
Definition: Image.h:87
Image Filter(const core::Tensor &kernel) const
Return a new image after filtering with the given kernel.
Definition: Image.cpp:278
int64_t GetRows() const
Get the number of rows of the image.
Definition: Image.h:84
const void * GetDataPtr() const
Get raw buffer of the Image data.
Definition: Image.h:121
const Dtype Int64
Definition: Dtype.cpp:47
const Dtype Float32
Definition: Dtype.cpp:42
constexpr nullopt_t nullopt
Definition: Optional.h:136
Generic file read and write utility for python interface.