This repository has been archived by the owner on May 6, 2023. It is now read-only.
/
ff_invfile.h
164 lines (133 loc) · 5.17 KB
/
ff_invfile.h
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
/* Copyright (c) 2008, Eidgenössische Technische Hochschule Zürich, ETHZ
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Eidgenössische Technische Hochschule Zürich, ETHZ "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL the Eidgenössische Technische Hochschule Zürich, ETHZ BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/////////////////////////////////////////////////////////
// Inverted file class/database structure
// Class allows insertation of documents (vw vector) into inverted file database as well
// of document query. Scoring is weigthed by IDF weights.
/////////////////////////////////////////////////////////
// Author: Friedrich Fraundorfer, fraundorfer@inf.ethz.ch
/*
//Sample code-snipet:
// load voctree
ff_voctree voctree;
voctree.init("voc1000000dog.bin");
// quantize SIFT vectors into visual words (vw's)
for (int i = 0; i < nr; i++) {
...
voctree.quantize(&vw, &sift); // quantizes a single SIFT vector into 1 visual word
...
}
// create inverted file
ff_invfile inv;
inv.init(nr, voctree.nrvisualwords(), 2000);
// add images to database
for (int i = 0; i < nr; i++) {
...
inv.insert(&vw, vwnr, label); // adds a document vector (consisting of vwnr visual words) to database under the denoted label
...
}
// compute idf weighting for database
inv.computeidf();
// query image
inv.querytopn(&vw, vwnr, 4, qdocnames, qscores); // returns the label of the 4 closest matches in the database
*/
#ifndef FF_INVFILE
#define FF_INVFILE
#define INV_BLOCKSIZE 10
/*#define __max(x, y) (((x) > (y))?(x):(y))*/
/*#define __min(x, y) (((x) > (y))?(y):(x))*/
inline int __max(int x, int y)
{
if (x < y)
return y;
else
return x;
}
inline int __min(int x, int y)
{
if (x < y)
return x;
else
return y;
}
class ff_invfile {
////
struct invblock {
invblock *next;
unsigned int data[INV_BLOCKSIZE];
};
public:
ff_invfile(void);
virtual ~ff_invfile(void);
// Class initialization and memory allocation
// Param: nrdocs ... (IN) maximum number of documents in database
// nrvisualwords ... (IN) range of vw's quantization of voctree
// maxvw ....... (IN) max number of vw per document
int init(int nrdocs, int nrvisualwords, int maxvw);
// Inserts a document into database. "docname" is a label for the document. On query the label gets returned.
// Param: vw ... (IN) visual words of the document
// nr ... (IN) length of visual word vector
// docname ... (IN) label for document
int insert(unsigned int *vw, int nr, int docname);
// Querying the database. Finds the closest database entry to the given document and returns its label
// Param: vw ... (IN) visual words of the query document
// nr ... (IN) length of visual word vector
int query(unsigned int *vw, int nr);
// Querying the database. Finds the n-closest database entries to the given document and returns their labels and scores
// A vector for holding the labels and scores has to be provided
// Param: vw ... (IN) visual words of the query document
// nr ... (IN) length of visual word vector
// sort ..(IN) specifies if docnames are sorted according to scores (sort == 1 ... sorted)
int querytopn(unsigned int *vw, int nr, int n, int *docnames, float *scores, int sort = 1);
// Computes IDF weights from current database.
void computeidf(void);
// Stores IDF weights to file.
void saveidf(char *fname);
// Loads IDF weights from file.
void loadidf(char *fname);
void clean(void);
inline int isvalid(void){return m_init;}
unsigned int *m_invnr;
protected:
int m_init;
invblock* m_blockspace;
invblock** m_inv;
int m_nrvisualwords;
int m_maxdocs;
int m_maxvw;
int m_blocknr;
int m_docnr;
int m_maxblocks;
float* m_idf;
float *m_score;
float *m_scoreadd;
int *m_scorepos;
int *m_docnames;
float *m_docsums;
float *m_docnormalizer;
int *m_docindex;
};
#endif //FF_INVFILE