libvisiontransfer  10.8.0
reconstruct3d-open3d.h
1 /*******************************************************************************
2  * Copyright (c) 2024 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_RECONSTRUCT3D_OPEN3D_H
16 #define VISIONTRANSFER_RECONSTRUCT3D_OPEN3D_H
17 
18 #ifdef OPEN3D_VERSION
19 
20 namespace visiontransfer {
21 
22 /*
23  * Open3d-specific implementations that need to be inlined in order to avoid
24  * dependencies for projects that do not make use of Open3D
25  */
26 
27 inline std::shared_ptr<open3d::geometry::PointCloud> Reconstruct3D::createOpen3DCloud(
28  const ImageSet& imageSet, ColorSource colSource, unsigned short minDisparity, unsigned short maxDisparity) {
29 
30  int numPoints = imageSet.getWidth() * imageSet.getHeight();
31  std::shared_ptr<open3d::geometry::PointCloud> ret(new open3d::geometry::PointCloud());
32 
33  // Convert the 3D point cloud
34  ret->points_.resize(numPoints);
35 
36  float* points = createPointMap(imageSet, minDisparity, maxDisparity);
37  float* end = &points[4*numPoints];
38  Eigen::Vector3d* dest = &ret->points_[0];
39 
40  while(points != end) {
41  float x = *(points++);
42  float y = *(points++);
43  float z = *(points++);
44  points++;
45 
46  *dest = Eigen::Vector3d(x, y, z);
47  dest++;
48  }
49 
50  ImageSet::ImageType colImg = getColorImage(imageSet, colSource);
51 
52  // Convert the color information if enabled
53  if(colSource != COLOR_NONE && imageSet.hasImageType(colImg)) {
54  ret->colors_.resize(numPoints);
55  unsigned char* pixel = imageSet.getPixelData(colImg);
56  Eigen::Vector3d* color = &ret->colors_[0];
57  Eigen::Vector3d* colorEnd = &ret->colors_[numPoints];
58 
59  switch(imageSet.getPixelFormat(colImg)) {
61  while(color != colorEnd) {
62  double col = double(*(pixel++))/0xFF;
63  *(color++) = Eigen::Vector3d(col, col, col);
64  }
65  break;
67  while(color != colorEnd) {
68  double col = double(*reinterpret_cast<unsigned short*>(pixel))/0xFFF;
69  pixel+=2;
70  *(color++) = Eigen::Vector3d(col, col, col);
71  }
72  break;
74  while(color != colorEnd) {
75  double r = double(*(pixel++))/0xFF;
76  double g = double(*(pixel++))/0xFF;
77  double b = double(*(pixel++))/0xFF;
78  *(color++) = Eigen::Vector3d(r, g, b);
79  }
80  break;
81  default: throw std::runtime_error("Illegal pixel format");
82  }
83  }
84 
85  return ret;
86 }
87 
88 inline std::shared_ptr<open3d::geometry::RGBDImage> Reconstruct3D::createOpen3DImageRGBD(const ImageSet& imageSet,
89  ColorSource colSource, unsigned short minDisparity) {
90 
91  std::shared_ptr<open3d::geometry::RGBDImage> ret(new open3d::geometry::RGBDImage);
92 
93  // Convert depth map
94  ret->depth_.width_ = imageSet.getWidth();
95  ret->depth_.height_ = imageSet.getHeight();
96  ret->depth_.num_of_channels_ = 1;
97  ret->depth_.bytes_per_channel_ = sizeof(float);
98  ret->depth_.data_.resize(ret->depth_.width_*ret->depth_.height_*ret->depth_.bytes_per_channel_);
99 
100  float* zMap = createZMap(imageSet, minDisparity);
101  memcpy(&ret->depth_.data_[0], zMap, ret->depth_.data_.size());
102 
103  // Convert color
104  ret->color_.width_ = imageSet.getWidth();
105  ret->color_.height_ = imageSet.getHeight();
106  ret->color_.num_of_channels_ = 3;
107  ret->color_.bytes_per_channel_ = 1;
108  ret->color_.data_.resize(ret->color_.width_ * ret->color_.height_ *
109  ret->color_.num_of_channels_ * ret->color_.bytes_per_channel_);
110 
111  ImageSet::ImageType colImg = getColorImage(imageSet, colSource);
112 
113  unsigned char* srcPixel = imageSet.getPixelData(colImg);
114  unsigned char* dstPixel = &ret->color_.data_[0];
115  unsigned char* dstEnd = &ret->color_.data_[ret->color_.data_.size()];
116 
117  switch(imageSet.getPixelFormat(colImg)) {
119  while(dstPixel != dstEnd) {
120  *(dstPixel++) = *srcPixel;
121  *(dstPixel++) = *srcPixel;
122  *(dstPixel++) = *(srcPixel++);
123  }
124  break;
126  while(dstPixel != dstEnd) {
127  unsigned short pixel16Bit = *reinterpret_cast<unsigned short*>(srcPixel);
128  unsigned char pixel8Bit = pixel16Bit / 0xF;
129  srcPixel += 2;
130 
131  *(dstPixel++) = pixel8Bit;
132  *(dstPixel++) = pixel8Bit;
133  *(dstPixel++) = pixel8Bit;
134  }
135  break;
137  memcpy(&ret->color_.data_[0], srcPixel, ret->color_.data_.size());
138  break;
139  default: throw std::runtime_error("Illegal pixel format");
140  }
141 
142  return ret;
143 }
144 
145 } // namespace
146 
147 #endif
148 #endif
visiontransfer::ImageSet::FORMAT_8_BIT_RGB
@ FORMAT_8_BIT_RGB
8-bit RGB format
Definition: imageset.h:79
visiontransfer::Reconstruct3D::createZMap
float * createZMap(const ImageSet &imageSet, unsigned short minDisparity=0, unsigned short maxDisparity=0xFFF)
Converts the disparity in an image set to a depth map.
Definition: reconstruct3d.cpp:108
visiontransfer::ImageSet::ImageType
ImageType
Supported image types.
Definition: imageset.h:97
visiontransfer::Reconstruct3D::createOpen3DImageRGBD
std::shared_ptr< open3d::geometry::RGBDImage > createOpen3DImageRGBD(const ImageSet &imageSet, ColorSource colSource=COLOR_AUTO, unsigned short minDisparity=0)
Converts the given disparity map to a Open3D RGBDn image.
Definition: reconstruct3d-open3d.h:100
visiontransfer::ImageSet::FORMAT_12_BIT_MONO
@ FORMAT_12_BIT_MONO
Definition: imageset.h:83
visiontransfer::ImageSet::FORMAT_8_BIT_MONO
@ FORMAT_8_BIT_MONO
8-bit greyscale format
Definition: imageset.h:76
visiontransfer::Reconstruct3D::createPointMap
float * createPointMap(const ImageSet &imageSet, unsigned short minDisparity=0, unsigned short maxDisparity=0xFFF)
Reconstructs the 3D location of each pixel in the given disparity map.
Definition: reconstruct3d.cpp:97
visiontransfer::Reconstruct3D::createOpen3DCloud
std::shared_ptr< open3d::geometry::PointCloud > createOpen3DCloud(const ImageSet &imageSet, ColorSource colSource=COLOR_AUTO, unsigned short minDisparity=0, unsigned short maxDisparity=0xFFF)
Projects the given disparity map to a Open3D point cloud.
Definition: reconstruct3d-open3d.h:39
visiontransfer::Reconstruct3D::COLOR_NONE
@ COLOR_NONE
Omit color information.
Definition: reconstruct3d.h:79
Allied Vision