19 using namespace filesystem;
23 const auto& ps = rs.
ps_;
25 exp_ =
static_cast<int>(ps.pixels_.size());
26 for (
int i = 0; i <
exp_; ++i) {
27 border_ = max(
border_, max(abs(ps.pixels_[i].GetDx()), abs(ps.pixels_[i].GetDy())));
28 top_ = min(
top_, ps.pixels_[i].GetDy());
38 for (
int i = 0; i <
exp_; ++i) {
39 mask_(ps.pixels_[i].GetDy() +
top_, ps.pixels_[i].GetDx() +
left_) = 1;
44 size_t linearMask = 0;
46 for (
const auto& p :
rs_.
ps_) {
79 const int h = clone.rows, w = clone.cols;
85 const cv::Mat1b read_pixels = clone(cv::Rect(cv::Point(c - msk.
left_, r - msk.
top_), cv::Point(c + 1 + msk.
right_, r + 1 + msk.
bottom_)));
88 if (freqs[
rule] == numeric_limits<unsigned long long>::max()) {
89 cout <<
"OVERFLOW freq\n";
98 image = cv::imread(FileName, cv::IMREAD_GRAYSCALE);
108 cv::threshold(image,
binary, 100, 1, cv::THRESH_BINARY);
113 bool LoadFileList(vector<pair<string, bool>>& filenames,
const string& files_path)
116 ifstream is(files_path);
122 while (getline(is, cur_filename)) {
125 RemoveCharacter(cur_filename,
'\r');
126 filenames.push_back(make_pair(cur_filename,
true));
182 is.exceptions(fstream::badbit | fstream::failbit | fstream::eofbit);
186 vector<rule> new_rules = rs.
rules;
187 std::for_each(new_rules.begin(), new_rules.end(), [&is](
rule& r) {
188 unsigned long long v;
189 is.read(reinterpret_cast<char*>(&v), 8);
192 rs.
rules = new_rules;
193 cout <<
"Frequencies of " << dataset <<
" were loaded from file.\n";
196 catch (
const ifstream::failure&) {
197 cout <<
"Frequencies of " << dataset <<
" couldn't be loaded from file.\n";
199 catch (
const runtime_error&) {
200 cout <<
"Frequencies of " << dataset <<
" couldn't be loaded from file.\n";
209 vector<pair<string, bool>> files_list;
210 if (!
LoadFileList(files_list, (dataset_path / path(
"files.txt")).
string())) {
211 cout <<
"Unable to find 'files.txt' of " << dataset_path <<
", dataset skipped.\n";
214 cout << dataset <<
":\n";
216 vector<unsigned long long> freqs(rs.
rules.size(), 0);
218 size_t files_list_size = files_list.size();
219 for (
size_t d = 0; d < files_list_size; ++d) {
220 cout <<
'\r' << d <<
'/' << files_list_size;
221 path file_name = files_list[d].first;
224 cout <<
"Unable to find '" << file_name <<
"' image in '" << dataset_path <<
"' dataset, image skipped\n";
229 cout <<
'\r' << files_list_size <<
'/' << files_list_size <<
'\n';
231 for_each(freqs.begin(), freqs.end(), [rs_it = rs.
rules.begin()](
unsigned long long f)
mutable { (*rs_it++).frequency += f; });
235 cerr <<
"Frequencies of " << dataset <<
" couldn't be stored into file.\n";
238 for_each(freqs.begin(), freqs.end(), [&os](
unsigned long long f) { os.write(reinterpret_cast<const char*>(&f), 8); });
356 assert((rs.
rules.size() % 2) == 0);
357 size_t half = rs.
rules.size() / 2;
358 for (
size_t i = half; i < rs.
rules.size(); i++) {
359 rs.
rules[i].frequency = rs.
rules[i - half].frequency;
bool AddFrequenciesToRuleset(rule_set &rs, bool force, bool is_thinning)
bool CountFrequenciesOnDataset(const string &dataset, rule_set &rs, bool force)
void CalculateConfigurationsFrequencyOnImage(const cv::Mat1b &img, const mask &msk, vector< unsigned long long > &freqs)
bool GetBinaryImage(const string &FileName, cv::Mat1b &binary)
bool LoadFileList(vector< pair< string, bool >> &filenames, const string &files_path)
std::filesystem::path global_input_path_
std::string frequencies_suffix_
std::vector< std::string > datasets_
std::filesystem::path frequencies_path_
size_t MaskToLinearMask(const cv::Mat1b &r_img) const
std::vector< rule > rules
std::unordered_map< std::string, size_t > conditions_pos
std::string binary(size_t u, size_t nbits)