A working solution

This commit is contained in:
Dmitry Kokorin 2018-09-05 20:26:58 +03:00
parent 9016f8461e
commit 196957f3d0
8 changed files with 14213 additions and 10 deletions

View file

@ -4,13 +4,19 @@
#include <iostream>
#include <stdexcept>
#include <omp.h>
#include <opencv2/opencv.hpp>
#include "integral_image.h"
namespace {
const std::string OUTPUT_FILE_POSTFIX = ".integral";
struct Arguments
{
int thread_number = 0;
@ -36,7 +42,7 @@ Arguments parse_arguments(int argc, char **argv)
ss.str(argv[i]);
ss >> args.thread_number;
if (ss.bad() || args.thread_number < 0)
if (ss.fail() || args.thread_number < 0)
throw std::invalid_argument("thread number is invalid");
}
else if (IMAGE_ARGUMENT == argv[i]) {
@ -55,17 +61,37 @@ Arguments parse_arguments(int argc, char **argv)
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 a default one
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);
if (0 == args.thread_number)
args.thread_number = omp_get_max_threads();
for (const auto &file_name : args.file_names) {
@ -75,11 +101,46 @@ int main(int argc, char **argv)
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();
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 (auto &channel : channels) {
using namespace integral_image;
//The specification implicitly assumes that an output 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.
Mat float_channel;
channel.convertTo(float_channel, CV_64FC1);
float_channel = integral_image_openmp(float_channel, args.thread_number);
fs << float_channel;
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() << std::endl;
std::cerr << "Invalid argument: " << e.what() << '\n'
<< "Usage: " << argv[0] << " [-t threads] [-i image_file_name]..." << std::endl;
return -1;
}