-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMatching.cpp
More file actions
233 lines (201 loc) · 5.38 KB
/
Copy pathMatching.cpp
File metadata and controls
233 lines (201 loc) · 5.38 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "Matching.hpp"
#include <iostream>
#include <vector>
#include <limits>
using std::cerr;
using std::endl;
using std::vector;
Matching::Matching(std::string s, RegionsSingleImage *a, RegionsSingleImage *d){
matchingAlgoStr = s;
Matching(a, d);
}
Matching::Matching(RegionsSingleImage *a, RegionsSingleImage *d){
annot = a;
det = d;
pairWiseScores = NULL;
}
Matching::~Matching(){
// free memory for the score matrix
clearPairWiseScores();
}
double Matching::computeScore(Region *r1, Region *r2)
{
double score = (r1->setIntersect(r2)) / (r1->setUnion(r2));
return score;
}
void Matching::computePairWiseScores(){
int nAnnot = annot->length();
int nDet = det->length();
if(pairWiseScores == NULL)
{
// first-time access of the score matrix, so initialize it
pairWiseScores = new vector<vector<double> *>;
pairWiseScores->reserve(nAnnot);
for(int ai=0; ai<nAnnot; ai++)
pairWiseScores->push_back(NULL);
}
for(int ai=0; ai<nAnnot; ai++)
{
const IplImage *im = annot->getImage();
assert(im != NULL);
IplImage *maskA = cvCreateImage(cvGetSize((IplImage *)im), IPL_DEPTH_8U, 1);
cvSetZero(maskA);
// Create the mask image corresponding to the annotated region
Region *ar = annot->get(ai);
maskA = ar->display(maskA, cvScalar(REGION_MASK_VALUE), CV_FILLED, NULL);
ar->mask = maskA;
vector<double> *v;
if( pairWiseScores->at(ai) != NULL)
v = pairWiseScores->at(ai);
else
v = new vector<double>(nDet, -1);
for(int di=0; di<nDet; di++)
{
Region *dr = det->get(di);
if( dr->isValid() && v->at(di) == -1)
{
// Create the mask image corresponding to the detected region
IplImage *maskD = cvCreateImage(cvGetSize((IplImage *)im), IPL_DEPTH_8U, 1);
cvSetZero(maskD);
maskD = dr->display(maskD, cvScalar(REGION_MASK_VALUE), CV_FILLED, NULL);
dr->mask = maskD;
v->at(di) = computeScore(ar, dr);
cvReleaseImage(&maskD);
dr->mask = NULL;
}
}
pairWiseScores->at(ai) = v;
cvReleaseImage(&maskA);
ar->mask = NULL;
}
}
void Matching::clearPairWiseScores(){
if(pairWiseScores == NULL)
return;
for(unsigned int ai=0; ai<pairWiseScores->size(); ai++)
delete(pairWiseScores->at(ai));
delete(pairWiseScores);
}
vector<MatchPair *>* Matching::runHungarian()
{
int nAnnot = annot->length();
int nDet = det->length();
// Remove the annotations with no matching detections
int *mapI = (int *)calloc(nAnnot, sizeof(int));
int mI = 0;
for(unsigned int i=0; i < nAnnot; i++)
{
double sum = 0;
for(unsigned int j=0; j < nDet; j++)
if(det->get(j)->isValid())
sum += pairWiseScores->at(i)->at(j);
if(sum != 0)
mapI[mI++] = i;
}
// Remove the detections with no matching annotations
int *mapJ = (int *)calloc(nDet, sizeof(int));
int mJ = 0;
for(unsigned int j=0; j < nDet; j++)
{
if(det->get(j)->isValid())
{
double sum = 0;
for(unsigned int i=0; i < nAnnot; i++)
sum += pairWiseScores->at(i)->at(j);
if(sum != 0)
mapJ[mJ++] = j;
}
}
int nRow, nCol;
bool useTranspose = (mI > mJ);
if(useTranspose)
{
nCol = mI; nRow = mJ;
}
else
{
nRow = mI; nCol = mJ;
}
// Initialize the match matrix used in the hungarian algorithm
double **matchMat = (double **)calloc(nRow, sizeof(double *));
for(unsigned int i=0; i< nRow; i++){
matchMat[i] = (double *)calloc(nCol, sizeof(double));
}
if(useTranspose)
{
for(unsigned int i=0; i< nRow; i++)
for(unsigned int j=0; j< nCol; j++)
matchMat[i][j] = pairWiseScores->at(mapI[j])->at(mapJ[i]);
}
else
{
for(unsigned int i=0; i< nRow; i++)
for(unsigned int j=0; j< nCol; j++)
matchMat[i][j] = pairWiseScores->at(mapI[i])->at(mapJ[j]);
}
hungarian_t prob;
hungarian_init(&prob,matchMat,nRow,nCol,HUNGARIAN_MAX);
//hungarian_init(&prob,matchMat,nAnnot,nDet,HUNGARIAN_MAX);
//hungarian_print_rating(&prob);
hungarian_solve(&prob);
//hungarian_print_assignment(&prob);
// Create the set of match pairs from the assignment matrix from the Hungarian algo
vector<MatchPair *> *mps = NULL;
if(hungarian_check_feasibility(&prob))
{
mps = new vector<MatchPair *>;
if(useTranspose)
{
for(unsigned int i=0; i < nRow; i++)
for(unsigned int j=0; j < nCol; j++)
{
double score = pairWiseScores->at(mapI[j])->at(mapJ[i]);
if(prob.a[i] == j && score > 0)
{
Region *dr = det->get(mapJ[i]);
Region *ar = annot->get(mapI[j]);
mps->push_back( new MatchPair(ar, dr, score) );
//ar->setValid(false);
//dr->setValid(false);
}
}
}
else
{
for(unsigned int i=0; i < nRow; i++)
{
vector<double> *pwsI = pairWiseScores->at(mapI[i]);
for(unsigned int j=0; j < nCol; j++)
{
double score = pwsI->at(mapJ[j]);
if(prob.a[i] == j && score > 0)
{
Region *dr = det->get(mapJ[j]);
Region *ar = annot->get(mapI[i]);
mps->push_back( new MatchPair(ar, dr, score) );
//ar->setValid(false);
//dr->setValid(false);
}
}
}
}
}
else
{
cerr << "Not yet implemented" << endl;
assert(false);
}
// free memory used by hungarian algorithm
hungarian_fini(&prob);
free(mapI);
free(mapJ);
for(unsigned int i=0; i<nRow; i++)
free(matchMat[i]);
free(matchMat);
return mps;
}
vector<MatchPair *>* Matching::getMatchPairs()
{
computePairWiseScores();
return runHungarian();
}