-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterpByOpticalFlow.cxx
More file actions
152 lines (116 loc) · 4.44 KB
/
InterpByOpticalFlow.cxx
File metadata and controls
152 lines (116 loc) · 4.44 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
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/optflow.hpp"
#include "iostream"
#include "string"
// This code is adapted from https://github.com/Meltz014/ocv
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " image1 image2 " << std::endl;
return EXIT_FAILURE;
}
std::string temp = argv[1];
std::string base_name = temp.substr(0,temp.find_last_of('.'));
std::string ext = temp.substr(temp.find_last_of('.')+1);
// Read images in
cv::Mat from, to, from_gray, to_gray;
from = cv::imread(argv[1]);
to = cv::imread(argv[2]);
cv::cvtColor( from, from_gray, cv::COLOR_RGB2GRAY );
cv::cvtColor( to, to_gray, cv::COLOR_RGB2GRAY );
// Compute flows
cv::Ptr<cv::DenseOpticalFlow> dof;
cv::Mat flow_fw, flow_bw;
dof = cv::optflow::createOptFlow_DeepFlow( );
dof->calc(from_gray, to_gray, flow_fw);
dof->calc(to_gray, from_gray, flow_bw);
cv::Mat flowxy_fw[2], flowxy_bw[2];
cv::split(flow_fw, flowxy_fw);
cv::split(flow_bw, flowxy_bw);
// Create output image
cv::Mat out(from.rows, from.cols, CV_8SC3, cv::Scalar(0));
cv::Mat out_fw, out_bw;
cv::Vec3b pt_0;
cv::Vec3b pt_1;
double flow_x, flow_x_fw, flow_x_bw;
double flow_y, flow_y_fw, flow_y_bw;
int r_fw_new = 0;
int c_fw_new = 0;
int r_bw_new = 0;
int c_bw_new = 0;
int rows = from_gray.rows;
int cols = from_gray.cols;
int channels = from.channels( );
double steps[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
for (int i = 0; i < 9; i++)
{
double current_ti = steps[i];
cv::addWeighted(from,(1.0f-current_ti),to,current_ti,0.0,out_fw);
cv::addWeighted(from,(1.0f-current_ti),to,current_ti,0.0,out_bw);
for (int r = 0; r < rows; ++r)
{
for ( int c = 0; c < cols; ++c )
{
// forward flow
flow_x_fw = flowxy_fw[ 0 ].at<float>( r, c );
if ( std::isnan( flow_x_fw ) )
{
flow_x_fw = 0.0f;
}
flow_y_fw = flowxy_fw[ 1 ].at<float>( r, c );
if ( std::isnan( flow_y_fw ) )
{
flow_y_fw = 0.0f;
}
// flow forward
flow_x = current_ti*flow_x_fw;
flow_y = current_ti*flow_y_fw;
// compute new image locations
r_fw_new = r + flow_y;
c_fw_new = c + flow_x;
// bounds checking
c_fw_new = std::min( std::max( c_fw_new, 0 ), cols - 1 );
r_fw_new = std::min( std::max( r_fw_new, 0 ), rows - 1 );
cv::Vec3b input_pixel = from.at<cv::Vec3b>(r,c);
cv::Vec3b out_pixel = out_fw.at<cv::Vec3b>(r_fw_new, c_fw_new);
out_pixel[0] = input_pixel[0];
out_pixel[1] = input_pixel[1];
out_pixel[2] = input_pixel[2];
out_fw.at<cv::Vec3b>(r_fw_new, c_fw_new) = out_pixel;
// backward flow
flow_x_bw = flowxy_bw[ 0 ].at<float>( r, c );
if ( std::isnan( flow_x_bw ) )
{
flow_x_bw = 0.0f;
}
flow_y_bw = flowxy_bw[ 1 ].at<float>( r, c );
if ( std::isnan( flow_y_bw ) )
{
flow_y_bw = 0.0f;
}
// flow backward
flow_x = (1-current_ti)*flow_x_bw;
flow_y = (1-current_ti)*flow_y_bw;
// compute new image locations
r_bw_new = r + flow_y;
c_bw_new = c + flow_x;
// bounds checking
c_bw_new = std::min( std::max( c_bw_new, 0 ), cols - 1 );
r_bw_new = std::min( std::max( r_bw_new, 0 ), rows - 1 );
input_pixel = to.at<cv::Vec3b>(r,c);
out_pixel = out_bw.at<cv::Vec3b>(r_bw_new, c_bw_new);
out_pixel[0] = input_pixel[0];
out_pixel[1] = input_pixel[1];
out_pixel[2] = input_pixel[2];
out_bw.at<cv::Vec3b>(r_bw_new, c_bw_new) = out_pixel;
}
}
cv::addWeighted(out_fw,(1.0f - current_ti),out_bw,current_ti,0.0,out);
std::stringstream outfile;
outfile << base_name << "_" << 10*current_ti << "." << ext;
cv::imwrite(outfile.str(), out);
}
return EXIT_SUCCESS;
}