forked from NickStupich/Rolling-Shutter-Video-Stabilization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullFrameTransform.cpp
85 lines (70 loc) · 3.65 KB
/
FullFrameTransform.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
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <time.h>
#include <numeric>
#include "svd.h"
#include "structures.h"
#include "settings.h"
#include "coreFuncs.h"
#include "FullFrameTransform.h"
#include "nullTransform.h"
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/video/tracking.hpp>
#include "opencv2/imgproc/imgproc_c.h"
FullFrameTransform::FullFrameTransform(){
Transformation tr = {0, 0, 0, 0, 0, 1, 0}; //cos term is 1
wholeFrameTransform = tr;
AbsoluteTransformation at = {wholeFrameTransform, 0, 0};
absoluteWholeFrameTransform = at;
}
FullFrameTransform::FullFrameTransform(Mat img1, Mat img2, int index0, int index1, bool evalShifts){
getWholeFrameTransform(img1, img2);
AbsoluteTransformation at = {wholeFrameTransform, 0, 0};
absoluteWholeFrameTransform = at;
imgBound ib = {0, img1.cols, 0, img1.rows};
frameBound = ib;
#ifdef SHFITS_FILENAME
if(evalShifts){
evalTransforms(index0, index1, (char*)SHFITS_FILENAME);
}
#endif
}
void FullFrameTransform::getWholeFrameTransform(Mat img1, Mat img2){
FeaturesInfo fi1 = extractFeaturesToTrack(img1);
FeaturesInfo fi2 = extractFeaturesToTrack(img2);
int length = max(fi1.features.size(), fi2.features.size());
std::vector<uchar> features_found;
features_found.reserve(length);
std::vector<float> feature_errors;
feature_errors.reserve(length);
calcOpticalFlowPyrLK( fi1.pyramid, fi2.pyramid, fi1.features, fi2.features, features_found, feature_errors ,
Size( WIN_SIZE, WIN_SIZE ), 5,
cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30, 0.01 ), 0 );
//wholeFrameTransform = densityWeightedSvd(fi1.features, fi2.features, features_found.size());
//wholeFrameTransform = prunedNonWeightedSvd(fi1.features, fi2.features, features_found.size());
wholeFrameTransform = WelschFit(fi1.features, fi2.features, features_found.size());
//wholeFrameTransform = RansacNonWeightedSvd(fi1.features, fi2.features, features_found.size());
//wholeFrameTransform = nonWeightedSvd(fi1.features, fi2.features, features_found.size());
}
void FullFrameTransform::CreateAbsoluteTransform(FullFrameTransform prevTransform){
float ix = prevTransform.absoluteWholeFrameTransform.idx * TRANSLATION_DECAY - prevTransform.wholeFrameTransform.ux1 + prevTransform.wholeFrameTransform.ux2;
float iy = prevTransform.absoluteWholeFrameTransform.idy * TRANSLATION_DECAY - prevTransform.wholeFrameTransform.uy1 + prevTransform.wholeFrameTransform.uy2;
absoluteWholeFrameTransform.trans = wholeFrameTransform;
absoluteWholeFrameTransform.idx = ix;
absoluteWholeFrameTransform.idy = iy;
absoluteWholeFrameTransform.trans.rotation = prevTransform.absoluteWholeFrameTransform.trans.rotation * ROTATION_DECAY + wholeFrameTransform.rotation;
absoluteWholeFrameTransform.trans.cos = cos(absoluteWholeFrameTransform.trans.rotation);
absoluteWholeFrameTransform.trans.sin = sin(absoluteWholeFrameTransform.trans.rotation);
}
void FullFrameTransform::TransformPoint(float x, float y, float &x2, float &y2){
GenericTransformPoint(wholeFrameTransform, x, y, x2, y2);
}
void FullFrameTransform::TransformPointAbs(float x, float y, float &x2, float &y2){
GenericTransformPointAbs(absoluteWholeFrameTransform, x, y, x2, y2);
}