-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (78 loc) · 3.04 KB
/
main.cpp
File metadata and controls
100 lines (78 loc) · 3.04 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <opencv2/opencv.hpp>
#include <string>
#include <fstream>
#include <iostream>
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "You need proper args\n";
return 0;
}
/*
F-measure_EdgeDetection.exe output_list.txt truth_list.txt
*/
std::ifstream input_list(argv[1]);
std::ifstream truth_list(argv[2]);
int count = 0;
int true_positive(0), true_negative(0), false_positive(0), false_negative(0);
double f_sum = 0.0;
while (!input_list.eof()) { // Assumption : len(input_list) == len(output_list)
int true_positive_in(0), true_negative_in(0), false_positive_in(0), false_negative_in(0);
count++;
std::cout << "Count : " << count << std::endl;
std::string input_path, truth_path;
std::getline(input_list, input_path);
std::getline(truth_list, truth_path);
cv::Mat inputImg = cv::imread(input_path, cv::IMREAD_GRAYSCALE);
cv::Mat truthImg = cv::imread(truth_path, cv::IMREAD_GRAYSCALE);
cv::Mat inputBinary, truthBinary;
cv::threshold(inputImg, inputBinary, 127, 255, cv::THRESH_BINARY);
cv::threshold(truthImg, truthBinary, 127, 255, cv::THRESH_BINARY);
if (inputBinary.size() != truthBinary.size()) {
std::cout << "[Warning] img size is not same!\n";
continue;
}
for (int j = 0; j < truthBinary.rows; ++j) {
for (int i = 0; i < truthBinary.cols; ++i) {
unsigned char pixel = truthImg.at<unsigned char>(j, i);
bool isEqual = (truthBinary.at<unsigned char>(j, i) == inputBinary.at<unsigned char>(j, i));
if (pixel == 255) { // white
if (isEqual)
true_positive_in++;
else
false_negative_in++;
}
else { // pixel == 0, black
if (isEqual)
true_negative_in++;
else
false_positive_in++;
}
}
}
double precision_in = (double)true_positive_in / (true_positive_in + false_positive_in + 0.000001);
double recall_in = (double)true_positive_in / (true_positive_in + false_negative_in + 0.000001);
double Fmeasure_in = 2 * precision_in * recall_in / (precision_in + recall_in);
std::cout << "Precision is " << precision_in << std::endl;
std::cout << "Recall is " << recall_in << std::endl;
std::cout << "F-measure is " << Fmeasure_in << std::endl << std::endl;
false_positive += false_positive_in;
false_negative += false_negative_in;
true_positive += true_positive_in;
true_negative += true_negative_in;
if (Fmeasure_in > 0)
f_sum += Fmeasure_in;
}
double precision = (double)true_positive / (true_positive + false_positive);
double recall = (double)true_positive / (true_positive + false_negative);
double Fmeasure = 2 * precision * recall / (precision + recall);
std::cout << "****************************************" << std::endl;
std::cout << "Average F-measure is " << (double) f_sum / count << std::endl;
std::cout << "****************************************" << std::endl;
std::cout << "OVERALL\n";
std::cout << "Precision is " << precision << std::endl;
std::cout << "Recall is " << recall << std::endl;
std::cout << "F-measure is " << Fmeasure << std::endl;
input_list.close();
truth_list.close();
return 0;
}