forked from kalxas/mseg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobia-utils-confmatrix.cpp
109 lines (92 loc) · 2.65 KB
/
obia-utils-confmatrix.cpp
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
101
102
103
104
105
106
107
108
109
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* confmatrix.cpp: Dip 2 Confusion Matrix *
* Version: 0.8.2 build 20 Nightly *
* Last revised: 15/06/2006 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DIP 2 by Angelos Tzotsos (Kalxas), Chris Iossifides (Chiossif) *
* MSEG algorithm by Angelos Tzotsos (Kalxas) *
* GCpp Lab. January 2006 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <string>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
float ac;
int count=0;
int success=0;
int **cm;
int classes = atoi(argv[3]);
int lines = atoi(argv[4]);
int columns = atoi(argv[5]);
if(argc != 6) //TODO: Add to Error Handling
{
cout << "Error in arguments:" << endl << endl
<< "Usage : confmatrix <input class raster> <input truth image> <class number> <image lines> <image columns>"
<< endl;
exit(1);
}
cm = (int **)malloc(classes * sizeof(int *));
for(int i=0;i<classes;i++)
{
cm[i] = (int *)malloc(classes * sizeof(int));
}
for(int i=0;i<classes;i++)
{
for(int j=0;j<classes;j++)
{
cm[i][j]=0;
}
}
ifstream clfile;
clfile.open(argv[1], ios::binary);
vector<unsigned char> clbuffer;
unsigned char tmp;
int tmp2;
clfile.seekg(0, ios::beg);
for(int i=0;i<lines*columns; i++)
{
clfile.read(reinterpret_cast<char *>(&tmp2), sizeof(int));
tmp = (unsigned char)tmp2;
clbuffer.push_back(tmp);
}
clfile.close();
ifstream trfile;
trfile.open(argv[2], ios::binary);
vector<unsigned char> trbuffer;
trfile.seekg(0, ios::beg);
for(int i=0;i<lines*columns; i++)
{
trfile.read(reinterpret_cast<char *>(&tmp), sizeof(unsigned char));
trbuffer.push_back(tmp);
}
trfile.close();
for(int i=0;i<lines*columns; i++)
{
if(trbuffer[i]!=255)
{
count++;
cm[trbuffer[i]-1][clbuffer[i]-1]++;
if(trbuffer[i]==clbuffer[i])
{
success++;
}
}
}
ac = ((float) success) / ((float)count);
for(int i=0;i<classes;i++)
{
for(int j=0;j<classes;j++)
{
cout << cm[i][j] << " ";
}
cout << endl;
}
cout << "Accuracy was computed to " << 100*ac << "%" << endl;
return EXIT_SUCCESS;
}