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 <Eigen/Core>
11 #include <memory>
12 #include <vector>
13 
14 #include "ecvHObject.h"
15 
16 namespace cloudViewer {
17 
18 namespace camera {
19 class PinholeCameraIntrinsic;
20 }
21 
22 namespace geometry {
23 
24 class Image;
25 
27 typedef std::vector<std::shared_ptr<Image>> ImagePyramid;
28 
33 class CV_DB_LIB_API Image : public ccHObject {
34 public:
44  Equal,
46  Weighted,
47  };
48 
52  enum class FilterType {
54  Gaussian3,
56  Gaussian5,
58  Gaussian7,
60  Sobel3Dx,
62  Sobel3Dy
63  };
64 
65 public:
67  Image(const char *name = "Image") : ccHObject(name) {}
68  ~Image() override {}
69 
70  // inherited methods (ccHObject)
71  virtual bool isSerializable() const override { return true; }
72 
74  virtual CV_CLASS_ENUM getClassID() const override {
75  return CV_TYPES::IMAGE2;
76  }
77 
78  virtual ccBBox getOwnBB(bool withGLFeatures = false) override;
79 
80 protected:
81  // Inherited from ccHObject
82  virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override {};
83  virtual void onDeletionOf(const ccHObject *obj) override;
84 
85 public:
87  inline virtual bool IsEmpty() const override { return !HasData(); }
88  virtual Eigen::Vector2d GetMin2DBound() const override;
89  virtual Eigen::Vector2d GetMax2DBound() const override;
90 
99  bool TestImageBoundary(double u, double v, double inner_margin = 0.0) const;
100 
101 public:
103  virtual bool HasData() const {
104  return width_ > 0 && height_ > 0 &&
105  data_.size() == size_t(height_ * BytesPerLine());
106  }
107 
110  int height,
111  int num_of_channels,
112  int bytes_per_channel) {
113  width_ = width;
114  height_ = height;
115  num_of_channels_ = num_of_channels;
116  bytes_per_channel_ = bytes_per_channel;
117  AllocateDataBuffer();
118  return *this;
119  }
120 
122  int BytesPerLine() const {
123  return width_ * num_of_channels_ * bytes_per_channel_;
124  }
125 
131  std::pair<bool, double> FloatValueAt(double u, double v) const;
132 
140  static std::shared_ptr<Image>
142  const camera::PinholeCameraIntrinsic &intrinsic);
143 
145  std::shared_ptr<Image> CreateFloatImage(
148 
150  template <typename T>
151  T *PointerAt(int u, int v) const;
152 
154  template <typename T>
155  T *PointerAt(int u, int v, int ch) const;
156 
157  std::shared_ptr<Image> ConvertDepthToFloatImage(
158  double depth_scale = 1000.0, double depth_trunc = 3.0) const;
159 
160  std::shared_ptr<Image> Transpose() const;
161 
163  std::shared_ptr<Image> FlipHorizontal() const;
165  std::shared_ptr<Image> FlipVertical() const;
166 
168  std::shared_ptr<Image> Filter(Image::FilterType type) const;
169 
171  std::shared_ptr<Image> Filter(const std::vector<double> &dx,
172  const std::vector<double> &dy) const;
173 
174  std::shared_ptr<Image> FilterHorizontal(
175  const std::vector<double> &kernel) const;
176 
178  std::shared_ptr<Image> Downsample() const;
179 
181  std::shared_ptr<Image> Dilate(int half_kernel_size = 1) const;
182 
185  Image &LinearTransform(double scale = 1.0, double offset = 0.0);
186 
191  Image &ClipIntensity(double min = 0.0, double max = 1.0);
192 
196  template <typename T>
197  std::shared_ptr<Image> CreateImageFromFloatImage() const;
198 
202 
204  ImagePyramid CreatePyramid(size_t num_of_levels,
205  bool with_gaussian_filter = true) const;
206 
208  std::shared_ptr<Image> CreateDepthBoundaryMask(
209  double depth_threshold_for_discontinuity_check = 0.1,
210  int half_dilation_kernel_size_for_discontinuity_map = 3) const;
211 
212 protected:
214  data_.resize(width_ * height_ * num_of_channels_ * bytes_per_channel_);
215  }
216 
217 public:
219  int width_ = 0;
221  int height_ = 0;
223  int num_of_channels_ = 0;
225  int bytes_per_channel_ = 0;
227  std::vector<uint8_t> data_;
228 };
229 
230 } // namespace geometry
231 } // namespace cloudViewer
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
#define CV_DB_LIB_API
Definition: CV_db.h:15
int width
std::string name
int height
int offset
char type
Bounding box structure.
Definition: ecvBBox.h:25
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
Contains the pinhole camera intrinsic parameters.
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
virtual bool IsEmpty() const override
Definition: Image.h:87
std::shared_ptr< Image > CreateFloatImage(Image::ColorToIntensityConversionType type=Image::ColorToIntensityConversionType::Weighted) const
Return a gray scaled float type image.
ColorToIntensityConversionType
Specifies whether R, G, B channels have the same weight when converting to intensity....
Definition: Image.h:42
@ Weighted
Weighted R, G, B channels: I = 0.299 * R + 0.587 * G + 0.114 * B.
Image & Prepare(int width, int height, int num_of_channels, int bytes_per_channel)
Prepare Image properties and allocate Image buffer.
Definition: Image.h:109
std::shared_ptr< Image > FlipVertical() const
Function to flip image vertically (upside down).
std::shared_ptr< Image > Filter(Image::FilterType type) const
Function to filter image with pre-defined filtering type.
std::shared_ptr< Image > Transpose() const
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
std::shared_ptr< Image > Filter(const std::vector< double > &dx, const std::vector< double > &dy) const
Function to filter image with arbitrary dx, dy separable filters.
Image(const char *name="Image")
Default Constructor.
Definition: Image.h:67
int BytesPerLine() const
Returns data size per line (row, or the width) in bytes.
Definition: Image.h:122
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: Image.h:71
std::shared_ptr< Image > FilterHorizontal(const std::vector< double > &kernel) const
virtual Eigen::Vector2d GetMax2DBound() const override
virtual bool HasData() const
Returns true if the Image has valid data.
Definition: Image.h:103
virtual void drawMeOnly(CC_DRAW_CONTEXT &context) override
Draws the entity only (not its children)
Definition: Image.h:82
bool TestImageBoundary(double u, double v, double inner_margin=0.0) const
Test if coordinate (u, v) is located in the inner_marge of the image.
std::vector< uint8_t > data_
Image storage buffer.
Definition: Image.h:227
static std::shared_ptr< Image > CreateDepthToCameraDistanceMultiplierFloatImage(const camera::PinholeCameraIntrinsic &intrinsic)
std::shared_ptr< Image > FlipHorizontal() const
Function to flip image horizontally (from left to right).
std::shared_ptr< Image > Dilate(int half_kernel_size=1) const
Function to dilate 8bit mask map.
std::pair< bool, double > FloatValueAt(double u, double v) const
std::shared_ptr< Image > ConvertDepthToFloatImage(double depth_scale=1000.0, double depth_trunc=3.0) const
std::shared_ptr< Image > CreateImageFromFloatImage() const
ImagePyramid CreatePyramid(size_t num_of_levels, bool with_gaussian_filter=true) const
Function to create image pyramid.
Image & ClipIntensity(double min=0.0, double max=1.0)
FilterType
Specifies the Image filter type.
Definition: Image.h:52
std::shared_ptr< Image > Downsample() const
Function to 2x image downsample using simple 2x2 averaging.
std::shared_ptr< Image > CreateDepthBoundaryMask(double depth_threshold_for_discontinuity_check=0.1, int half_dilation_kernel_size_for_discontinuity_map=3) const
Function to create a depthmap boundary mask from depth image.
static ImagePyramid FilterPyramid(const ImagePyramid &input, Image::FilterType type)
Function to filter image pyramid.
virtual Eigen::Vector2d GetMin2DBound() const override
virtual CV_CLASS_ENUM getClassID() const override
Returns unique class ID.
Definition: Image.h:74
Image & LinearTransform(double scale=1.0, double offset=0.0)
T * PointerAt(int u, int v) const
Function to access the raw data of a single-channel Image.
T * PointerAt(int u, int v, int ch) const
Function to access the raw data of a multi-channel Image.
virtual void onDeletionOf(const ccHObject *obj) override
This method is called when another object is deleted.
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
ImGuiContext * context
Definition: Window.cpp:76
@ IMAGE2
Definition: CVTypes.h:156
std::vector< std::shared_ptr< Image > > ImagePyramid
Typedef and functions for ImagePyramid.
Definition: Image.h:24
Generic file read and write utility for python interface.
Display context.