Skip to content
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

tolerant bracket size estimation #1488

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/aliceVision/hdr/brackets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ bool estimateBracketsFromSfmData(std::vector<std::vector<std::shared_ptr<sfmData
return false;
}

if ((countBrackets > 0) && ((countImages % countBrackets) != 0))
{
return false;
}

const sfmData::Views & views = sfmData.getViews();

Expand Down Expand Up @@ -79,6 +75,7 @@ bool estimateBracketsFromSfmData(std::vector<std::vector<std::shared_ptr<sfmData
group.clear();
}

lastExposure = exp;
group.push_back(view);
}
}
Expand All @@ -88,6 +85,37 @@ bool estimateBracketsFromSfmData(std::vector<std::vector<std::shared_ptr<sfmData
groups.push_back(group);
}


//Check maximal size
int maxSize = 0;
for (auto & group : groups)
{
if (group.size() > maxSize)
{
maxSize = group.size();
}
}

//Only keep groups with majority group size
auto groupIt = groups.begin();
while (groupIt != groups.end())
{
if (groupIt->size() != maxSize)
{
groupIt = groups.erase(groupIt);
}
else
{
groupIt++;
}
}

//Make sure we only have a few spurious measures
if (groups.size() * maxSize < countImages - 2)
{
return false;
}

std::vector< std::vector<sfmData::ExposureSetting>> v_exposuresSetting;
for(auto & group : groups)
{
Expand Down
6 changes: 1 addition & 5 deletions src/software/pipeline/main_LdrToHdrMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,449 +68,445 @@
return hdrImagePath;
}

int aliceVision_main(int argc, char** argv)
{
std::string sfmInputDataFilename;
std::string inputResponsePath;
std::string sfmOutputDataFilepath;
int nbBrackets = 3;
bool byPass = false;
bool keepSourceImageName = false;
int channelQuantizationPower = 10;
int offsetRefBracketIndex = 1000; // By default, use the automatic selection
double meanTargetedLumaForMerging = 0.4;
double minSignificantValue = 0.05;
double maxSignificantValue = 0.995;
bool computeLightMasks = false;
image::EImageColorSpace workingColorSpace = image::EImageColorSpace::SRGB;

hdr::EFunctionType fusionWeightFunction = hdr::EFunctionType::GAUSSIAN;
float highlightCorrectionFactor = 0.0f;
float highlightTargetLux = 120000.0f;

image::EStorageDataType storageDataType = image::EStorageDataType::Float;

int rangeStart = -1;
int rangeSize = 1;

// Command line parameters
po::options_description requiredParams("Required parameters");
requiredParams.add_options()
("input,i", po::value<std::string>(&sfmInputDataFilename)->required(),
"SfMData file input.")
("response,o", po::value<std::string>(&inputResponsePath)->required(),
"Input path for the response file.")
("outSfMData,o", po::value<std::string>(&sfmOutputDataFilepath)->required(),
"SfMData file output.");

po::options_description optionalParams("Optional parameters");
optionalParams.add_options()
("nbBrackets,b", po::value<int>(&nbBrackets)->default_value(nbBrackets),
"bracket count per HDR image (0 means automatic).")
("byPass", po::value<bool>(&byPass)->default_value(byPass),
"bypass HDR creation and use medium bracket as input for next steps")
("keepSourceImageName", po::value<bool>(&keepSourceImageName)->default_value(keepSourceImageName),
"Keep the filename of the input image selected as central image for the output image filename")
("channelQuantizationPower", po::value<int>(&channelQuantizationPower)->default_value(channelQuantizationPower),
"Quantization level like 8 bits or 10 bits.")
("workingColorSpace", po::value<image::EImageColorSpace>(&workingColorSpace)->default_value(workingColorSpace),
("Working color space: " + image::EImageColorSpace_informations()).c_str())
("fusionWeight,W", po::value<hdr::EFunctionType>(&fusionWeightFunction)->default_value(fusionWeightFunction),
"Weight function used to fuse all LDR images together (gaussian, triangle, plateau).")
("offsetRefBracketIndex", po::value<int>(&offsetRefBracketIndex)->default_value(offsetRefBracketIndex),
"Zero to use the center bracket. +N to use a more exposed bracket or -N to use a less exposed backet.")
("meanTargetedLumaForMerging", po::value<double>(&meanTargetedLumaForMerging)->default_value(meanTargetedLumaForMerging),
"Mean expected luminance after merging step when input LDR images are decoded in sRGB color space. Must be in the range [0, 1].")
("minSignificantValue", po::value<double>(&minSignificantValue)->default_value(minSignificantValue),
"Minimum channel input value to be considered in advanced pixelwise merging. Used in advanced pixelwise merging.")
("maxSignificantValue", po::value<double>(&maxSignificantValue)->default_value(maxSignificantValue),
"Maximum channel input value to be considered in advanced pixelwise merging. Used in advanced pixelwise merging.")
("computeLightMasks", po::value<bool>(&computeLightMasks)->default_value(computeLightMasks),
"Compute masks of dark and high lights and missing mid lights info.")
("highlightTargetLux", po::value<float>(&highlightTargetLux)->default_value(highlightTargetLux),
"Highlights maximum luminance.")
("highlightCorrectionFactor", po::value<float>(&highlightCorrectionFactor)->default_value(highlightCorrectionFactor),
"float value between 0 and 1 to correct clamped highlights in dynamic range: use 0 for no correction, 1 for "
"full correction to maxLuminance.")
("storageDataType", po::value<image::EStorageDataType>(&storageDataType)->default_value(storageDataType),
("Storage data type: " + image::EStorageDataType_informations()).c_str())
("rangeStart", po::value<int>(&rangeStart)->default_value(rangeStart),
"Range image index start.")
("rangeSize", po::value<int>(&rangeSize)->default_value(rangeSize),
"Range size.");

CmdLine cmdline("This program merges LDR images into HDR images.\n"
"AliceVision LdrToHdrMerge");

cmdline.add(requiredParams);
cmdline.add(optionalParams);
if (!cmdline.execute(argc, argv))
{
return EXIT_FAILURE;
}

// set maxThreads
HardwareContext hwc = cmdline.getHardwareContext();
omp_set_num_threads(hwc.getMaxThreads());

// Analyze path
boost::filesystem::path path(sfmOutputDataFilepath);
std::string outputPath = path.parent_path().string();

// Read sfm data
sfmData::SfMData sfmData;
if(!sfmDataIO::Load(sfmData, sfmInputDataFilename, sfmDataIO::ESfMData::ALL))
{
ALICEVISION_LOG_ERROR("The input SfMData file '" << sfmInputDataFilename << "' cannot be read.");
return EXIT_FAILURE;
}
// Check input compatibility with brackets
const int countImages = sfmData.getViews().size();
if(countImages == 0)
{
ALICEVISION_LOG_ERROR("The input SfMData contains no image.");
return EXIT_FAILURE;
}
if(nbBrackets > 0 && (countImages % nbBrackets) != 0)
{
ALICEVISION_LOG_ERROR("The input SfMData file (" << countImages << " images) is not compatible with the number of brackets (" << nbBrackets << " brackets).");
return EXIT_FAILURE;
}
if(nbBrackets == 1 && !byPass)
{
ALICEVISION_LOG_WARNING("Enable bypass as there is only one input bracket.");
byPass = true;
}

const std::size_t channelQuantization = std::pow(2, channelQuantizationPower);

// Fusion always produces linear image. sRGB is the only non linear color space that must be changed to linear (sRGB linear).
image::EImageColorSpace mergedColorSpace = (workingColorSpace == image::EImageColorSpace::SRGB) ? image::EImageColorSpace::LINEAR : workingColorSpace;

// Make groups
std::vector<std::vector<std::shared_ptr<sfmData::View>>> groupedViews;
if (!hdr::estimateBracketsFromSfmData(groupedViews, sfmData, nbBrackets))
{
ALICEVISION_LOG_ERROR("Failure to estimate brackets.");
return EXIT_FAILURE;
}

// Check groups
std::size_t usedNbBrackets;
{
std::set<std::size_t> sizeOfGroups;
for(auto& group : groupedViews)
{
sizeOfGroups.insert(group.size());
}
if(sizeOfGroups.size() == 1)
{
usedNbBrackets = *sizeOfGroups.begin();
if(usedNbBrackets == 1)
{
ALICEVISION_LOG_INFO("No multi-bracketing.");
}
ALICEVISION_LOG_INFO("Number of brackets automatically detected: "
<< usedNbBrackets << ". It will generate " << groupedViews.size()
<< " hdr images.");
}
else
{
ALICEVISION_LOG_ERROR("Exposure groups do not have a consistent number of brackets.");
return EXIT_FAILURE;
}
}

// Group all groups sharing the same intrinsic
std::map<IndexT, std::vector<std::vector<std::shared_ptr<sfmData::View>>>> groupedViewsPerIntrinsics;
for (const auto & group : groupedViews)
{
IndexT intrinsicId = UndefinedIndexT;

for (const auto & v : group)
{
IndexT lid = v->getIntrinsicId();
if (intrinsicId == UndefinedIndexT)
{
intrinsicId = lid;
}

if (lid != intrinsicId)
{
ALICEVISION_LOG_INFO("One group shall not have multiple intrinsics");
return EXIT_FAILURE;
}
}

if (intrinsicId == UndefinedIndexT)
{
ALICEVISION_LOG_INFO("One group has no intrinsics");
return EXIT_FAILURE;
}

groupedViewsPerIntrinsics[intrinsicId].push_back(group);
}


//Estimate target views for each group
std::map<IndexT, std::vector<std::shared_ptr<sfmData::View>>> targetViewsPerIntrinsics;
std::map<IndexT, int> targetIndexPerIntrinsics;
if (!byPass)
{
for (const auto& intrinsicGroup : groupedViewsPerIntrinsics)
{
IndexT intrinsicId = intrinsicGroup.first;
std::vector<std::vector<std::shared_ptr<sfmData::View>>> groups = intrinsicGroup.second;
std::vector<std::shared_ptr<sfmData::View>> targetViews;

const int middleIndex = usedNbBrackets / 2;
const int targetIndex = middleIndex + offsetRefBracketIndex;
const bool isOffsetRefBracketIndexValid = (targetIndex >= 0) && (targetIndex < usedNbBrackets);

const fs::path lumaStatFilepath(fs::path(inputResponsePath).parent_path() / (std::string("luminanceStatistics") + "_" + std::to_string(intrinsicId) + ".txt"));

if (!fs::is_regular_file(lumaStatFilepath) && !isOffsetRefBracketIndexValid)
{
ALICEVISION_LOG_ERROR("Unable to open the file " << lumaStatFilepath.string() << " with luminance statistics. This file is needed to select the optimal exposure for the creation of HDR images.");
return EXIT_FAILURE;
}

// Adjust the targeted luminance level by removing the corresponding gamma if the working color space is not sRGB.
if (workingColorSpace != image::EImageColorSpace::SRGB)
{
meanTargetedLumaForMerging = std::pow((meanTargetedLumaForMerging + 0.055) / 1.055, 2.2);
}
targetIndexPerIntrinsics[intrinsicId] = hdr::selectTargetViews(targetViews, groups, offsetRefBracketIndex, lumaStatFilepath.string(), meanTargetedLumaForMerging);

if ((targetViews.empty() || targetViews.size() != groups.size()) && !isOffsetRefBracketIndexValid)
{
ALICEVISION_LOG_ERROR("File " << lumaStatFilepath.string() << " is not valid. This file is required to select the optimal exposure for the creation of HDR images.");
return EXIT_FAILURE;
}

targetViewsPerIntrinsics[intrinsicId] = targetViews;
}
}

// Define range to compute
if(rangeStart != -1)
{
if(rangeStart < 0 || rangeSize < 0 || rangeStart > groupedViews.size())
{
ALICEVISION_LOG_ERROR("Range is incorrect");
return EXIT_FAILURE;
}

if(rangeStart + rangeSize > groupedViews.size())
{
rangeSize = groupedViews.size() - rangeStart;
}
}
else
{
rangeStart = 0;
rangeSize = groupedViews.size();
}
ALICEVISION_LOG_DEBUG("Range to compute: rangeStart=" << rangeStart << ", rangeSize=" << rangeSize);

if(rangeStart == 0)
{
int pos = 0;
sfmData::SfMData outputSfm;
outputSfm.getIntrinsics() = sfmData.getIntrinsics();

// If we are on the first chunk, or we are computing all the dataset
// Export a new sfmData with HDR images as new Views.
for (const auto & groupedViews : groupedViewsPerIntrinsics)
{
IndexT intrinsicId = groupedViews.first;

const auto & groups = groupedViews.second;
const auto & targetViews = targetViewsPerIntrinsics[intrinsicId];

for (int g = 0; g < groups.size(); g++, pos++)
{
std::shared_ptr<sfmData::View> hdrView;

const auto & group = groups[g];

if (group.size() == 1)
{
hdrView = std::make_shared<sfmData::View>(*group.at(0));
}
else if (targetViews.empty())
{
ALICEVISION_LOG_ERROR("Target view for HDR merging has not been computed");
return EXIT_FAILURE;
}
else
{
hdrView = std::make_shared<sfmData::View>(*targetViews.at(g));
}
if(!byPass)
{
boost::filesystem::path p(targetViews[g]->getImagePath());
const std::string hdrImagePath = getHdrImagePath(outputPath, pos, keepSourceImageName ? p.stem().string() : "");
hdrView->setImagePath(hdrImagePath);
}
hdrView->addMetadata("AliceVision:ColorSpace", image::EImageColorSpace_enumToString(mergedColorSpace));
outputSfm.getViews()[hdrView->getViewId()] = hdrView;
}
}

// Export output sfmData
if(!sfmDataIO::Save(outputSfm, sfmOutputDataFilepath, sfmDataIO::ESfMData::ALL))
{
ALICEVISION_LOG_ERROR("Can not save output sfm file at " << sfmOutputDataFilepath);
return EXIT_FAILURE;
}
}


if(byPass)
{
ALICEVISION_LOG_INFO("Bypass enabled, nothing to compute.");
return EXIT_SUCCESS;
}

int rangeEnd = rangeStart + rangeSize;

int pos = 0;
for (const auto & pGroupedViews : groupedViewsPerIntrinsics)
{
IndexT intrinsicId = pGroupedViews.first;

const auto & groupedViews = pGroupedViews.second;
const auto & targetViews = targetViewsPerIntrinsics.at(intrinsicId);

hdr::rgbCurve fusionWeight(channelQuantization);
fusionWeight.setFunction(fusionWeightFunction);
hdr::rgbCurve response(channelQuantization);

const std::string baseName = (fs::path(inputResponsePath).parent_path() / std::string("response_")).string();
const std::string intrinsicName = baseName + std::to_string(intrinsicId);
const std::string intrinsicInputResponsePath = intrinsicName + ".csv";

ALICEVISION_LOG_DEBUG("inputResponsePath: " << intrinsicInputResponsePath);
response.read(intrinsicInputResponsePath);

for (std::size_t g = 0; g < groupedViews.size(); ++g, ++pos)
{
if (pos < rangeStart || pos >= rangeEnd)
{
continue;
}

const std::vector<std::shared_ptr<sfmData::View>> & group = groupedViews[g];

std::vector<image::Image<image::RGBfColor>> images(group.size());
std::shared_ptr<sfmData::View> targetView = targetViews[g];
std::vector<sfmData::ExposureSetting> exposuresSetting(group.size());

// Load all images of the group
for(std::size_t i = 0; i < group.size(); ++i)
{
const std::string filepath = group[i]->getImagePath();
ALICEVISION_LOG_INFO("Load " << filepath);

image::ImageReadOptions options;
options.workingColorSpace = workingColorSpace;
options.rawColorInterpretation = image::ERawColorInterpretation_stringToEnum(group[i]->getRawColorInterpretation());
options.colorProfileFileName = group[i]->getColorProfileFileName();

// Whatever the raw color interpretation mode, the default read processing for raw images is to apply white balancing in libRaw, before demosaicing.
// The DcpMetadata mode allows to not apply color management after demosaicing.
// Because if requested after demosaicing, white balancing is done at color management stage, we can set this option to true to get real raw data,
// without any white balancing, when the DcpMetadata mode is selected.
if (options.rawColorInterpretation == image::ERawColorInterpretation::DcpMetadata)
{
options.doWBAfterDemosaicing = true;
}

image::readImage(filepath, images[i], options);

exposuresSetting[i] = group[i]->getCameraExposureSetting();
}

if(!sfmData::hasComparableExposures(exposuresSetting))
{
ALICEVISION_THROW_ERROR("Camera exposure settings are inconsistent.");
}

std::vector<double> exposures = getExposures(exposuresSetting);

// Merge HDR images
image::Image<image::RGBfColor> HDRimage;
image::Image<image::RGBfColor> lowLightMask;
image::Image<image::RGBfColor> highLightMask;
image::Image<image::RGBfColor> noMidLightMask;
if(images.size() > 1)
{
hdr::hdrMerge merge;
sfmData::ExposureSetting targetCameraSetting = targetView->getCameraExposureSetting();
hdr::MergingParams mergingParams;
mergingParams.targetCameraExposure = targetCameraSetting.getExposure();
mergingParams.refImageIndex = targetIndexPerIntrinsics[intrinsicId];
mergingParams.minSignificantValue = minSignificantValue;
mergingParams.maxSignificantValue = maxSignificantValue;
mergingParams.computeLightMasks = computeLightMasks;

merge.process(images, exposures, fusionWeight, response, HDRimage, lowLightMask, highLightMask, noMidLightMask, mergingParams);
if(highlightCorrectionFactor > 0.0f)
{
merge.postProcessHighlight(images, exposures, fusionWeight, response, HDRimage, targetCameraSetting.getExposure(), highlightCorrectionFactor, highlightTargetLux);
}
}
else if(images.size() == 1)
{
// Nothing to do
HDRimage = images[0];
}

boost::filesystem::path p(targetView->getImagePath());
const std::string hdrImagePath = getHdrImagePath(outputPath, pos, keepSourceImageName ? p.stem().string() : "");

// Write an image with parameters from the target view
std::map<std::string, std::string> viewMetadata = targetView->getMetadata();

oiio::ParamValueList targetMetadata;
for (const auto& meta : viewMetadata)
{
if (meta.first.compare(0, 3, "raw") == 0)
{
targetMetadata.add_or_replace(oiio::ParamValue("AliceVision:" + meta.first, meta.second));
}
else
{
targetMetadata.add_or_replace(oiio::ParamValue(meta.first, meta.second));
}
}

targetMetadata.add_or_replace(oiio::ParamValue("AliceVision:ColorSpace", image::EImageColorSpace_enumToString(mergedColorSpace)));

image::ImageWriteOptions writeOptions;
writeOptions.fromColorSpace(mergedColorSpace);
writeOptions.toColorSpace(mergedColorSpace);
writeOptions.storageDataType(storageDataType);

image::writeImage(hdrImagePath, HDRimage, writeOptions, targetMetadata);

if(computeLightMasks)
{
const std::string hdrMaskLowLightPath =
getHdrMaskPath(outputPath, pos, "lowLight", keepSourceImageName ? p.stem().string() : "");
const std::string hdrMaskHighLightPath =
getHdrMaskPath(outputPath, pos, "highLight", keepSourceImageName ? p.stem().string() : "");
const std::string hdrMaskNoMidLightPath =
getHdrMaskPath(outputPath, pos, "noMidLight", keepSourceImageName ? p.stem().string() : "");

image::ImageWriteOptions maskWriteOptions;
maskWriteOptions.exrCompressionMethod(image::EImageExrCompression::None);

image::writeImage(hdrMaskLowLightPath, lowLightMask, maskWriteOptions);
image::writeImage(hdrMaskHighLightPath, highLightMask, maskWriteOptions);
image::writeImage(hdrMaskNoMidLightPath, noMidLightMask, maskWriteOptions);
}
}
}

return EXIT_SUCCESS;

Check notice on line 512 in src/software/pipeline/main_LdrToHdrMerge.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/software/pipeline/main_LdrToHdrMerge.cpp#L71-L512

Complex Method
Expand Down
1 change: 1 addition & 0 deletions src/software/pipeline/main_LdrToHdrSampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int aliceVision_main(int argc, char** argv)
std::vector<std::vector<std::shared_ptr<sfmData::View>>> groupedViews;
if (!hdr::estimateBracketsFromSfmData(groupedViews, sfmData, nbBrackets))
{
ALICEVISION_LOG_ERROR("Failure to estimate brackets.");
return EXIT_FAILURE;
}

Expand Down
Loading