File size: 4,019 Bytes
f12ab4c |
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 |
import numpy as np
import os
import torch
import json
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--in_root', type=str, default="", help='process folder')
parser.add_argument('--out_root', type=str, default="output", help='output folder')
args = parser.parse_args()
in_root = args.in_root
def compute_rotation(angles):
"""
Return:
rot -- torch.tensor, size (B, 3, 3) pts @ trans_mat
Parameters:
angles -- torch.tensor, size (B, 3), radian
"""
batch_size = angles.shape[0]
ones = torch.ones([batch_size, 1])
zeros = torch.zeros([batch_size, 1])
x, y, z = angles[:, :1], angles[:, 1:2], angles[:, 2:],
rot_x = torch.cat([
ones, zeros, zeros,
zeros, torch.cos(x), -torch.sin(x),
zeros, torch.sin(x), torch.cos(x)
], dim=1).reshape([batch_size, 3, 3])
rot_y = torch.cat([
torch.cos(y), zeros, torch.sin(y),
zeros, ones, zeros,
-torch.sin(y), zeros, torch.cos(y)
], dim=1).reshape([batch_size, 3, 3])
rot_z = torch.cat([
torch.cos(z), -torch.sin(z), zeros,
torch.sin(z), torch.cos(z), zeros,
zeros, zeros, ones
], dim=1).reshape([batch_size, 3, 3])
rot = rot_z @ rot_y @ rot_x
return rot.permute(0, 2, 1)[0]
npys = sorted([x for x in os.listdir(in_root) if x.endswith(".npy")])
mode = 1 #1 = IDR, 2 = LSX
outAll={}
for src_filename in npys:
src = os.path.join(in_root, src_filename)
print(src)
dict_load=np.load(src, allow_pickle=True)
angle = dict_load.item()['angle']
trans = dict_load.item()['trans'][0]
R = compute_rotation(torch.from_numpy(angle)).numpy()
trans[2] += -10
c = -np.dot(R, trans)
pose = np.eye(4)
pose[:3, :3] = R
c *= 0.27 # factor to match tripleganger
c[1] += 0.006 # offset to align to tripleganger
c[2] += 0.161 # offset to align to tripleganger
c = c/np.linalg.norm(c)*2.7 ##yiqian教我放到半球上去
pose[0,3] = c[0]
pose[1,3] = c[1]
pose[2,3] = c[2]
focal = 2985.29 # = 1015*1024/224*(300/466.285)#
pp = 512#112
w = 1024#224
h = 1024#224
if mode==1:
count = 0
K = np.eye(3)
K[0][0] = focal
K[1][1] = focal
K[0][2] = w/2.0
K[1][2] = h/2.0
K = K.tolist()
Rot = np.eye(3)
Rot[0, 0] = 1
Rot[1, 1] = -1
Rot[2, 2] = -1
pose[:3, :3] = np.dot(pose[:3, :3], Rot)
pose = pose.tolist()
out = {}
out["intrinsics"] = K
out["pose"] = pose
out["angle"] = (angle * [1, -1, 1]).flatten().tolist()
outAll[src_filename.replace(".npy", ".png")] = out
elif mode==2:
dst = os.path.join(in_root, src_filename.replace(".npy", "_lscam.txt"))
outCam = open(dst, "w")
outCam.write("#focal length\n")
outCam.write(str(focal) + " " + str(focal) + "\n")
outCam.write("#principal point\n")
outCam.write(str(pp) + " " + str(pp) + "\n")
outCam.write("#resolution\n")
outCam.write(str(w) + " " + str(h) + "\n")
outCam.write("#distortion coeffs\n")
outCam.write("0 0 0 0\n")
outCam.write("MATRIX :\n")
for r in range(4):
outCam.write(str(pose[r, 0]) + " " + str(pose[r, 1]) + " " + str(pose[r, 2]) + " " + str(pose[r, 3]) + "\n")
outCam.close()
if mode == 1:
dst = os.path.join(args.out_root, "cameras.json")
with open(dst, "w") as outfile:
json.dump(outAll, outfile, indent=4)
|