In instant-ngp, the transforms.json file can be automatically generated by calling scripts/colmap2nerf.py, that is, the camera parameters, but sometimes these parameters are obtained from the camera itself, in order to automatically generate instant-ngp for each camera parameter information, that is, the txt content The supported transforms.json file is implemented in python here.
The format of the txt content of each camera is as follows: the first three lines are camera internal parameters, including fx, fy, cx, cy; the last three lines are camera external parameters, which are Rt
8242.163473484103 0 2576.928611903816 0
0 8242.163473484103 1733.503691370957 0
0 0 1 0
2.044289726145588e-004 -0.2587487517264626 -0.9659446369688031 27.59432346095996
-0.9993063898830017 -3.602307923217642e-002 9.438056030485108e-003 -0.6400803719560595
-3.723838540803551e-002 0.9652727185840433 -0.2585766451355823 35.62807466319453
The python implementation is as follows:
import os
import json
def get_dir_list(path):
dir_list = []
for x in os.listdir(path):
if x.endswith(".txt"):
dir_list.append(path+"/"+x)
return dir_list
def vec_to_matrix(els):
matrix = [
[
els[0][0],
els[0][1],
els[0][2],
els[0][3]
],
[
els[1][0],
els[1][1],
els[1][2],
els[1][3]
],
[
els[2][0],
els[2][1],
els[2][2],
els[2][3],
],
[
0.0,
0.0,
0.0,
1.0
]
]
return matrix
def get_image_name(txt_name, img_suffix_name):
pos = txt_name.rfind("/")
name = txt_name[pos+1:]
name = name.replace("txt", img_suffix_name)
return "images/" + name
def write_to_json(dir_list, out_file, img_suffix_name):
out = {
"frames": [],
}
for x in dir_list:
with open(os.path.join(x), "r") as f:
els = [] # 6*4
for line in f:
if line[0] == "#":
continue
tmp = []
for v in line.split(" "):
tmp.append(v.replace("\n", "")) # remove line breaks(\n) at the end of the line
ret = [float(ele) for ele in tmp] # str to float
els.append(ret)
if len(els) != 6:
print(f"Error: the number of rows that are not supported:{len(els)}")
raise
# camera intrinsics
fl_x = els[0][0]
fl_y = els[1][1]
cx = els[0][2]
cy = els[1][2]
# camera extrinsics: R t
transform_matrix = vec_to_matrix(els[3:6])
image_name = get_image_name(x, img_suffix_name)
frame = {"file_path":image_name,"fl_x":fl_x,"fl_y":fl_y,"cx":cx,"cy":cy,"transform_matrix":transform_matrix}
out["frames"].append(frame)
with open(out_file, "w") as f:
json.dump(out, f, indent=2)
if __name__ == "__main__":
dir_list = get_dir_list("test_data/txt")
#print(f"dir_list:\n\t{dir_list}")
write_to_json(dir_list, "test_data/txt/transforms.json", "png")
print("test finish")
The result after execution is shown in the figure below: