-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEQRToCube.m
91 lines (75 loc) · 2.21 KB
/
EQRToCube.m
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
function [top, bot, front, back, right,left] = EQRToCube(image)
%EQRTOCUBE Converts equirectangular image to cube images.
% Detailed explanation goes here
[H,W,C] = size(image);
faceSide = floor(sqrt((H*W)/ 6));
normalize = @(p) ((p - (faceSide/2)) / (faceSide/2));
mapH = @(y) min(H,max(1, round(H*((y + (pi/2)) / pi))));
mapW = @(x) min(W,max(1, round(W*((x + pi) / (2*pi)))));
top = zeros([faceSide, faceSide, C], 'uint8');
bot = zeros([faceSide, faceSide, C], 'uint8');
front = zeros([faceSide, faceSide, C], 'uint8');
back = zeros([faceSide, faceSide, C], 'uint8');
right = zeros([faceSide, faceSide, C], 'uint8');
left = zeros([faceSide, faceSide, C], 'uint8');
fixed = 1; % fix z = 1: top.
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(normalize(i),normalize(j),fixed);
h = mapH(-phi);
w = mapW(theta);
top(i,j,:) = image(h,w,:);
end
end
top = rot90(top);
fixed = -1; % fix z = -1: bottom
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(normalize(i),normalize(j),fixed);
h = mapH(-phi);
w = mapW(theta);
bot(i,j,:) = image(h,w,:);
end
end
bot = flip(rot90(bot, -1),2);
fixed = 1; % fix x = 1: front
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(normalize(i),fixed,normalize(j));
h = mapH(-phi);
w = mapW(theta);
front(i,j,:) = image(h,w,:);
end
end
front = flip(rot90(front),2);
fixed = -1; % fix x = -1: back
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(normalize(i),fixed,normalize(j));
h = mapH(-phi);
w = mapW(theta);
back(i,j,:) = image(h,w,:);
end
end
back = rot90(back);
fixed = 1; % fix y = 1: left
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(fixed,normalize(i),normalize(j));
h = mapH(-phi);
w = mapW(theta);
left(i,j,:) = image(h,w,:);
end
end
left = rot90(left);
fixed = -1; % fix y = -1: right
for i = 1:faceSide
for j = 1:faceSide
[theta,phi,~] = cart2sph(fixed,normalize(i),normalize(j));
h = mapH(-phi);
w = mapW(theta);
right(i,j,:) = image(h,w,:);
end
end
right = flip(rot90(right,-1),1);
end