-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_warping.cpp
225 lines (182 loc) · 7.62 KB
/
image_warping.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
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
#include <cmath>
#include <iostream>
#include "image_warping.h"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
const float PI = 3.14159265358979f;
void cal_trans_matrix(float roll, float pitch, float yaw, uint16_t image_width, uint16_t image_height, float *trans_matrix)
{
// rotation matrix
Mat RX, RY, RZ, trans_matrix_mat;
Mat A1, A2, T, R;
float theta, phi, gamma;
float d, focal;
// convert degree to rad
gamma = roll * PI / 180.0;
theta = pitch * PI / 180.0;
phi = yaw * PI / 180.0;
d = sqrt(image_height * image_height + image_width * image_width);
focal = gamma != 0 ? (d / (2 * sin(gamma))) : d;
A1 = (Mat_<float>(4, 3) << 1.0, 0, -0.5 * image_width,
0, 1, -0.5 * image_height,
0, 0, 1,
0, 0, 1);
RX = (Mat_<float>(4, 4) << 1.0, 0, 0, 0,
0, cos(theta), -sin(theta), 0,
0, sin(theta), cos(theta), 0,
0, 0, 0, 1);
RY = (Mat_<float>(4, 4) << cos(phi), 0, -sin(phi), 0,
0, 1, 0, 0,
sin(phi), 0, cos(phi), 0,
0, 0, 0, 1);
RZ = (Mat_<float>(4, 4) << cos(gamma), -sin(gamma), 0, 0,
sin(gamma), cos(gamma), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
T = (Mat_<float>(4, 4) << 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, focal,
0, 0, 0, 1);
R = ((RX * RY) * RZ);
A2 = (Mat_<float>(3, 4) << focal, 0, 0.5 * image_width, 0,
0, focal, 0.5 * image_height, 0,
0, 0, 1, 0);
trans_matrix_mat = A2 * (T * (R * A1));
memcpy(trans_matrix, trans_matrix_mat.data, 3 * 3 * sizeof(float));
// reference : https://github.com/eborboihuc/rotate_3d
}
void warp_rgb_image(float *trans_matrix, uint8_t *image, uint16_t image_width,
uint16_t image_height, uint8_t *warp_image)
{
float x, y, z, idx_z;
uint16_t x1, x2, y1, y2;
uint8_t p11, p12, p22, p21;
float dx1, dx2, dy1, dy2;
float tmp1, tmp2;
for (int i = 0; i < image_height; i++)
{
for (int j = 0; j < image_width; j++)
{
x = trans_matrix[0] * j + trans_matrix[1] * i + trans_matrix[2];
y = trans_matrix[3] * j + trans_matrix[4] * i + trans_matrix[5];
z = trans_matrix[6] * j + trans_matrix[7] * i + trans_matrix[8];
x = x / z;
y = y / z;
x1 = floor(x);
y1 = floor(y);
x2 = ceil(x);
y2 = ceil(y);
dx1 = x - x1;
dx2 = 1.0 - dx1;
dy1 = y - y1;
dy2 = 1.0 - dy1;
// intepolate r
if (x1 > 0 && x1 < image_width && x2 > 0 && x2 < image_width && y1 > 0 && y1 < image_height && y2 > 0 && y2 < image_height)
{
p11 = image[3 * (y1 * image_width + x1)];
p21 = image[3 * (y2 * image_width + x1)];
p12 = image[3 * (y1 * image_width + x2)];
p22 = image[3 * (y2 * image_width + x2)];
tmp1 = dx2 * p11 + dx1 * p12;
tmp2 = dx2 * p21 + dx1 * p22;
warp_image[3 * (i * image_width + j)] = (uint8_t)(dy2 * tmp1 + dy1 * tmp2);
// intepolate g
p11 = image[3 * (y1 * image_width + x1) + 1];
p21 = image[3 * (y2 * image_width + x1) + 1];
p12 = image[3 * (y1 * image_width + x2) + 1];
p22 = image[3 * (y2 * image_width + x2) + 1];
tmp1 = dx2 * p11 + dx1 * p12;
tmp2 = dx2 * p21 + dx1 * p22;
warp_image[3 * (i * image_width + j) + 1] = (uint8_t)(dy2 * tmp1 + dy1 * tmp2);
// intepolate b
p11 = image[3 * (y1 * image_width + x1) + 2];
p21 = image[3 * (y2 * image_width + x1) + 2];
p12 = image[3 * (y1 * image_width + x2) + 2];
p22 = image[3 * (y2 * image_width + x2) + 2];
tmp1 = dx2 * p11 + dx1 * p12;
tmp2 = dx2 * p21 + dx1 * p22;
warp_image[3 * (i * image_width + j) + 2] = (uint8_t)(dy2 * tmp1 + dy1 * tmp2);
}
}
}
}
void convert_trans_mat_to_fixed(float *trans_matrix, int32_t trans_matrix_fixed[16])
{
// convert trans_matrix to fixed
for (int i = 0; i < 9; i++)
{
trans_matrix_fixed[i] = trans_matrix[i] * (float)(1 << TRAN_MAT_FRACT_BITS);
}
}
void warp_rgb_image_fixed(int32_t* trans_matrix_fixed, uint8_t *image, uint16_t image_width,
uint16_t image_height, uint8_t *warp_image)
{
int32_t x, y, z;
uint32_t x1, x2, y1, y2;
uint8_t p11, p12, p22, p21;
uint32_t dx1, dx2, dy1, dy2;
uint64_t tmp1, tmp2;
double inv_z;
uint32_t fixed_inv_z;
for (int32_t i = 0; i < image_height; i++)
{
for (int32_t j = 0; j < image_width; j++)
{
x = trans_matrix_fixed[0] * j + trans_matrix_fixed[1] * i + trans_matrix_fixed[2];
y = trans_matrix_fixed[3] * j + trans_matrix_fixed[4] * i + trans_matrix_fixed[5];
z = trans_matrix_fixed[6] * j + trans_matrix_fixed[7] * i + trans_matrix_fixed[8];
x1 = x / z;
y1 = y / z;
if (x == x1 * z)
{
x2 = x1;
}
else
{
x2 = x1 + 1;
}
if (y == y1 * z)
{
y2 = y1;
}
else
{
y2 = y1 + 1;
}
dx1 = x - x1 * z;
dx2 = z - dx1;
dy1 = y - y1 * z;
dy2 = z - dy1;
inv_z = (1.0 / z);
fixed_inv_z = inv_z * (1 << Z_FRACT_BITS);
// intepolate r
if (x1 > 0 && x1 < image_width && x2 > 0 && x2 < image_width && y1 > 0 && y1 < image_height && y2 > 0 && y2 < image_height)
{
p11 = image[3 * (y1 * image_width + x1)];
p21 = image[3 * (y2 * image_width + x1)];
p12 = image[3 * (y1 * image_width + x2)];
p22 = image[3 * (y2 * image_width + x2)];
tmp1 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p11 + dx1 * p12))) >> Z_FRACT_BITS;
tmp2 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p21 + dx1 * p22))) >> Z_FRACT_BITS;
warp_image[3 * (i * image_width + j)] = (uint8_t)(((uint64_t)fixed_inv_z * (uint64_t)(dy2 * tmp1 + dy1 * tmp2)) >> Z_FRACT_BITS);
// intepolate g
p11 = image[3 * (y1 * image_width + x1) + 1];
p21 = image[3 * (y2 * image_width + x1) + 1];
p12 = image[3 * (y1 * image_width + x2) + 1];
p22 = image[3 * (y2 * image_width + x2) + 1];
tmp1 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p11 + dx1 * p12))) >> Z_FRACT_BITS;
tmp2 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p21 + dx1 * p22))) >> Z_FRACT_BITS;
warp_image[3 * (i * image_width + j) + 1] = (uint8_t)(((uint64_t)fixed_inv_z * (uint64_t)(dy2 * tmp1 + dy1 * tmp2)) >> Z_FRACT_BITS);
// intepolate b
p11 = image[3 * (y1 * image_width + x1) + 2];
p21 = image[3 * (y2 * image_width + x1) + 2];
p12 = image[3 * (y1 * image_width + x2) + 2];
p22 = image[3 * (y2 * image_width + x2) + 2];
tmp1 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p11 + dx1 * p12))) >> Z_FRACT_BITS;
tmp2 = (((uint64_t)fixed_inv_z * (uint64_t)(dx2 * p21 + dx1 * p22))) >> Z_FRACT_BITS;
warp_image[3 * (i * image_width + j) + 2] = (uint8_t)(((uint64_t)fixed_inv_z * (uint64_t)(dy2 * tmp1 + dy1 * tmp2)) >> Z_FRACT_BITS);
}
}
}
}