| 1 |
#include "colmap/mvs/depth_map.h" |
| 2 |
|
| 3 |
#include "pycolmap/helpers.h" |
| 4 |
#include "pycolmap/pybind11_extension.h" |
| 5 |
|
| 6 |
#include <pybind11/eigen.h> |
| 7 |
#include <pybind11/pybind11.h> |
| 8 |
|
| 9 |
using namespace colmap; |
| 10 |
using namespace colmap::mvs; |
| 11 |
using namespace pybind11::literals; |
| 12 |
namespace py = pybind11; |
| 13 |
|
| 14 |
#ifdef _MSC_VER // If compiling with MSVC |
| 15 |
#include <stddef.h> |
| 16 |
typedef ptrdiff_t ssize_t; |
| 17 |
#endif |
| 18 |
|
| 19 |
py::array_t<float> ArrayFromDepthMap(const DepthMap& self) { |
| 20 |
const std::vector<ssize_t> shape = {static_cast<ssize_t>(self.GetHeight()), |
| 21 |
static_cast<ssize_t>(self.GetWidth())}; |
| 22 |
py::array_t<float> output(shape); |
| 23 |
py::buffer_info output_info = output.request(); |
| 24 |
float* output_ptr = reinterpret_cast<float*>(output_info.ptr); |
| 25 |
std::memcpy(output_ptr, self.GetPtr(), self.GetNumBytes()); |
| 26 |
return output; |
| 27 |
} |
| 28 |
|
| 29 |
DepthMap DepthMapFromArray(py::array_t<float, py::array::c_style> array, |
| 30 |
float depth_min, |
| 31 |
float depth_max) { |
| 32 |
if (array.ndim() != 2) { |
| 33 |
throw std::runtime_error("Input array must have 2 dimensions!"); |
| 34 |
} |
| 35 |
|
| 36 |
const int width = array.shape(1); |
| 37 |
const int height = array.shape(0); |
| 38 |
if (width == 0 || height == 0) { |
| 39 |
throw std::runtime_error( |
| 40 |
"Input array must have positive width and height!"); |
| 41 |
} |
| 42 |
|
| 43 |
DepthMap output(width, height, depth_min, depth_max); |
| 44 |
|
| 45 |
const float* input_ptr = static_cast<float*>(array.request().ptr); |
| 46 |
std::memcpy(output.GetPtr(), input_ptr, output.GetNumBytes()); |
| 47 |
return output; |
| 48 |
} |
| 49 |
|
| 50 |
void BindDepthMap(pybind11::module& m) { |
| 51 |
py::classh<DepthMap>(m, "DepthMap") |
| 52 |
.def(py::init<>()) |
| 53 |
.def(py::init<size_t, size_t, float, float>(), |
| 54 |
"width"_a, |
| 55 |
"height"_a, |
| 56 |
"depth_min"_a, |
| 57 |
"depth_max"_a) |
| 58 |
.def("to_array", &ArrayFromDepthMap) |
| 59 |
.def_static("from_array", |
| 60 |
&DepthMapFromArray, |
| 61 |
"array"_a, |
| 62 |
"depth_min"_a, |
| 63 |
"depth_max"_a, |
| 64 |
"Create depth map as a copy of array. Returns depth map " |
| 65 |
"with shape (H, W).") |
| 66 |
.def("rescale", &DepthMap::Rescale, "factor"_a, "Rescale depth map.") |
| 67 |
.def("downsize", |
| 68 |
&DepthMap::Downsize, |
| 69 |
"max_width"_a, |
| 70 |
"max_height"_a, |
| 71 |
"Downsize depth map to fit maximum dimensions.") |
| 72 |
.def("to_bitmap", |
| 73 |
&DepthMap::ToBitmap, |
| 74 |
"min_percentile"_a, |
| 75 |
"max_percentile"_a, |
| 76 |
"Convert depth map to bitmap for visualization.") |
| 77 |
.def_property_readonly( |
| 78 |
"width", &DepthMap::GetWidth, "Width of the depth map.") |
| 79 |
.def_property_readonly( |
| 80 |
"height", &DepthMap::GetHeight, "Height of the depth map.") |
| 81 |
.def_property_readonly( |
| 82 |
"depth_min", &DepthMap::GetDepthMin, "Minimum depth value.") |
| 83 |
.def_property_readonly( |
| 84 |
"depth_max", &DepthMap::GetDepthMax, "Maximum depth value.") |
| 85 |
.def("read", |
| 86 |
&DepthMap::Read, |
| 87 |
"path"_a, |
| 88 |
"Read depth map from file at given path.") |
| 89 |
.def("write", |
| 90 |
&DepthMap::Write, |
| 91 |
"path"_a, |
| 92 |
"Write depth map to file at given path.") |
| 93 |
.def("__repr__", &CreateRepresentation<DepthMap>); |
| 94 |
} |