-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (67 loc) · 1.83 KB
/
main.cpp
File metadata and controls
72 lines (67 loc) · 1.83 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
69
70
71
72
#include <unistd.h>
#include <opencv2/opencv.hpp>
// local includes in gsm/Doxa
#include "Doxa/Image.hpp"
#include "Doxa/Parameters.hpp"
#include "Doxa/ISauvola.hpp"
#include "Doxa/PNM.hpp"
struct args {
char *inf;
char *outf;
double k;
int w;
};
int parse(int argc, char **argv, args& args) {
int option;
while((option = getopt(argc, argv, ":k:w:")) != -1) {
switch (option) {
case 'k':
args.k = strtod(optarg, nullptr);
break;
case 'w':
args.w = atoi(optarg);
break;
default:
return 1;
}
}
if (optind >= argc) {
return 1;
}
args.inf = argv[optind++];
if (optind >= argc) {
return 1;
}
args.outf = argv[optind++];
if (optind < argc) {
return 1;
}
return 0;
}
int main(int argc, char **argv) {
// Handle command line args.
struct args args = {nullptr, nullptr, 0.2, 75};
if (parse(argc, argv, args) != 0) {
std::cerr << "Usage: " << argv[0] << " [Options] IN OUT\n";
std::cerr << "Options:\n";
std::cerr << " -k n\tset k-value (default: 0.2)\n";
std::cerr << " -w n\tset window size (default: 75)\n";
return EXIT_FAILURE;
}
// Read image and convert it into gray scale.
auto img = cv::imread(args.inf);
cv::Mat graymat;
cv::cvtColor(img, graymat, cv::COLOR_BGR2GRAY);
assert(graymat.channels() == 1);
std::vector<Doxa::Pixel8> buf(graymat.begin<Doxa::Pixel8>(), graymat.end<Doxa::Pixel8>());
// Binarize the image.
const auto w = graymat.cols;
const auto h = graymat.rows;
const auto image = Doxa::Image::Reference(w, h, buf.data());
Doxa::Parameters parameters(Doxa::ParameterMap({{"window", args.w}}));
parameters.Set("k", args.k);
Doxa::Image binaryImage = Doxa::ISauvola::ToBinaryImage(image, parameters);
cv::Mat binmat(h, w, CV_8UC1, binaryImage.data);
cv::imwrite(args.outf, binmat);
return EXIT_SUCCESS;
}