forked from minogump/360ImagePyramidProjection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.cpp
304 lines (250 loc) · 8.26 KB
/
convert.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
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
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
static inline double radians(const double deg) { return deg * CV_PI / 180.0; }
// convertCoordinate from coordinate A to B
// x 是沿所求坐标系转换为原坐标系X轴旋转的角度
// y 是沿所求坐标系转换为原坐标系y轴旋转的角度
// z 是沿所求坐标系转换为原坐标系z轴旋转的角度
// sequence 是矩阵旋转的次序,0是x->y->z,1是x->z->y,2是y->x->z,3是y->z->x,4是z->x->y,5是z->y->x
Mat convertCoordinate(double x, double y, double z, int sequence)
{
Mat mx = (Mat_<double>(3, 3) << 1, 0, 0, 0, cos(x), -sin(x), 0, sin(x), cos(x));
Mat my = (Mat_<double>(3, 3) << cos(y), 0, sin(y), 0, 1, 0, -sin(y), 0, cos(y));
Mat mz = (Mat_<double>(3, 3) << cos(z), -sin(z), 0, sin(z), cos(z), 0, 0, 0, 1);
Mat A = Mat::zeros(3, 3, CV_64FC1);
if (sequence == 0)
A = mz * my * mx;
else if (sequence == 1)
A = my * mz * mx;
else if (sequence == 2)
A = mz * mx * my;
else if (sequence == 3)
A = mx * mz * my;
else if (sequence == 4)
A = my * mx * mz;
else if (sequence == 5)
A = mx * my * mz;
return A;
}
Mat A1 = convertCoordinate(radians(0), radians(90), radians(-45), 5);
Mat B1 = convertCoordinate(radians(45), -asin(1.0 / sqrt(3)) - CV_PI / 2.0, radians(0), 2);
Mat A2 = convertCoordinate(radians(0), radians(0), radians(45), 2);
Mat B2 = convertCoordinate(radians(-45), asin(1.0 / sqrt(3)) - CV_PI, radians(0), 2);
Mat A3 = convertCoordinate(radians(180), radians(90), radians(-45), 1);
Mat B3 = convertCoordinate(radians(135), asin(1.0 / sqrt(3)) - CV_PI / 2.0, radians(0), 2);
Mat A4 = convertCoordinate(radians(45), radians(-90), radians(0), 3);
Mat B4 = convertCoordinate(radians(45), asin(1.0 / sqrt(3)) - CV_PI / 2.0, radians(0), 2);
// get x, y, z coords from out image pixels coords
// i, j 是输出图像的xy坐标
// face 是面的号码
// halfOutSize 输出图像宽度的一半
// 将输出图像的xy值转换为三维空间中的xyz坐标,对应成一个坐标值由 -1到1的立方体
//Vec3d outImgToXYZ(int i, int j, int face, int halfOutSize, int toward,
// const Mat & A1, const Mat & A2, const Mat & A3, const Mat & A4,
// const Mat & B1, const Mat & B2, const Mat & B3, const Mat & B4)
Vec3d outImgToXYZ(float i, float j, int face, int halfOutSize, int toward)
{
double a, b;
Vec3d vec(0, 0, 0);
Mat c = Mat::zeros(3, 1, CV_64FC1);
Mat d = Mat::zeros(3, 1, CV_64FC1);
Mat t = Mat::zeros(3, 1, CV_64FC1);
if (toward == 0) // front face
{
a = i * 4.0 / (halfOutSize * 2);
b = j * 4.0 / (halfOutSize * 2);
if (face == 0) // down
{
a = a - 2.0;
b = 2.0 - b;
vec = Vec3d(sqrt(3) - 1, a, b);
}
else if (face == 1) // left top
{
t.at<double>(0, 0) = b - 1;
t.at<double>(1, 0) = a - 1;
t.at<double>(2, 0) = 0;
//first coordinate conversion
c = A1 * t;
c.at<double>(2, 0) = c.at<double>(2, 0) * sqrt(3); // stretch;
//second coordinate conversion
c.at<double>(0, 0) = c.at<double>(0, 0) - (1 - sqrt(3));
c.at<double>(1, 0) = c.at<double>(1, 0) - 0;
c.at<double>(2, 0) = c.at<double>(2, 0) - sqrt(2);
d = B1 * c;
vec = Vec3d(d.at<double>(0, 0), d.at<double>(1, 0), d.at<double>(2, 0));
}
else if (face == 2) // left bottom
{
t.at<double>(0, 0) = b - 3;
t.at<double>(1, 0) = a - 1;
t.at<double>(2, 0) = 0;
//first coordinate conversion
c = A2 * t;
c.at<double>(0, 0) = c.at<double>(0, 0) * sqrt(3); // stretch
//second coordinate conversion
c.at<double>(0, 0) = c.at<double>(0, 0) - sqrt(2);
c.at<double>(1, 0) = c.at<double>(1, 0) - 0;
c.at<double>(2, 0) = c.at<double>(2, 0) - (1 - sqrt(3));
d = B2 * c;
vec = Vec3d(d.at<double>(0, 0), d.at<double>(1, 0), d.at<double>(2, 0));
}
else if (face == 3) // right top
{
t.at<double>(0, 0) = b - 1;
t.at<double>(1, 0) = a - 3;
t.at<double>(2, 0) = 0;
// first coordinate conversion
c = A3 * t;
c.at<double>(2, 0) = c.at<double>(2, 0) * sqrt(3); // stretch
// second coordinate conversion
c.at<double>(0, 0) = c.at<double>(0, 0) - (sqrt(3) - 1);
c.at<double>(1, 0) = c.at<double>(1, 0) - 0;
c.at<double>(2, 0) = c.at<double>(2, 0) - sqrt(2);
d = B3 * c;
vec = Vec3d(d.at<double>(0, 0), d.at<double>(1, 0), d.at<double>(2, 0));
}
else if (face == 4) // right bottom
{
t.at<double>(0, 0) = b - 3;
t.at<double>(1, 0) = a - 3;
t.at<double>(2, 0) = 0;
// first coordinate conversion
c = A4 * t;
c.at<double>(2, 0) = c.at<double>(2, 0) * sqrt(3);; // stretch
// second coordinate conversion
c.at<double>(0, 0) = c.at<double>(0, 0) - (sqrt(3) - 1);
c.at<double>(1, 0) = c.at<double>(1, 0) - 0;
c.at<double>(2, 0) = c.at<double>(2, 0) - sqrt(2);
d = B4 * c;
vec = Vec3d(d.at<double>(0, 0), d.at<double>(1, 0), d.at<double>(2, 0));
}
}
return vec;
}
void mapShpereToPyramidCoordinate(
const float i,
const float j,
int face,
const Size SphereSize,
int halfOutSize,
float & srcX,
float & srcY)
{
int edge = SphereSize.width / 4;
Vec3d vec = outImgToXYZ(i, j, face, halfOutSize, 0);
double theta = atan2(vec[1], vec[0]); // 水平方向夹角
double r = hypot(vec[0], vec[1]); // 计算斜边长
double phi = atan2(vec[2], r); // 垂直方向夹角
// 对应原图像的坐标值
srcX = (float)(2.0 * edge * (theta + CV_PI) / CV_PI);
srcY = (float)(2.0 * edge * (CV_PI / 2 - phi) / CV_PI);
}
Mat getConvertMap(const Size SphereSize, const Size PyramidSize)
{
Mat convertMap = Mat(PyramidSize, CV_32FC2);
int halfOutSize = PyramidSize.width / 2;
int face = 0;
for (int i = 0; i < PyramidSize.width; ++i)
{
for (int j = 0; j < PyramidSize.height; ++j)
{
face = 0;
if (fabs(double(halfOutSize - i)) + fabs(double(halfOutSize - j)) <= halfOutSize)
{
face = 0;
}
else if (i < halfOutSize && j < halfOutSize)
{
face = 1; // 左上角
}
else if (i < halfOutSize && j > halfOutSize)
{
face = 2; // 左下角
}
else if (i > halfOutSize && j < halfOutSize)
{
face = 3; // 右上角
}
else if (i > halfOutSize && j > halfOutSize)
{
face = 4; // 右下角
}
float srcX;
float srcY;
mapShpereToPyramidCoordinate((float)i, (float)j, face, SphereSize, halfOutSize, srcX, srcY);
convertMap.at<Point2f>(j, i) = Point2f(srcX, srcY);
}
}
return convertMap;
}
void convert2Pyr(const cv::Mat& SphereImage, Mat & PyramidImage)
{
Mat convertMap = getConvertMap(SphereImage.size(), PyramidImage.size());
remap(SphereImage, PyramidImage, convertMap, Mat(), CV_INTER_CUBIC, BORDER_WRAP);
}
// convert using an inverse transformation
void convertBack(const Mat & imgIn, Mat & imgOut)
{
Size inSize = imgIn.size();
Size outSize = imgOut.size();
int edge = inSize.width / 4; // 视角宽度
int halfOutSize = outSize.width / 2;
int face = 0;
for (int i = 0; i < outSize.width; i++)
{
for (int j = 0; j < outSize.height; j++)
{
face = 0;
if (fabs(double(halfOutSize - i)) + fabs(double(halfOutSize - j)) <= halfOutSize)
{
face = 0;
}
else if (i < halfOutSize && j < halfOutSize)
{
face = 1; // 左上角
}
else if (i < halfOutSize && j > halfOutSize)
{
face = 2; // 左下角
}
else if (i > halfOutSize && j < halfOutSize)
{
face = 3; // 右上角
}
else if (i > halfOutSize && j > halfOutSize)
{
face = 4; // 右下角
}
Vec3d vec = outImgToXYZ((float)i, (float)j, face, halfOutSize, 0);
double theta = atan2(vec[1], vec[0]); // 水平方向夹角
double r = hypot(vec[0], vec[1]); // 计算斜边长
double phi = atan2(vec[2], r); // 垂直方向夹角
// 对应原图像的坐标值
double uf = (2.0 * edge * (theta + CV_PI) / CV_PI);
double vf = (2.0 * edge * (CV_PI / 2 - phi) / CV_PI);
int ui = int(uf) % inSize.width;
int vi = vf < 0 ? 0 : int(vf);
vi = vi > inSize.height - 1 ? inSize.height - 1 : vi;
imgOut.at<Vec3b>(j, i) = imgIn.at<Vec3b>(vi, ui);
}
}
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("usage : convert.exe shpere.jpg pyramid.jpg\n");
return -1;
}
Mat imgIn = imread(argv[1]);
int outWidth = (int)(imgIn.size().width * sqrt(2) / 4);
Mat imgOut(Size(outWidth, outWidth), CV_8UC3);
printf("Converting ...\n");
//convertBack(imgIn, imgOut);
convert2Pyr(imgIn, imgOut);
imwrite(argv[2], imgOut);
return 0;
}