integral_image/main.cpp
2018-09-05 23:37:37 +03:00

147 lines
3.6 KiB
C++

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <opencv2/opencv.hpp>
#include "integral_image.h"
namespace {
const std::string OUTPUT_FILE_POSTFIX = ".integral";
struct Arguments
{
int thread_number = 0;
std::vector<std::string> file_names;
};
Arguments parse_arguments(int argc, char **argv)
{
const std::string THREAD_ARGUMENT = "-t";
const std::string IMAGE_ARGUMENT = "-i";
Arguments args;
std::stringstream ss;
for (int i = 1; i < argc; ++i) {
if (THREAD_ARGUMENT == argv[i]) {
if (++i == argc)
throw std::invalid_argument("thread number isn't specified");
ss.str(argv[i]);
ss >> args.thread_number;
if (ss.fail() || args.thread_number < 0)
throw std::invalid_argument("thread number is invalid");
}
else if (IMAGE_ARGUMENT == argv[i]) {
if (++i == argc)
throw std::invalid_argument("image file name isn't specified");
args.file_names.push_back(argv[i]);
}
else {
throw std::invalid_argument("unknown argument");
}
}
return args;
}
std::ostream &operator<<(std::ostream &os, const integral_image::Mat &mat)
{
//The specification doesn't require any precision or data presentation format,
//so here we use default ones
if (mat.data) {
for (size_t row = 0; row < mat.rows; ++row) {
os << mat[row][0];
for (size_t col = 0; col < mat.cols; ++col)
os << ' ' << mat[row][col];
os << '\n';
}
}
os << std::endl;
return os;
}
}
int main(int argc, char **argv)
{
try {
Arguments args = parse_arguments(argc, argv);
for (const auto &file_name : args.file_names) {
auto mat = cv::imread(file_name);
if (mat.data == NULL) {
std::cerr << "Image file " << file_name << " is absent or damaged, skipping" << std::endl;
continue;
}
std::vector<cv::Mat> channels(mat.channels());
cv::split(mat, &channels[0]);
mat.release();
const auto output_file_name = file_name + OUTPUT_FILE_POSTFIX;
std::fstream fs(output_file_name, std::ios_base::out);
if (fs.bad()) {
std::cerr << "Can't open output file " << output_file_name << " , skipping" << std::endl;
continue;
}
for (const auto &channel : channels) {
using namespace integral_image;
//The specification implicitly assumes that an output file consists of floating-point values.
//Since the reasons of that decision are unknown, we convert data to 'double' format before
//calculations. Pros: no overflow, cons: precision loss, slower calculations.
Mat float_mat;
channel.convertTo(float_mat, CV_64FC1);
float_mat = integral_image_openmp(float_mat, args.thread_number);
fs << float_mat;
if (fs.fail()) {
std::cerr << "Failed to write data to file " << output_file_name << std::endl;
break;
}
}
}
}
catch (const std::invalid_argument &e) {
std::cerr << "Invalid argument: " << e.what() << '\n'
<< "Usage: " << argv[0] << " [-t threads] [-i image_file_name]..." << std::endl;
return -1;
}
return 0;
}