From c6f314de958e082e55bbba79ba4d59c226deed22 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 26 Nov 2016 12:43:05 +0100 Subject: merge --- merge.cc | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 merge.cc (limited to 'merge.cc') diff --git a/merge.cc b/merge.cc new file mode 100644 index 0000000..a41cfc7 --- /dev/null +++ b/merge.cc @@ -0,0 +1,131 @@ +#include +#include +#include "opencv2/opencv.hpp" +#include "opencv2/photo/photo.hpp" +#include "cxxopts/src/cxxopts.hpp" + +using namespace std; +using namespace cv; + +int +load_images(const vector &files, vector &images) +{ + for (const auto& f : files) { + cerr << "reading " << f << endl; + images.push_back(imread(f, 1)); + if (images.back().empty()) { + cerr << "failed to read " << f << endl; + return 1; + } + } + + return 0; +} + +void +mertens_fusion(vector &images, string output) +{ + cerr << "Mertens fusion" << endl; + Ptr merge_mertens = createMergeMertens(); + Mat m; + merge_mertens->process(images, m); + + cerr << "writing " << output << endl; + imwrite(output, m * 255); +} + +void +hdr(vector &images, string output, string algorithm, string tonemap, + vector times, float gamma) +{ + cerr << "HDR (" << algorithm << "/" << tonemap << ")" << endl; + + Mat response, hdr, ldr; + Ptr calibrate; + Ptr merge; + if (algorithm == "Debevec") { + calibrate = createCalibrateDebevec(); + merge = createMergeDebevec(); + } + else + if (algorithm == "Robertson") { + calibrate = createCalibrateRobertson(); + merge = createMergeRobertson(); + } + calibrate->process(images, response, times); + merge->process(images, hdr, times, response); + + Ptr map; + if (tonemap == "Durand") { + map = createTonemapDurand(gamma); + } else + if (tonemap == "Drago") { + map = createTonemapDrago(gamma); + } else + if (tonemap == "Reinhard") { + map = createTonemapReinhard(gamma); + } else + if (tonemap == "Mantiuk") { + map = createTonemapMantiuk(gamma); + } + map->process(hdr, ldr); + + cerr << "writing " << output << endl; + imwrite(output, ldr * 255); +} + + +int +main (int argc, char** argv) +{ + cxxopts::Options opt(argv[0], " - Options"); + opt.add_options() + ("f,files", "files", cxxopts::value>()) + ("F,Mertens", "Mertens fusion") + ("D,debevec", "Debevec HDR") + ("R,robertson", "Robertson HDR") + ("w,times", "times for HDR", cxxopts::value>()) + ("t,tonemap", "tonemapping", cxxopts::value()) + ("g,gamma", "gamma for HDR", cxxopts::value()) + ("o,output", "output file", cxxopts::value()) + ("O,Output", "another output file", cxxopts::value()) + ("p,OUTPUT", "another output file", cxxopts::value()); + + opt.parse(argc, argv); + + auto& files = opt["f"].as>(); + vector images; + auto do_fusion = opt.count("F"); + bool do_debevec_hdr = opt.count("D"); + bool do_robertson_hdr = opt.count("R"); + bool do_hdr = do_debevec_hdr||do_robertson_hdr; + vector times = opt["w"].as>(); + float gamma = opt["g"].as(); + cout << gamma << endl; + if (do_hdr && !opt.count("g")) + gamma = 1.0; + string tonemap; + if (opt.count("t")) + tonemap = opt["t"].as(); + else + tonemap = "Durand"; + auto& output = opt["o"].as(); + auto& output1 = opt["O"].as(); + auto& output2 = opt["p"].as(); + + if (load_images(files, images) > 0) + return 1; + + if (do_fusion) + mertens_fusion(images, output); + + if (do_hdr) { + if (do_debevec_hdr) + hdr(images, output1, "Debevec", tonemap, times, gamma); + if (do_robertson_hdr) + hdr(images, output2, "Robertson", tonemap, times, gamma); + } + + return 0; +} + -- cgit v1.2.3