-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhard.cpp
More file actions
68 lines (58 loc) · 1.88 KB
/
hard.cpp
File metadata and controls
68 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include "otsuFuncs.h"
int main(const int argc, const char* argv[])
{
if (argc < 4)
{
std::cout << "3 arguments expected, " << std::to_string(argc - 1) << " found" << std::endl;
return -1;
}
std::string threadsCountStr = argv[1];
std::string in = argv[2];
std::string out = argv[3];
int threadsCount;
try
{
threadsCount = std::stoi(threadsCountStr);
if (threadsCount < -1)
{
std::cout << "Threads count " << threadsCountStr <<
" is invalid.\nValid values:\n\t0: Default number of threads\n >0: Given number of threads\n -1: Disable OpenMP (SingleThread)" << std::endl;
return -1;
}
}
catch (std::invalid_argument& e)
{
std::cout << "Threads count should be a number: " << threadsCountStr << std::endl;
return -1;
}
try
{
PnmImage image;
image.loadFromFile(in);
if (threadsCount > 0)
{
omp_set_num_threads(threadsCount);
}
bool isOmpEnabled = threadsCount != -1;
double start = omp_get_wtime();
std::vector<int> thresholds = calculateOtsuThresholds(image, isOmpEnabled);
image.applyThresholds(thresholds, isOmpEnabled);
double end = omp_get_wtime();
printf("Time (%i thread(s)): %g ms\n", threadsCount == 0 ? omp_get_max_threads() : threadsCount, (end - start) * 1000);
printf("%u %u %u\n", thresholds[0], thresholds[1], thresholds[2]);
image.saveToFile(out);
}
catch (const std::ios_base::failure& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::runtime_error& e)
{
std::cout << e.what() << std::endl;
}
return 0;
}