-
-
Notifications
You must be signed in to change notification settings - Fork 826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Depth/normal maps rendering from Mesh #1752
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// This file is part of the AliceVision project. | ||
// Copyright (c) 2024 AliceVision contributors. | ||
// This Source Code Form is subject to the terms of the Mozilla Public License, | ||
// v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include <aliceVision/sfmData/SfMData.hpp> | ||
#include <aliceVision/sfmDataIO/sfmDataIO.hpp> | ||
#include <aliceVision/system/Logger.hpp> | ||
#include <aliceVision/cmdline/cmdline.hpp> | ||
#include <aliceVision/system/main.hpp> | ||
#include <aliceVision/image/Image.hpp> | ||
#include <aliceVision/mesh/MeshIntersection.hpp> | ||
|
||
#include <filesystem> | ||
|
||
|
||
|
||
// These constants define the current software version. | ||
// They must be updated when the command line is changed. | ||
#define ALICEVISION_SOFTWARE_VERSION_MAJOR 1 | ||
#define ALICEVISION_SOFTWARE_VERSION_MINOR 0 | ||
|
||
using namespace aliceVision; | ||
|
||
namespace po = boost::program_options; | ||
|
||
int aliceVision_main(int argc, char** argv) | ||
{ | ||
// command-line parameters | ||
std::string sfmDataFilename; | ||
std::string meshFilename; | ||
std::string outputDirectory; | ||
|
||
// clang-format off | ||
po::options_description requiredParams("Required parameters"); | ||
requiredParams.add_options() | ||
("input,i", po::value<std::string>(&sfmDataFilename)->required(), | ||
"SfMData file.") | ||
("mesh,i", po::value<std::string>(&meshFilename)->required(), | ||
"mesh file.") | ||
("output,o", po::value<std::string>(&outputDirectory)->required(), | ||
"Output directory for depthmaps."); | ||
// clang-format on | ||
|
||
CmdLine cmdline("AliceVision depthmapRendering"); | ||
cmdline.add(requiredParams); | ||
if (!cmdline.execute(argc, argv)) | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// set maxThreads | ||
HardwareContext hwc = cmdline.getHardwareContext(); | ||
omp_set_num_threads(hwc.getMaxThreads()); | ||
|
||
std::filesystem::path pathOutputDirectory(outputDirectory); | ||
|
||
// Load input scene | ||
sfmData::SfMData sfmData; | ||
if (!sfmDataIO::load(sfmData, sfmDataFilename, sfmDataIO::ESfMData::ALL)) | ||
{ | ||
ALICEVISION_LOG_ERROR("The input SfMData file '" << sfmDataFilename << "' cannot be read"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
//Load mesh in the mesh intersection object | ||
ALICEVISION_LOG_INFO("Loading mesh"); | ||
mesh::MeshIntersection mi; | ||
if (!mi.initialize(meshFilename)) | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
for (const auto & [index, view] : sfmData.getViews()) | ||
{ | ||
if (!sfmData.isPoseAndIntrinsicDefined(index)) | ||
{ | ||
continue; | ||
} | ||
|
||
|
||
ALICEVISION_LOG_INFO("Generating depthmap for view " << index); | ||
|
||
//Retrieve metadatas for copying in the depthmap | ||
oiio::ParamValueList metadata = image::readImageMetadata(view->getImageInfo()->getImagePath()); | ||
|
||
const auto & intrinsic = sfmData.getIntrinsicSharedPtr(*view); | ||
std::shared_ptr<camera::Pinhole> pinHole = std::dynamic_pointer_cast<camera::Pinhole>(intrinsic); | ||
if (!pinHole) | ||
{ | ||
ALICEVISION_LOG_INFO("This view is not a pinhole camera. Ignoring."); | ||
continue; | ||
} | ||
|
||
const auto pose = sfmData.getPose(*view).getTransform(); | ||
|
||
Vec3 center = pose.center(); | ||
|
||
mi.setPose(pose); | ||
|
||
int w = view->getImageInfo()->getWidth(); | ||
int h = view->getImageInfo()->getHeight(); | ||
|
||
image::Image<float> image(w, h, 0.0f); | ||
image::Image<unsigned char> mask(w, h, 0); | ||
|
||
#pragma omp parallel for | ||
for (int i = 0; i < h; i++) | ||
{ | ||
for (int j = 0; j < w; j++) | ||
{ | ||
Vec2 pt; | ||
pt.x() = j; | ||
pt.y() = i; | ||
|
||
//Find the 3d point | ||
//Which is the intersection of the ray and the mesh | ||
Vec3 pt3d; | ||
if (!mi.peekPoint(pt3d, *intrinsic, pt)) | ||
{ | ||
continue; | ||
} | ||
|
||
//Assume depth map contains length to camera center | ||
double length = (pt3d - center).norm(); | ||
image(i, j) = length; | ||
mask(i, j) = 255; | ||
} | ||
} | ||
|
||
|
||
//Store metadata used for 3D Viewer | ||
Eigen::Matrix<double, 3, 3, Eigen::RowMajor> iCamArr = pose.rotation().transpose() * pinHole->K().inverse(); | ||
metadata.push_back(oiio::ParamValue("AliceVision:CArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::VEC3), 1, ¢er[0])); | ||
metadata.push_back(oiio::ParamValue("AliceVision:iCamArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::MATRIX33), 1, iCamArr.data())); | ||
|
||
|
||
//Store depthmap | ||
auto path = (pathOutputDirectory / (std::to_string(index) + "_depthMap.exr")); | ||
ALICEVISION_LOG_INFO("Ouput depthmap to " << path); | ||
image::writeImage(path.string(), image, image::ImageWriteOptions(), metadata); | ||
|
||
//Store mask | ||
path = (pathOutputDirectory / (std::to_string(index) + "_mask.exr")); | ||
ALICEVISION_LOG_INFO("Ouput mask to " << path); | ||
image::writeImage(path.string(), mask, image::ImageWriteOptions(), metadata); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// This file is part of the AliceVision project. | ||
// Copyright (c) 2024 AliceVision contributors. | ||
// This Source Code Form is subject to the terms of the Mozilla Public License, | ||
// v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include <aliceVision/sfmData/SfMData.hpp> | ||
#include <aliceVision/sfmDataIO/sfmDataIO.hpp> | ||
#include <aliceVision/system/Logger.hpp> | ||
#include <aliceVision/cmdline/cmdline.hpp> | ||
#include <aliceVision/system/main.hpp> | ||
#include <aliceVision/image/Image.hpp> | ||
#include <aliceVision/mesh/MeshIntersection.hpp> | ||
|
||
#include <filesystem> | ||
|
||
|
||
|
||
// These constants define the current software version. | ||
// They must be updated when the command line is changed. | ||
#define ALICEVISION_SOFTWARE_VERSION_MAJOR 1 | ||
#define ALICEVISION_SOFTWARE_VERSION_MINOR 0 | ||
|
||
using namespace aliceVision; | ||
|
||
namespace po = boost::program_options; | ||
|
||
int aliceVision_main(int argc, char** argv) | ||
{ | ||
// command-line parameters | ||
std::string sfmDataFilename; | ||
std::string meshFilename; | ||
std::string outputDirectory; | ||
|
||
// clang-format off | ||
po::options_description requiredParams("Required parameters"); | ||
requiredParams.add_options() | ||
("input,i", po::value<std::string>(&sfmDataFilename)->required(), | ||
"SfMData file.") | ||
("mesh,i", po::value<std::string>(&meshFilename)->required(), | ||
"mesh file.") | ||
("output,o", po::value<std::string>(&outputDirectory)->required(), | ||
"Output directory for depthmaps."); | ||
// clang-format on | ||
|
||
CmdLine cmdline("AliceVision normalmapRendering"); | ||
cmdline.add(requiredParams); | ||
if (!cmdline.execute(argc, argv)) | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// set maxThreads | ||
HardwareContext hwc = cmdline.getHardwareContext(); | ||
omp_set_num_threads(hwc.getMaxThreads()); | ||
|
||
std::filesystem::path pathOutputDirectory(outputDirectory); | ||
|
||
// Load input scene | ||
sfmData::SfMData sfmData; | ||
if (!sfmDataIO::load(sfmData, sfmDataFilename, sfmDataIO::ESfMData::ALL)) | ||
{ | ||
ALICEVISION_LOG_ERROR("The input SfMData file '" << sfmDataFilename << "' cannot be read"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
//Load mesh in the mesh intersection object | ||
ALICEVISION_LOG_INFO("Loading mesh"); | ||
mesh::MeshIntersection mi; | ||
if (!mi.initialize(meshFilename)) | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
for (const auto & [index, view] : sfmData.getViews()) | ||
{ | ||
if (!sfmData.isPoseAndIntrinsicDefined(index)) | ||
{ | ||
continue; | ||
} | ||
|
||
|
||
ALICEVISION_LOG_INFO("Generating depthmap for view " << index); | ||
|
||
//Retrieve metadatas for copying in the depthmap | ||
oiio::ParamValueList metadata = image::readImageMetadata(view->getImageInfo()->getImagePath()); | ||
|
||
const auto & intrinsic = sfmData.getIntrinsicSharedPtr(*view); | ||
const auto pose = sfmData.getPose(*view); | ||
|
||
Vec3 center = pose.getTransform().center(); | ||
|
||
mi.setPose(pose.getTransform()); | ||
|
||
int w = view->getImageInfo()->getWidth(); | ||
int h = view->getImageInfo()->getHeight(); | ||
image::Image<image::RGBfColor> image(w, h, image::RGBfColor(0.0f,0.0f,0.0f)); | ||
|
||
#pragma omp parallel for | ||
for (int i = 0; i < h; i++) | ||
{ | ||
for (int j = 0; j < w; j++) | ||
{ | ||
Vec2 pt; | ||
pt.x() = j; | ||
pt.y() = i; | ||
|
||
//Find the 3d point | ||
//Which is the intersection of the ray and the mesh | ||
//And get its normal | ||
Vec3 normal; | ||
if (!mi.peekNormal(normal, *intrinsic, pt)) | ||
{ | ||
continue; | ||
} | ||
|
||
Vec3 cnormal = pose.getTransform().rotation() * normal; | ||
|
||
auto & rgb = image(i, j); | ||
rgb.r() = cnormal.x(); | ||
rgb.g() = cnormal.y(); | ||
rgb.b() = cnormal.z(); | ||
} | ||
} | ||
|
||
auto path = (pathOutputDirectory / (std::to_string(index) + "_normalMap.exr")); | ||
ALICEVISION_LOG_INFO("Ouput to " << path); | ||
image::writeImage(path.string(), image, image::ImageWriteOptions(), metadata); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we convert it here or convert on the load of the OBJ as we usually do (to keep the CV coordinates everywhere)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks much more efficient given that, for lidar files, the number of vertices is way higher than the number of peeks in an image.