-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalignment.hpp
99 lines (79 loc) · 3.02 KB
/
alignment.hpp
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
#pragma once
#include "imgproc.hpp"
struct VideoAlignerParams {
/*
Enable initialization from phase correlation.
This can be useful for handling fast camera pans.
Otherwise it's unlikely to be useful.
*/
bool phase_correlate = false;
double phase_correlate_threshold = 0.5;
/*
There is a sweet spot for this threshold.
Too low: Will iterate too many times, accumulating errors until it diverges.
Too high: Will iterate too few times, creating visual errors and/or diverging more.
*/
double threshold = 0.03;
/*
The smallest fraction of the image to use for the sparse set.
Too small: Will not find enough keypoints to do a good alignment.
Too large: Will accept too many false positives, and may not converge.
*/
float smallest_fraction = 0.5f;
/*
The maximum number of iterations to run before giving up at each level.
*/
int max_iters = 64;
/*
The minimum width or height of the image pyramid (smallest layer size).
*/
int pyramid_min_width = 20;
int pyramid_min_height = 20;
// Maximum converged displacement in pixels at any pyramid level
double max_displacement = 8.0;
};
/*
(1) Keyframe every other frame
(2) Use cv::phaseCorrelate to find the initial x/y shift guess
(3) Compute image pyramids
(4) Precompute x/y gradient images and Hessians
(5) Lucas-Kanade iterations
(6) Flip sign based on keyframe ahead or behind and return result
*/
class VideoAligner
{
public:
// Returns false if track is lost or kernel fails
bool AlignNextFrame(
const cv::Mat& frame,
SimilarityTransform& transform,
const VideoAlignerParams& params = VideoAlignerParams());
protected:
// Alternating frame indexing
int CurrFrameIndex = 0;
int PrevFrameIndex = 1;
int FramesAccumulated = 0;
const int KeyframeIndex = 1;
const int NonKeyframeIndex = 0;
int PyramidLevels = -1;
const int PhaseLevel = 2;
int LastWidth = -1, LastHeight = -1;
cv::Mat GrayInput[2];
std::vector<Halide::Runtime::Buffer<uint8_t>> ScalePyramid[2];
std::vector<Halide::Runtime::Buffer<float>> KeyframeGradX, KeyframeGradY;
std::vector<Halide::Runtime::Buffer<uint16_t>> KeyframeArgMaxX, KeyframeArgMaxY;
std::vector<int> KeyframeTileSize;
std::vector<Halide::Runtime::Buffer<float>> KeyframeJacobianX, KeyframeJacobianY;
cv::Mat PhaseImage[2];
struct DeltaPixel {
uint16_t abs_delta, tile_x, tile_y;
};
std::vector<DeltaPixel> DeltaPixelsX, DeltaPixelsY;
std::vector<Halide::Runtime::Buffer<uint16_t>> SelectedPixelsX, SelectedPixelsY;
std::vector<Halide::Runtime::Buffer<float>> SelectedJacobianX, SelectedJacobianY;
std::vector<Halide::Runtime::Buffer<uint16_t>> WarpDiffX, WarpDiffY;
Halide::Runtime::Buffer<double> IcaResult;
// Returns false if this is the first frame
bool ComputePyramid(const cv::Mat& inputFrame, const VideoAlignerParams& params);
bool ComputeKeyFrame();
};