|
| 1 | +// This file is part of the AliceVision project. |
| 2 | +// Copyright (c) 2024 AliceVision contributors. |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public License, |
| 4 | +// v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +#include <aliceVision/sfmData/SfMData.hpp> |
| 8 | +#include <aliceVision/sfmDataIO/sfmDataIO.hpp> |
| 9 | +#include <aliceVision/system/Logger.hpp> |
| 10 | +#include <aliceVision/cmdline/cmdline.hpp> |
| 11 | +#include <aliceVision/system/main.hpp> |
| 12 | +#include <aliceVision/image/Image.hpp> |
| 13 | +#include <aliceVision/mesh/MeshIntersection.hpp> |
| 14 | + |
| 15 | +#include <filesystem> |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +// These constants define the current software version. |
| 20 | +// They must be updated when the command line is changed. |
| 21 | +#define ALICEVISION_SOFTWARE_VERSION_MAJOR 1 |
| 22 | +#define ALICEVISION_SOFTWARE_VERSION_MINOR 0 |
| 23 | + |
| 24 | +using namespace aliceVision; |
| 25 | + |
| 26 | +namespace po = boost::program_options; |
| 27 | + |
| 28 | +int aliceVision_main(int argc, char** argv) |
| 29 | +{ |
| 30 | + // command-line parameters |
| 31 | + std::string sfmDataFilename; |
| 32 | + std::string meshFilename; |
| 33 | + std::string outputDirectory; |
| 34 | + |
| 35 | + // clang-format off |
| 36 | + po::options_description requiredParams("Required parameters"); |
| 37 | + requiredParams.add_options() |
| 38 | + ("input,i", po::value<std::string>(&sfmDataFilename)->required(), |
| 39 | + "SfMData file.") |
| 40 | + ("mesh,i", po::value<std::string>(&meshFilename)->required(), |
| 41 | + "mesh file.") |
| 42 | + ("output,o", po::value<std::string>(&outputDirectory)->required(), |
| 43 | + "Output directory for depthmaps."); |
| 44 | + // clang-format on |
| 45 | + |
| 46 | + CmdLine cmdline("AliceVision depthmapRendering"); |
| 47 | + cmdline.add(requiredParams); |
| 48 | + if (!cmdline.execute(argc, argv)) |
| 49 | + { |
| 50 | + return EXIT_FAILURE; |
| 51 | + } |
| 52 | + |
| 53 | + // set maxThreads |
| 54 | + HardwareContext hwc = cmdline.getHardwareContext(); |
| 55 | + omp_set_num_threads(hwc.getMaxThreads()); |
| 56 | + |
| 57 | + std::filesystem::path pathOutputDirectory(outputDirectory); |
| 58 | + |
| 59 | + // Load input scene |
| 60 | + sfmData::SfMData sfmData; |
| 61 | + if (!sfmDataIO::load(sfmData, sfmDataFilename, sfmDataIO::ESfMData::ALL)) |
| 62 | + { |
| 63 | + ALICEVISION_LOG_ERROR("The input SfMData file '" << sfmDataFilename << "' cannot be read"); |
| 64 | + return EXIT_FAILURE; |
| 65 | + } |
| 66 | + |
| 67 | + //Load mesh in the mesh intersection object |
| 68 | + ALICEVISION_LOG_INFO("Loading mesh"); |
| 69 | + mesh::MeshIntersection mi; |
| 70 | + if (!mi.initialize(meshFilename)) |
| 71 | + { |
| 72 | + return EXIT_FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + for (const auto & [index, view] : sfmData.getViews()) |
| 76 | + { |
| 77 | + if (!sfmData.isPoseAndIntrinsicDefined(index)) |
| 78 | + { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + ALICEVISION_LOG_INFO("Generating depthmap for view " << index); |
| 84 | + |
| 85 | + //Retrieve metadatas for copying in the depthmap |
| 86 | + oiio::ParamValueList metadata = image::readImageMetadata(view->getImageInfo()->getImagePath()); |
| 87 | + |
| 88 | + const auto & intrinsic = sfmData.getIntrinsicSharedPtr(*view); |
| 89 | + std::shared_ptr<camera::Pinhole> pinHole = std::dynamic_pointer_cast<camera::Pinhole>(intrinsic); |
| 90 | + if (!pinHole) |
| 91 | + { |
| 92 | + ALICEVISION_LOG_INFO("This view is not a pinhole camera. Ignoring."); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const auto pose = sfmData.getPose(*view).getTransform(); |
| 97 | + |
| 98 | + Vec3 center = pose.center(); |
| 99 | + |
| 100 | + mi.setPose(pose); |
| 101 | + |
| 102 | + int w = view->getImageInfo()->getWidth(); |
| 103 | + int h = view->getImageInfo()->getHeight(); |
| 104 | + |
| 105 | + image::Image<float> image(w, h, 0.0f); |
| 106 | + image::Image<unsigned char> mask(w, h, 0); |
| 107 | + |
| 108 | + #pragma omp parallel for |
| 109 | + for (int i = 0; i < h; i++) |
| 110 | + { |
| 111 | + for (int j = 0; j < w; j++) |
| 112 | + { |
| 113 | + Vec2 pt; |
| 114 | + pt.x() = j; |
| 115 | + pt.y() = i; |
| 116 | + |
| 117 | + //Find the 3d point |
| 118 | + //Which is the intersection of the ray and the mesh |
| 119 | + Vec3 pt3d; |
| 120 | + if (!mi.peekPoint(pt3d, *intrinsic, pt)) |
| 121 | + { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + //Assume depth map contains length to camera center |
| 126 | + double length = (pt3d - center).norm(); |
| 127 | + image(i, j) = length; |
| 128 | + mask(i, j) = 255; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + //Store metadata used for 3D Viewer |
| 134 | + Eigen::Matrix<double, 3, 3, Eigen::RowMajor> iCamArr = pose.rotation().transpose() * pinHole->K().inverse(); |
| 135 | + metadata.push_back(oiio::ParamValue("AliceVision:CArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::VEC3), 1, ¢er[0])); |
| 136 | + metadata.push_back(oiio::ParamValue("AliceVision:iCamArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::MATRIX33), 1, iCamArr.data())); |
| 137 | + |
| 138 | + |
| 139 | + //Store depthmap |
| 140 | + auto path = (pathOutputDirectory / (std::to_string(index) + "_depthMap.exr")); |
| 141 | + ALICEVISION_LOG_INFO("Ouput depthmap to " << path); |
| 142 | + image::writeImage(path.string(), image, image::ImageWriteOptions(), metadata); |
| 143 | + |
| 144 | + //Store mask |
| 145 | + path = (pathOutputDirectory / (std::to_string(index) + "_mask.exr")); |
| 146 | + ALICEVISION_LOG_INFO("Ouput mask to " << path); |
| 147 | + image::writeImage(path.string(), mask, image::ImageWriteOptions(), metadata); |
| 148 | + } |
| 149 | + |
| 150 | + return EXIT_SUCCESS; |
| 151 | +} |
0 commit comments