@@ -8,10 +8,19 @@ import json
8
8
import numpy as np
9
9
10
10
import opensfm .dataset as dataset
11
+ from opensfm import features
11
12
import opensfm .io as io
12
13
from opensfm .reconstruction import *
13
14
14
15
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
+
15
24
16
25
### Prepare OpenSfM output for dense reconstruction with PMVS
17
26
@@ -68,35 +77,30 @@ if __name__ == "__main__":
68
77
fvis .write ("%d " % ai )
69
78
fvis .write ("\n " )
70
79
71
- #### write camera projection matrix to txt/%08d.txt
80
+ #### radially undistort the original image
81
+ original_image = data .image_as_array (image )
72
82
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 ]))
73
88
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 )
77
93
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' ])
79
97
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
82
99
83
100
new_txt = os .path .join (output_path , "txt" , base + ".txt" )
84
101
with open (new_txt , "w" ) as f :
85
102
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' )
100
104
101
105
fvis .close ()
102
106
0 commit comments