diff options
author | asuonpaa <34128694+asuonpaa@users.noreply.github.com> | 2019-10-28 15:12:21 +0200 |
---|---|---|
committer | dan sinclair <dsinclair@google.com> | 2019-10-28 09:12:21 -0400 |
commit | 40fce113d6fec2ee35cf1c3574bcdb8e0129c373 (patch) | |
tree | 40dd2438da28d219ddcb41e92d41f955897f81bd | |
parent | b4e20fdcf0f43e577103d2f6f834bc9e98cc173d (diff) | |
download | amber-40fce113d6fec2ee35cf1c3574bcdb8e0129c373.tar.gz |
Specify comparison parameters after comparison algorithm (#696)
Prepare image_diff tool to allow different parameters per
comparison algorithm. Now all comparison parameters are
expected to be provided after the algorithm parameter.
This change removes the separate tolerance parameter as
future comparators might not use it.
-rw-r--r-- | samples/image_diff.cc | 69 |
1 files changed, 59 insertions, 10 deletions
diff --git a/samples/image_diff.cc b/samples/image_diff.cc index 7c8db69..ec541fc 100644 --- a/samples/image_diff.cc +++ b/samples/image_diff.cc @@ -13,6 +13,7 @@ // limitations under the License. #include <iostream> +#include <sstream> #include <vector> #include "src/buffer.h" @@ -37,39 +38,87 @@ struct Options { const char kUsage[] = R"(Usage: image_diff [options] image1.png image2.png - options: - --rmse -- Compare using RMSE algorithm (default). - --histogram_emd -- Compare using histogram EMD algorithm. - -t | --tolerance <float> -- Tolerance value for comparison. - -h | --help -- This help text. +Exactly one algorithm (and its parameters) must be specified. + +Algorithms: + + --rmse TOLERANCE + Compare using the Root Mean Square Error (RMSE) algorithm with + a floating point TOLERANCE value in the range 0..255, where 0 + indicates identical images and 255 indicates images where every + color channel has the maximum difference. + + --histogram_emd TOLERANCE + Compare the per-channel color histograms of the images using a + variant of the Earth Mover's Distance (EMD) algorithm with a + floating point TOLERANCE value in the range 0.0..1.0, where 0.0 + indicates identical histograms and 1.0 indicates completely + different histograms for at least one of the color channels. + E.g. an image with red=255 for every pixel vs. an image with + red=0 for every pixel. + +Other options: + + -h | --help This help text. )"; bool ParseArgs(const std::vector<std::string>& args, Options* opts) { + int num_algorithms = 0; for (size_t i = 1; i < args.size(); ++i) { const std::string& arg = args[i]; if (arg == "-h" || arg == "--help") { opts->show_help = true; return true; } else if (arg == "--rmse") { + num_algorithms++; opts->compare_algorithm = CompareAlgorithm::kRMSE; + ++i; + if (i >= args.size()) { + std::cerr << "Missing tolerance value for RMSE comparison." + << std::endl; + return false; + } + std::stringstream sstream(args[i]); + sstream >> opts->tolerance; + if (sstream.fail()) { + std::cerr << "Invalid tolerance value " << args[i] << std::endl; + return false; + } + if (opts->tolerance < 0 || opts->tolerance > 255) { + std::cerr << "Tolerance must be in the range 0..255." << std::endl; + return false; + } + } else if (arg == "--histogram_emd") { + num_algorithms++; opts->compare_algorithm = CompareAlgorithm::kHISTOGRAM_EMD; - } else if (arg == "-t" || arg == "--tolerance") { ++i; if (i >= args.size()) { - std::cerr << "Missing value for " << args[i - 1] << " argument." + std::cerr << "Missing tolerance value for histogram EMD comparison." << std::endl; return false; } - opts->tolerance = std::stof(std::string(args[i])); - if (opts->tolerance < 0) { - std::cerr << "Tolerance must be non-negative." << std::endl; + std::stringstream sstream(args[i]); + sstream >> opts->tolerance; + if (sstream.fail()) { + std::cerr << "Invalid tolerance value " << args[i] << std::endl; + return false; + } + if (opts->tolerance < 0 || opts->tolerance > 1) { + std::cerr << "Tolerance must be in the range 0..1." << std::endl; return false; } } else if (!arg.empty()) { opts->input_filenames.push_back(arg); } } + if (num_algorithms == 0) { + std::cerr << "No comparison algorithm specified." << std::endl; + return false; + } else if (num_algorithms > 1) { + std::cerr << "Only one comparison algorithm can be specified." << std::endl; + return false; + } return true; } |