-
Notifications
You must be signed in to change notification settings - Fork 0
/
homography.h
370 lines (301 loc) · 14.2 KB
/
homography.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
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
#ifndef VFS_HOMOGRAPHY_H
#define VFS_HOMOGRAPHY_H
#include <memory>
#include <array>
#include <functional>
#include <cuda_runtime.h>
#include "nppi.h"
#include "/home/bhaynes/projects/eigen/Eigen/Dense"
class CudaImage;
namespace vfs::graphics {
namespace internal {
struct SiftData;
}
template<const size_t channels, typename... T>
class GpuImage;
template<const size_t channels>
class GpuImage<channels> {
public:
GpuImage(void* device, const int step, const size_t height, const size_t width)
: device_(device), step_(step), height_(height), width_(width)
{ }
size_t height() const { return height_; }
size_t width() const { return width_; }
int sheight() const { return static_cast<int>(height_); }
int swidth() const { return static_cast<int>(width_); }
void* device() const { return device_; }
int step() const { return step_; }
NppiSize size() const { return {static_cast<int>(width()), static_cast<int>(height())}; }
NppiRect extent() const { return {0, 0, static_cast<int>(width()), static_cast<int>(height())}; }
template<typename T>
std::vector<T> download() const {
std::vector<T> output;
output.reserve(sizeof(T) * GpuImage<channels>::height() * GpuImage<channels>::width());
if(cudaMemcpy2D(output, sizeof(T) * GpuImage<channels>::width(), device(),
static_cast<size_t>(step()), width(), height(), cudaMemcpyDeviceToHost) != cudaSuccess)
throw std::runtime_error("Error downloading frame");
return output;
}
protected:
void device(void* value) { device_ = value; }
private:
void* device_;
const int step_;
const size_t height_, width_;
};
template<const size_t channels, typename T>
class GpuImage<channels, T>: public GpuImage<channels> {
public:
using allocator_t = std::function<T*(int, int, int*)>;
using copier = std::function<NppStatus(const T*, int, T*, int, NppiSize)>;
//GpuImage(const T* device, const int step, const size_t height, const size_t width)
// : GpuImage<channels>(static_cast<void*>(device), step, height, width), owned_(false)
//{ }
GpuImage(const allocator_t &allocator, const size_t height, const size_t width)
: GpuImage<channels, T>(allocate(allocator, height, width), allocator, height, width)
{ }
GpuImage(const std::vector<T> &data, const allocator_t &allocator, const size_t height, const size_t width)
: GpuImage<channels, T>(allocate(allocator, height, width), allocator, height, width) {
if(cudaMemcpy2D(GpuImage<channels>::device(), static_cast<size_t>(GpuImage<channels>::step()),
data.data(), sizeof(T) * channels * width,
sizeof(T) * channels * width, height, cudaMemcpyHostToDevice) != cudaSuccess)
throw std::runtime_error("Error copying data to GPU");
}
~GpuImage() {
cudaFree(device());
GpuImage<channels>::device(nullptr);
}
T* device() const { return static_cast<T*>(GpuImage<channels>::device()); }
template<typename iterator>
void upload(const iterator data) const {
if(cudaMemcpy2D(device(),
static_cast<size_t>(GpuImage<channels>::step()),
&*data,
sizeof(T) * channels * GpuImage<channels>::width(),
sizeof(T) * channels * GpuImage<channels>::width(),
GpuImage<channels>::height(),
cudaMemcpyHostToDevice) != cudaSuccess)
throw std::runtime_error("Error uploading frame");
}
virtual std::vector<T> download() const {
std::vector<T> output;
output.resize(channels * GpuImage<channels>::height() * GpuImage<channels>::width());
if(cudaMemcpy2D(output.data(),
sizeof(T) * channels * GpuImage<channels>::width(),
device(),
static_cast<size_t>(GpuImage<channels>::step()),
sizeof(T) * channels * GpuImage<channels>::width(),
GpuImage<channels>::height(), cudaMemcpyDeviceToHost) != cudaSuccess)
throw std::runtime_error("Error downloading frame");
return output;
}
size_t byte_step() const { return sizeof(T) * channels * GpuImage<channels>::step(); }
int sbyte_step() const { return static_cast<int>(byte_step()); }
const allocator_t& allocator() const { return allocator_; }
GpuImage<channels, T> slice(
const copier copier,
const NppiRect ®ion) const {
auto output = GpuImage<channels, T>(allocator_, GpuImage<channels>::height(), GpuImage<channels>::width());
slice(output, copier, region);
return output;
}
GpuImage<channels, T>& slice(
GpuImage<channels, T> &output,
const copier &copier,
const NppiRect &input_region,
const NppiSize &output_region={0,0}) const {
auto input_offset = input_region.y * GpuImage<channels>::step() + input_region.x * channels * sizeof(T);
auto output_offset = output_region.height * output.step() + output_region.width * channels * sizeof(T);
//if(input_region.height != output.sheight() - output_region.height || input_region.width != output.swidth() - output_region.width)
// throw std::runtime_error("Slice region does not match output image size");
/*else*/ if(copier(device() + input_offset, GpuImage<channels>::step(), output.device() + output_offset, output.step(),
{input_region.width, input_region.height}) != NPP_SUCCESS)
throw std::runtime_error("Error copying during slice");
return output;
}
private:
GpuImage(const std::pair<T*, int> &pair, const allocator_t allocator, const size_t height, const size_t width)
: GpuImage<channels>(pair.first, pair.second, height, width), owned_(true), allocator_(allocator)
{ }
std::pair<T*, int> allocate(const allocator_t &allocator, const size_t height, const size_t width) {
int step;
T* device;
if(width == 0u || height == 0u) {
throw std::runtime_error("Cannot allocate image with zero width or height");
} else if((device = allocator(static_cast<int>(width), static_cast<int>(height), &step)) != nullptr)
return {device, step};
else
throw std::runtime_error("Failed to allocate memory");
}
const bool owned_;
const allocator_t allocator_;
};
struct Partitions {
struct {
ssize_t x0, x1, y0, y1;
} left, right;
};
class Homography {
public:
explicit Homography()
: Homography(std::array<float, 9>{})
{ }
explicit Homography(const std::array<float, 9> &values)
: values_(values), inverse_{}
{ }
explicit operator float*() {
return values_.data();
}
bool operator==(const Homography& other) const { return values_ == other.values_; }
bool operator!=(const Homography& other) const { return !(*this == other); }
std::array<double[3], 3> matrix() const {
//TODO memoize this
std::array<double[3], 3> matrix;
for(auto i = 0u; i < values_.size(); i++)
matrix.at(i / 3)[i % 3] = values_.at(i);
//matrix.at(i % 3)[i / 3] = values_.at(i);
//matrix[0][0] = 1;
//matrix[0][1] = 0;
//matrix[0][2] = 0;
//matrix[1][0] = 0;
//matrix[1][1] = 1;
//matrix[1][2] = 0;
//matrix[2][0] = 0;
//matrix[2][1] = 0;
//matrix[2][2] = 1;
return matrix;
}
const std::array<float, 9> &inverse() const {
if(!inverted_) {
inverted_ = true;
Eigen::Matrix3f M(values_.data());
//Transpose because Eigen is column-major and we're row-major
Eigen::Map<Eigen::Matrix3f>(inverse_.data(), M.rows(), M.cols()) = M.transpose().inverse().transpose();
}
return inverse_;
}
std::array<double[3], 3> inverse3x3(const NppiSize translation={0,0}) const {
//TODO memoize this
std::array<double[3], 3> matrix;
const auto &linear = inverse();
/* inverted_ = false;
const_cast<float*>(values_.data())[5] += 200;
const auto &linear = inverse();
const_cast<float*>(values_.data())[5] -= 200;
inverted_ = false;*/
for (auto i = 0u; i < linear.size(); i++)
matrix.at(i / 3)[i % 3] = linear.at(i);
if(translation.width) {
matrix[0][0] += translation.width * matrix[2][0];
matrix[0][1] += translation.width * matrix[2][1];
matrix[0][2] += translation.width * matrix[2][2];
}
if(translation.height) {
matrix[1][0] += translation.height * matrix[2][0];
matrix[1][1] += translation.height * matrix[2][1];
matrix[1][2] += translation.height * matrix[2][2];
}
//matrix[0][0] += translation.height;
//matrix[0][1] += translation.width;
//matrix[0][2] += translation.width;
//***matrix[1][2] += translation.height;
//matrix[0][0] += translation.width;
//*matrix[0][1] += translation.width;
//*matrix[0][2] += translation.width; //*
//*matrix[2][0] += translation.height;
//*matrix[2][1] += translation.height;
//*matrix[2][2] += translation.height;
return matrix;
}
Partitions partitions(const NppiSize &size) const {
/*
# Left overlap at x0
i = np.array([0, 0, 1])
#Hi = np.linalg.inv(H)
H0 = Hi.dot(i)
H0n = H0 / H0[2]
x0 = H0n[0]
p0 = int(x0)
*/
const auto &H = values_;
const auto &Hi = inverse();
// M[0]/[2], where M = H^{-1} \dot [0 0 1]
auto right0 = Hi[6] / Hi[8];
// M[0]/[2], where M = H^{-1} \dot [width 0 1]
auto right1 = ((size.width * Hi[0]) + Hi[6]) / Hi[8];
// M[0]/[2], where M = H \dot [width 0 1]
auto left0 = H[6] / H[8];
// M[0]/[2], where M = H \dot [width 0 1]
auto left1 = (size.width * H[0] + H[2]) / (size.width * H[6] + H[8]);
auto top_right0 = Hi[5] / Hi[8];
auto top_right1 = -((size.width * Hi[3]) + Hi[5]) / Hi[8];
/*auto h00 = (size.width * H[0]);
auto h02 = (1 * H[2]);
auto h0 = h00 + h02;
auto h20 = size.width * H[6];
auto h22 = H[8];
auto h2 = h20 + h22;
auto h = h0 / h2;*/
if(left1 > size.width) {
left0 = std::max(0.0f, left0 - left1 - size.width);
left1 = size.width;
}
if(right1 > size.width) {
right0 = std::max(0.0f, left0 - left1 - size.width);
right1 = size.width;
}
assert(left0 >= -0.5);
assert(left1 >= left0);
assert(right0 >= -0.5);
assert(right1 >= right0);
//auto tmp = left1;
//left1 = right1;
//right1 = tmp;
//left0 += 256;
//right0 += 256;
return {{static_cast<size_t>(left0),
static_cast<size_t>(left1) + static_cast<size_t>(left1) % 2,
0,
0},
{static_cast<size_t>(right0),
static_cast<size_t>(right1) + static_cast<size_t>(right1) % 2,
static_cast<size_t>(top_right0) + static_cast<size_t>(top_right0) % 2,
static_cast<size_t>(top_right1) + static_cast<size_t>(top_right1) % 2}};
//std::make_pair<size_t, size_t>(static_cast<size_t>(left0), static_cast<size_t>(left1));
}
private:
std::array<float, 9> values_;
mutable std::array<float, 9> inverse_;
mutable bool inverted_ = false;
};
class SiftConfiguration {
public:
SiftConfiguration(size_t height, size_t width,
float blur = 1.0f, float threshold = 3.0f, int octaves = 5, bool scale = false);
~SiftConfiguration();
CudaImage& left() { return *left_; }
CudaImage& right() { return *right_; }
internal::SiftData& left_data() { return *left_data_; }
internal::SiftData& right_data() { return *right_data_; }
int octaves() const { return octaves_; }
float blur() const { return blur_; }
float threshold() const { return threshold_; }
float* scratch() const { return scratch_; }
private:
const size_t height_, width_;
const float blur_, threshold_; //4.5f
const int octaves_;
const bool scale_;
const std::unique_ptr<CudaImage> left_, right_;
const std::unique_ptr<internal::SiftData> left_data_, right_data_;
float *scratch_;
};
Homography find_homography(const std::vector<unsigned char>&, const std::vector<unsigned char>&,
size_t height, size_t width);
Homography find_homography(const std::vector<unsigned char>&, const std::vector<unsigned char>&,
size_t, size_t, SiftConfiguration&);
Homography find_homography(const GpuImage<3, Npp8u>&, const GpuImage<3, Npp8u>&, SiftConfiguration&);
Homography find_homography(const GpuImage<1, Npp8u>&, const GpuImage<1, Npp8u>&, SiftConfiguration&);
Homography find_homography(const GpuImage<1, Npp32f>&, const GpuImage<1, Npp32f>&, SiftConfiguration&);
}
#endif //VFS_HOMOGRAPHY_H