netatmo-algo/src/main_cli.cpp

69 lines
2 KiB
C++
Raw Normal View History

2022-02-11 20:06:26 +01:00
#include <filesystem>
#include <iostream>
2022-02-12 00:28:54 +01:00
#include <vector>
2022-02-17 23:15:45 +01:00
#include "a2.h"
2022-02-12 00:28:54 +01:00
#include "bounding_box.h"
2022-02-17 23:15:45 +01:00
#include "mapping.h"
#include "mask.h"
2022-02-12 00:28:54 +01:00
#include "pack.h"
#include "png.h"
2022-02-11 20:06:26 +01:00
namespace fs = std::filesystem;
int main(int argc, const char* argv[]) {
// We take an image path and a list of points (grouped by 4 to define
// bounding boxes).
if (argc < 6 or (argc - 2) % 4 != 0) {
std::cerr
<< "Usage: " << argv[0] << " path/to/image"
2022-02-15 21:18:50 +01:00
<< " [x y width height ...]\n"
<< "x/y/w/h points must be grouped by 4 to define bounding boxes\n";
2022-02-11 20:06:26 +01:00
return 1;
}
const fs::path input(argv[1]);
if (!fs::exists(input)) {
std::cerr << "Input file " << input << " does not exist.\n";
return 1;
}
2022-02-17 23:15:45 +01:00
const std::optional<freling::Frame> in_frame = freling::load_png(input);
2022-02-12 00:28:54 +01:00
if (!in_frame) {
std::cerr << "Cannot load file " << input << "\n";
return 1;
}
int i = 2;
2022-02-15 13:17:14 +01:00
std::vector<freling::BoundingBox> bboxes;
2022-02-12 00:28:54 +01:00
bboxes.reserve((argc - 2) / 4);
while (i < argc) {
2022-02-15 21:18:50 +01:00
const int32_t x = atoi(argv[i]);
const int32_t y = atoi(argv[i + 1]);
const uint32_t w = atoi(argv[i + 2]);
const uint32_t h = atoi(argv[i + 3]);
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
2022-02-12 00:28:54 +01:00
i += 4;
}
2022-02-17 23:15:45 +01:00
freling::Mask in_mask(in_frame->width, in_frame->height);
2022-02-12 00:28:54 +01:00
2022-02-15 21:18:50 +01:00
std::vector<freling::BoundingBox> packed_bboxes;
2022-02-17 23:15:45 +01:00
std::optional<freling::Frame> regions =
pack(*in_frame, bboxes, packed_bboxes);
in_mask.fill(false);
for (const auto& b : bboxes) {
for (int j = b.y; j < b.y + b.height; ++j) {
for (int i = b.x; i < b.x + b.width; ++i) {
in_mask.set(i, j, true);
}
}
};
std::vector<freling::BoundingBox> a2_bboxes = freling::A2(packed_bboxes);
std::vector<freling::BoundingBox> a2_bboxes_origin =
freling::map_to_origin(bboxes, packed_bboxes, a2_bboxes);
2022-02-12 00:28:54 +01:00
2022-02-11 20:06:26 +01:00
return 0;
}