-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSiftMatch.cpp
More file actions
185 lines (157 loc) · 4.92 KB
/
SiftMatch.cpp
File metadata and controls
185 lines (157 loc) · 4.92 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
#include <iostream>
#include <vector>
#include <cuda_runtime.h>
#include "GlobalUtil.h"
#include "SiftMatch.h"
#include "CuTexImage.h"
#include "ProgramCU.h"
#include "CUDATimer.h"
SiftMatchGPU::SiftMatchGPU(int max_sift)
{
_num_sift[0] = _num_sift[1] = 0;
_id_sift[0] = _id_sift[1] = 0;
_have_loc[0] = _have_loc[1] = 0;
_max_sift = max_sift <= 0 ? 4096 : ((max_sift + 31) / 32 * 32);
_initialized = 0;
d_rowMatchDistances = NULL;
_timer = new CUDATimer();
}
SiftMatchGPU::~SiftMatchGPU()
{
if (d_rowMatchDistances) cutilSafeCall(cudaFree(d_rowMatchDistances));
if (_timer) delete _timer;
}
void SiftMatchGPU::InitSiftMatch()
{
//if (!CheckCudaDevice(GlobalUtil::_DeviceIndex)) {
// std::cout << "ERROR checking cuda device" << std::endl;
// return;
//}
if (_initialized) return;
_initialized = 1;
cutilSafeCall(cudaMalloc(&d_rowMatchDistances, sizeof(float) * 4096));
}
//void* SiftMatchGPU::operator new (size_t size){
// void * p = malloc(size);
// if (p == 0)
// {
// const std::bad_alloc ba;
// throw ba;
// }
// return p;
//}
//int SiftMatchGPU::CheckCudaDevice(int device)
//{
// return ProgramCU::CheckCudaDevice(device);
//}
void SiftMatchGPU::SetDescriptors(int index, int num, unsigned char* d_descriptors, int id)
{
if (_initialized == 0) return;
if (index > 1) index = 1;
if (index < 0) index = 0;
_have_loc[index] = 0;
//the same feature is already set
if (id != -1 && id == _id_sift[index]) return;
_id_sift[index] = id;
if (num > _max_sift) num = _max_sift;
_num_sift[index] = num;
_texDes[index].setImageData(8 * num, 1, 4, d_descriptors);
}
void SiftMatchGPU::SetDescriptorsFromCPU(int index, int num, const unsigned char* descriptors, int id)
{
if (_initialized == 0) return;
if (index > 1) index = 1;
if (index < 0) index = 0;
_have_loc[index] = 0;
//the same feature is already set
if (id != -1 && id == _id_sift[index]) return;
_id_sift[index] = id;
if (num > _max_sift) num = _max_sift;
_num_sift[index] = num;
_texDes[index].InitTexture(8 * num, 1, 4);
_texDes[index].CopyFromHost((void*)descriptors);
}
void SiftMatchGPU::SetDescriptorsFromCPU(int index, int num, const float* descriptors, int id)
{
if (_initialized == 0) return;
if (index > 1) index = 1;
if (index < 0) index = 0;
if (num > _max_sift) num = _max_sift;
sift_buffer.resize(num * 128 / 4);
unsigned char * pub = (unsigned char*)&sift_buffer[0];
for (int i = 0; i < 128 * num; ++i)
{
pub[i] = int(512 * descriptors[i] + 0.5);
}
SetDescriptorsFromCPU(index, num, pub, id);
}
void SiftMatchGPU::SetFeautreLocation(int index, const float* locations, int gap)
{
if (_num_sift[index] <= 0) return;
_texLoc[index].InitTexture(_num_sift[index], 1, 2);
if (gap == 0)
{
_texLoc[index].CopyFromHost(locations);
}
else
{
sift_buffer.resize(_num_sift[index] * 2);
float* pbuf = (float*)(&sift_buffer[0]);
for (int i = 0; i < _num_sift[index]; ++i)
{
pbuf[i * 2] = *locations++;
pbuf[i * 2 + 1] = *locations++;
locations += gap;
}
_texLoc[index].CopyFromHost(pbuf);
}
_have_loc[index] = 1;
}
void SiftMatchGPU::GetSiftMatch(int max_match, ImagePairMatch& imagePairMatch, uint2 keyPointOffset, float distmax, float ratiomax, int mutual_best_match)
{
if (_initialized == 0 || _num_sift[0] <= 0 || _num_sift[1] <= 0) {
cudaMemset(imagePairMatch.d_numMatches, 0, sizeof(int));
return;
}
if (GlobalUtil::_EnableDetailedTimings) {
_timer->startEvent("MultiplyDescriptor");
}
ProgramCU::MultiplyDescriptor(_texDes, _texDes + 1, &_texDot, (mutual_best_match ? &_texCRT : NULL));
if (GlobalUtil::_EnableDetailedTimings) {
_timer->endEvent();
}
GetBestMatch(max_match, imagePairMatch, distmax, ratiomax, keyPointOffset);//, mutual_best_match);
}
void SiftMatchGPU::GetBestMatch(int max_match, ImagePairMatch& imagePairMatch, float distmax, float ratiomax, uint2 keyPointOffset)//, int mbm)
{
_texMatch[0].InitTexture(_num_sift[0], 1);
if (GlobalUtil::_EnableDetailedTimings) {
_timer->startEvent("GetRowMatch");
}
ProgramCU::GetRowMatch(&_texDot, _texMatch, d_rowMatchDistances, distmax, ratiomax);
if (GlobalUtil::_EnableDetailedTimings) {
_timer->endEvent();
}
//_texMatch[1].InitTexture(_num_sift[1], 1);
if (GlobalUtil::_EnableDetailedTimings) {
_timer->startEvent("GetColMatch");
}
ProgramCU::GetColMatch(&_texCRT, distmax, ratiomax, &_texMatch[0], d_rowMatchDistances, imagePairMatch.d_keyPointIndices, imagePairMatch.d_distances, imagePairMatch.d_numMatches, keyPointOffset);
if (GlobalUtil::_EnableDetailedTimings) {
_timer->endEvent();
}
}
void SiftMatchGPU::EvaluateTimings()
{
if (!GlobalUtil::_EnableDetailedTimings) {
std::cout << "Error timings not enabled" << std::endl;
return;
}
else {
_timer->evaluate(true);
}
}
SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift)
{
return new SiftMatchGPU(max_sift);
}