构建相机参数文件
通过reconstruction.json来获取相机内外参数:
了解其中的元素:
"cameras": {
"v2 dji fc6310r 5472 3648 brown 0.6666": {
"projection_type": "brown",
"width": 5472,
"height": 3648,
"focal_x": 0.6705854906080082,
"focal_y": 0.6705854906080082,
"c_x": -0.0008743229461124625,
"c_y": -0.007920054330814687,
"k1": -0.009704124772487914,
"k2": -0.0007112073497550784,
"p1": -0.0036577168962138615,
"p2": -0.00040388999688357444,
"k3": 0.008826904511977495
}
},
"shots": {
"100_0039_0112.JPG": {
"rotation": [
3.1344387599198793,
-0.057623164437503364,
0.019009945955184574
],
"translation": [
-294.3844983459479,
-124.77306157157436,
2266.6169629250944
],
"camera": "v2 dji fc6310r 5472 3648 brown 0.6666",
"orientation": 1,
"capture_time": 1723038534.0,
"gps_dop": 10.0,
"gps_position": [
264.2819588735218,
-145.36810024470105,
2269.213865772821
],
"vertices": [],
"faces": [],
"scale": 1.0,
"covariance": [],
"merge_cc": 0
},
json中将数据以字典形式存储,其中:
cameras:width为图像宽度,height为图像高度,focal_x为横向焦距,focal_y为纵向焦距,c_x为视图横坐标中心,c_y为视图纵坐标中心,k1,k2,k3以及p1,p2为相机畸变矫正
shots中存储了SFM构建的连接图中照片的位姿:
"100_0039_0112.JPG"为照片标号,rotation为俯仰角,translation为三个坐标系上的平移
通过如下代码进行reconstruction.json的文件转存为camera的txt文件
import json
import os
import cv2
import numpy as np
def rotvector2rot(rotvector):
Rm = cv2.Rodrigues(rotvector)[0]
return Rm
# 打开并读取JSON文件
with open('D:/project/project1/opensfm/reconstruction.json', 'r') as file:
data = json.load(file)
# 访问并打印数据
# print(data["shots"]) # 输出: John Doe
# print(data[0]["shots"]['100_0039_0112.JPG'])
img_path = "D:/project/project1/images/"
save_path = "D:/project/MVSNET/scan1/cams/"
count = 0
# intrinsic = data[0]["cameras"]['v2 dji fc6310r 5472 3648 brown 0.6666']['focal_x']
# print(intrinsic)
for root, dirs, files in os.walk(img_path, topdown=False):
# print(files) # 当前目录路径
file_count = 0
for file in files:
try:
# print(data[0]["shots"][file]['rotation'])
# print(data[0]["shots"][file]['translation'])
rota = np.array(data[0]["shots"][file]['rotation'])
tran = np.array([data[0]["shots"][file]['translation']]).T
zero_ = np.array([[0, 0, 0, 1]])
# print(rotvector2rot(rota))
# print(tran)
save_np = np.vstack((np.hstack((rotvector2rot(rota), tran)), zero_))
txt_name = "00000000"
txt_num = str(file_count)
txt_name = save_path + txt_name[0:len(txt_name) - len(txt_num)] + txt_num + "_cam.txt"
file_count += 1
# print(txt_name)
with open(txt_name, 'w', encoding='utf-8') as f:
f.write("extrinsic" + '\n')
for i in range(4):
for j in range(4):
f.write(str(save_np[i][j]) + ' ')
f.write('\n')
f.write('\n' + 'intrinsic' + '\n')
f.write(str(data[0]["cameras"]['v2 dji fc6310r 5472 3648 brown 0.6666']['focal_x']) + ' ')
f.write('0' + ' ')
f.write(str(data[0]["cameras"]['v2 dji fc6310r 5472 3648 brown 0.6666']['c_x']) + '\n')
f.write('0' + ' ')
f.write(str(data[0]["cameras"]['v2 dji fc6310r 5472 3648 brown 0.6666']['focal_y']) + ' ')
f.write(str(data[0]["cameras"]['v2 dji fc6310r 5472 3648 brown 0.6666']['c_y']) + '\n')
f.write('0' + ' ')
f.write('0' + ' ')
f.write('1' + ' ')
f.write('\n')
f.write('\n' + '425 2.5')
count += 1
except:
continue
print(count)
构建pair.txt
通过如下代码进行pair构建,大多数的DTU格式数据集中相似度不关键,故设为1000
import os
import cv2
import numpy as np
img_path = "D:/project/project1/images/"
for root, dirs, files in os.walk(img_path, topdown=False):
# print(files) # 当前目录路径
file_count = 1
txt_name = "D:/project/pair.txt"
with open(txt_name, 'w', encoding='utf-8') as f:
f.write(str(len(files)) + '\n')
for i in range(len(files) - 35):
f.write(str(i) + '\n')
f.write("10" + ' ')
file_source = files[i]
path_real = img_path + file_source
key_value = {}
for j in range(i + 1, i + 11):
f.write(str(j) + ' 1000 ')
f.write('\n')