Skip to content

Commit ffb2611

Browse files
committed
Resize images when exporting to pmvs
We extract features from images of size `feature_process_size`. PMVS should use images of the same size or smaller. Calibration won't be precise enough otherwise.
1 parent 1b295ae commit ffb2611

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

bin/export_pmvs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ import json
88
import numpy as np
99

1010
import opensfm.dataset as dataset
11+
from opensfm import features
1112
import opensfm.io as io
1213
from opensfm.reconstruction import *
1314

1415

16+
def opencv_calibration_matrix(width, height, focal):
17+
'''Calibration matrix as used by OpenCV and PMVS
18+
'''
19+
f = focal * max(width, height)
20+
return np.matrix([[f, 0, 0.5 * (width - 1)],
21+
[0, f, 0.5 * (height - 1)],
22+
[0, 0, 1.0]])
23+
1524

1625
### Prepare OpenSfM output for dense reconstruction with PMVS
1726

@@ -68,35 +77,30 @@ if __name__ == "__main__":
6877
fvis.write("%d " % ai)
6978
fvis.write("\n")
7079

71-
#### write camera projection matrix to txt/%08d.txt
80+
#### radially undistort the original image
81+
original_image = data.image_as_array(image)
7282
camera = reconstruction['cameras'][shot['camera']]
83+
original_h, original_w = original_image.shape[:2]
84+
K = opencv_calibration_matrix(original_w, original_h, camera['focal'])
85+
k1 = camera["k1"]
86+
k2 = camera["k2"]
87+
undistorted_image = cv2.undistort(original_image, K, np.array([k1, k2, 0, 0]))
7388

74-
w = camera["width"]
75-
h = camera["height"]
76-
f = camera["focal"] * max(w, h)
89+
#### resize and save the undistorted to visualize/%08d.jpg
90+
resized_image = features.resized_image(undistorted_image, data.config)
91+
new_image_path = os.path.join(output_path, "visualize", base + ".jpg")
92+
cv2.imwrite(new_image_path, resized_image)
7793

78-
# build that projection matrix that PMVS actually accepts!
94+
#### write camera projection matrix to txt/%08d.txt
95+
resized_h, resized_w = resized_image.shape[:2]
96+
resized_K = opencv_calibration_matrix(resized_w, resized_h, camera['focal'])
7997
Rt = Rt_from_shot(shot)
80-
K = np.matrix([[f, 0, 0.5 * (w - 1)], [0, f, 0.5 * (h - 1)], [0, 0, 1.0]])
81-
P = K*Rt
98+
P = resized_K * Rt
8299

83100
new_txt = os.path.join(output_path, "txt", base + ".txt")
84101
with open(new_txt, "w") as f:
85102
f.write("CONTOUR\n")
86-
for r in np.array(P):
87-
for c in r:
88-
f.write("%f " % c)
89-
f.write("\n")
90-
91-
#### radially undistort image and copy to visualize/%08d.jpg
92-
orig_image = os.path.abspath(data.image_files[image]) #why is dataset.__image_file() method private?
93-
new_image = os.path.join(output_path, "visualize", base + ".jpg")
94-
95-
im = cv2.imread(orig_image)
96-
k1 = camera["k1"]
97-
k2 = camera["k2"]
98-
im = cv2.undistort(im, K, np.array([k1, k2, 0, 0]))
99-
cv2.imwrite(new_image, im)
103+
np.savetxt(f, P, '%f')
100104

101105
fvis.close()
102106

0 commit comments

Comments
 (0)