-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.cpp
372 lines (320 loc) · 10.2 KB
/
retrieve.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "def_general.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#ifdef WIN32
#ifdef _DEBUG
#pragma comment( lib, "opencv_core231d.lib" )
#else
#pragma comment( lib, "opencv_core231.lib" )
#endif
#endif
#include "extern.h"
#include "proctime.h"
#include "affine.h"
#include "hcompress.h"
#include "disc.h"
#include "hash.h"
#include "fpath.h"
extern strHash cut;
int corres_num=0;
int corres[3][1000 * kMaxPointNum];
std::vector<unsigned int> vote;
int fnum=0;
int mnum=0;
int dnum=0;
using namespace std;
int VoteDirectlyByHL( strHash *hl, int *score, int p, unsigned char quotient )
// ハッシュリストから直にvoteする
// 今回voteした中で最大のものを返す
// 特徴量の比較を,商の比較に変更
{
int i;
static int max_vote = -1;
int hdoc, max_doc=0;
short hpoint;
unsigned char hquotient;
if( hl != NULL ) {
for( i = 0; i < *hl; i++ ) {
hdoc = *( ( int * )( hl + 1 + i * 7 ) );
hpoint = *( ( short * )( hl + 1 + i * 7 + 4 ) );
hquotient = *( ( unsigned char * )( hl + 1 + i * 7 + 6 ) );
if ( //flag1[hdoc] == 0 && /* 対応点フラグチェック */
//flag2[hdoc][hpoint] == 0 &&
( memcmp(&hquotient, "ient, 1) == 0 ) /* 商の比較 */ ) {
score[hdoc]++; // 投票
if ( score[hdoc] > max_vote ) {
max_vote = score[hdoc]; // 最大なら更新
max_doc = hdoc;
}
//flag1[hdoc] = 1; // hp->docは投票済み
//flag2[hdoc][hpoint] = 1; // hp-docのhp->pointは投票済み
//flag1_clear[flag1_num++] = hdoc;
//flag2_dclear[flag2_num] = hdoc;
//flag2_pclear[flag2_num] = hpoint;
//corres[0][flag2_num] = hdoc;
//corres[1][flag2_num] = p;
//corres[2][flag2_num++] = hpoint;
//if ( max_vote > 10 && hdoc == max_doc )
// break;
}
}
}
return max_vote;
}
int VoteDirectlyByHL2( strHash *hl, int *score, int p, unsigned char quotient )
// ハッシュリストから直にvoteする
// 今回voteした中で最大のものを返す
// 特徴量の比較を,商の比較に変更
{
int i;
static int max_vote = -1;
unsigned int pdoc, max_doc=0;
unsigned short ppoint;
unsigned char pquotient;
unsigned char mask = 255 >> (8-eQuotBit);
if( hl != NULL && hl != &cut ) {
for( i = 0; i < *hl; i++ ) {
ReadHComp2Dat( (hl+1+eHComp2DatByte*i), &pdoc, &ppoint, &pquotient );
//quotient = quotient & 0x0007;
quotient = quotient & mask;
//printf("%d : %d : %d : %d\n", pdoc, ppoint, pquotient, quotient);
//pdoc -= 5000;
if ( //flag1[pdoc] == 0 &&
//flag2[pdoc][ppoint] == 0 &&
//pdoc != 0 &&
( memcmp(&pquotient, "ient, 1) == 0 )) {
score[pdoc]++; // 投票
vote.push_back(pdoc);
if ( score[pdoc] > max_vote ) {
max_vote = score[pdoc]; // 最大なら更新
max_doc = pdoc;
}
//flag1[pdoc] = 1; // hp->docは投票済み
//flag2[pdoc][ppoint] = 1; // hp-docのhp->pointは投票済み
//flag1_clear[flag1_num++] = pdoc;
//flag2_dclear[flag2_num] = pdoc;
//flag2_pclear[flag2_num] = ppoint;
corres[0][corres_num] = pdoc;
corres[1][corres_num] = p;
corres[2][corres_num++] = ppoint;
//if ( max_vote > 10 && pdoc == max_doc )
// break;
}
}
}
return max_vote;
}
void CalcScoreRotateOnce( CvPoint *ps, double *areas, int num, int *score, strDisc *disc, strHash **hash )
// アフィン不変量を主成分得点に変換してハッシュに登録する
// 回転不変版
{
char **hindex = NULL, *hindex_area = NULL, *inv_array = NULL, *vec_flag=NULL, *sum=NULL;
int i, j, k, l, m, st, row, max_vote = 0;
int *idx = NULL, *idxcom1 = NULL, *idxcom2 = NULL, *idxcom1bak = NULL;
unsigned long long int index;
unsigned char quotient;
double cr;
// 配列の確保
idx = (int *)calloc( eGroup1Num, sizeof(int) );
idxcom1 = (int *)calloc( eGroup2Num, sizeof(int) );
idxcom1bak = (int *)calloc( eGroup2Num-1, sizeof(int) );
idxcom2 = (int *)calloc( eGroup3Num, sizeof(int) );
inv_array = (char *)calloc( eGroup2Num-1, sizeof(char) );
hindex_area = (char *)calloc( eGroup2Num, sizeof(char) );
hindex = (char **)calloc( eMultiVecNum, sizeof(char *) );
for ( i = 0; i < eMultiVecNum; i++ )
hindex[i] = (char *)calloc( eNumCom2, sizeof(char) );
if ( eRetMultiVec ) {
vec_flag = (char *)calloc( eNumCom2, sizeof(char) );
sum = (char *)calloc( eNumCom2, sizeof(char) );
}
//memset( score, 0, sizeof( int ) * eDbDocs );
for ( i = 0; i < num; i++ ) { // for all points
//ClearFlag1();
// 近傍店を時計回りに整列
CalcOrderCWN( i, ps, idx, eGroup1Num );
for ( j = 0; j < eNumCom1-1; j++ ) { // nCm
for ( k = 0; k < eGroup2Num; k++ ) idxcom1[k] = idx[com1[j][k]]; // m点の抽出
/************** 開始点の探索 **************************************************/
// 不変量の計算
for ( st = 0; st < eGroup2Num-1; st++ ) {
cr = CalcAffineInv( ps[idxcom1[0]], ps[idxcom1[(st%(eGroup2Num-1))+1]], ps[idxcom1[((st+1)%(eGroup2Num-1))+1]], ps[idxcom1[((st+2)%(eGroup2Num-1))+1]]);
if ( eTrueHist ) inv_array[st] = Affine2Disc2( cr, disc );
else inv_array[st] = Affine2Disc( cr, disc );
}
// 不変量から開始点を見つける
st = FindStartPoint( inv_array, eGroup2Num-1 );
// stを開始点として回転させる
for ( k = 0; k < eGroup2Num-1; k++ ) idxcom1bak[k] = idxcom1[k+1];
for ( k = 0; k < eGroup2Num-1; k++ ) idxcom1[k+1] = idxcom1bak[(k+st)%(eGroup2Num-1)];
/************** 探索の終了 ****************************************************/
// アフィン不変量を標準化
for ( k = 0; k < eNumCom2; k++ ) {
for ( l = 0; l < eGroup3Num; l++ ) idxcom2[l] = idxcom1[com2[k][l]];
cr = CalcAffineInv(ps[idxcom2[0]], ps[idxcom2[1]], ps[idxcom2[2]], ps[idxcom2[3]]);
//if ( cr > 10.0 ) cr = 10.0;
if ( eRetMultiVec ) hindex[0][k] = Affine2DiscForRetrieve( cr, disc, &vec_flag[k] );
else {
if ( eTrueHist ) hindex[0][k] = Affine2Disc2( cr, disc );
else hindex[0][k] = Affine2Disc( cr, disc );
}
}
//for ( k = 0; k < 17; k++ ) printf("%2d", vec_flag[k]);
//puts("");
//for ( k = 0; k < 17; k++ ) printf("%d ", hindex[0][k]);
//puts("");
if ( eUseArea ) CalcHindexAreaRatio( idxcom1, areas, hindex_area );
if ( eRetMultiVec ) {
row = 1;
for ( k = 0; k < eMultiNum; k++ ) {
//for ( k = 0; k < eNumCom2; k++ ) {
if ( vec_flag[k] != 0 ) {
sum[k] = vec_flag[k];
for ( l = 0; l < row; l++ ) {
for ( m = 0; m < eNumCom2; m++ ) {
hindex[row + l][m] = hindex[l][m] + sum[m];
}
}
row *= 2;
sum[k] = 0;
}
}
//printf( "%d\n", row );
//for ( k = 0; k < row; k++ ) {
// for ( l = 0; l < 17; l++ ) printf("%d ", hindex[k][l]);
// puts("");
//}
fnum+=row;
mnum++;
for ( k = 0; k < row; k++ ) {
//for ( l = 0; l < 17; l++ ) printf("%d ", hindex[k][l]);
//puts("");
if ( eUseArea ) index = HashFuncArea( hindex[k], hindex_area, disc->num, "ient );
else index = HashFunc( hindex[k], disc->num, "ient );
//printf("%d\n", index);
if ( eCompressHash ) max_vote = VoteDirectlyByHL2( hash[index], score, i, quotient );
else max_vote = VoteDirectlyByHL( hash[index], score, i, quotient );
}
memset( vec_flag, 0, sizeof( char ) * eNumCom2 );
} else {
if ( eUseArea ) index = HashFuncArea( hindex[0], hindex_area, disc->num, "ient );
else index = HashFunc( hindex[0], disc->num, "ient );
//printf("%llu\n", index);
if ( eCompressHash ) max_vote = VoteDirectlyByHL2( hash[index], score, i, quotient );
else max_vote = VoteDirectlyByHL( hash[index], score, i, quotient );
}
}
}
dnum++;
//cout << dnum << ":" << mnum << ":" << fnum << endl;
for ( i = 0; i < eMultiVecNum; i++ ) free( hindex[i] );
free( hindex );
free( hindex_area );
free( idx );
free( idxcom1 );
free( idxcom2 );
free( idxcom1bak );
free( inv_array );
free( vec_flag );
free( sum );
}
int Retrieve( CvPoint *ps, double *areas, int num, int *score, strDisc *disc, int *reg_nums, int *ret_time, strHash **hash )
// ハッシュから検索する
{
int i, max_doc = 0;
int max_score = 0;
int start, end;
//// flag2の初期化
//ClearFlag2();
//// 点対応テーブルの作成
//ClearCorres();
// 検索開始
start = GetProcTimeMicroSec();
// 検索
std::sort(vote.begin(), vote.end());
vote.erase( std::unique(vote.begin(), vote.end()), vote.end());
for ( int i = 0; i < vote.size(); i++ ) {
score[vote[i]] = 0;
}
vote.clear();
//memset( score, 0, sizeof( int ) * eDbDocs );
CalcScoreRotateOnce( ps, areas, num, score, disc, hash );
// 検索終了
end = GetProcTimeMicroSec();
// 検索時間
*ret_time = end - start;
if ( !eExperimentMode ) printf( "retrieval time : %d[milisec]\n", *ret_time );
// ソート
for ( i = 0; i < eDbDocs; i++ ) {
if ( score[i] > max_score ) {
max_doc = i;
max_score = score[i];
}
}
return max_doc;
}
int RetrieveCor( CvPoint *ps, double *areas, int num, int *score, int pcor[][2], int *pcornum0, strDisc *disc, int *reg_nums, strHash **hash )
// ハッシュから検索する
{
int i, j, k, max_doc=0, flag;
int max_score = 0;
memset( corres[0], -1, sizeof(int)*corres_num );
corres_num = 0;
// 検索
CalcScoreRotateOnce( ps, areas, num, score, disc, hash );
// スコアの正規化
for ( i = 0; i < eDbDocs; i++ ) {
score[i] = (int)((double)score[i] - (double)reg_nums[i] * eProp); // CBDARでの補正
}
// ソート
for ( i = 0; i < eDbDocs; i++ ) {
if ( score[i] > max_score ) {
max_doc = i;
max_score = score[i];
}
}
for ( i = 0, j = 0; i < corres_num; i++ ) {
flag = 0;
if ( corres[0][i] == max_doc ) {
for ( k = 0; k < j; k++ ) {
if ( pcor[k][0] == corres[1][i] ) flag = 1;
}
if ( flag != 1 ) {
pcor[j][0] = corres[1][i];
pcor[j][1] = corres[2][i];
// fprintf( fp1, "%d,%d\n", pcor[j][0], pcor[j][1] );
//printf( "%d,%d\n", pcor[j][0], pcor[j][1] );
j++;
}
}
}
*pcornum0 = j;
return max_doc;
}
int IsSucceed( char *str1, char *str2 )
// 実験モード用:成功判定
{
char base1[kMaxPathLen], base2[kMaxPathLen];
GetBasename( str1, kMaxPathLen, base1 );
GetBasename( str2, kMaxPathLen, base2 );
return !strcmp( base1, base2 );
}
double Calc12Diff( int *score )
// 1位と2位の差を計算
{
int i;
int score_1st = 1, score_2nd = 1;
for ( i = 0; i < eDbDocs; i++ ) {
if ( score[i] > score_1st ) {
score_2nd = score_1st;
score_1st = score[i];
} else if ( score[i] > score_2nd ) {
score_2nd = score[i];
}
}
return ((double) score_1st) / ((double) score_2nd);
}