Skip to content

Commit

Permalink
Resize images when exporting to pmvs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
paulinus committed Nov 26, 2015
1 parent 1b295ae commit ffb2611
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions bin/export_pmvs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import json
import numpy as np

import opensfm.dataset as dataset
from opensfm import features
import opensfm.io as io
from opensfm.reconstruction import *


def opencv_calibration_matrix(width, height, focal):
'''Calibration matrix as used by OpenCV and PMVS
'''
f = focal * max(width, height)
return np.matrix([[f, 0, 0.5 * (width - 1)],
[0, f, 0.5 * (height - 1)],
[0, 0, 1.0]])


### Prepare OpenSfM output for dense reconstruction with PMVS

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

#### write camera projection matrix to txt/%08d.txt
#### radially undistort the original image
original_image = data.image_as_array(image)
camera = reconstruction['cameras'][shot['camera']]
original_h, original_w = original_image.shape[:2]
K = opencv_calibration_matrix(original_w, original_h, camera['focal'])
k1 = camera["k1"]
k2 = camera["k2"]
undistorted_image = cv2.undistort(original_image, K, np.array([k1, k2, 0, 0]))

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

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

new_txt = os.path.join(output_path, "txt", base + ".txt")
with open(new_txt, "w") as f:
f.write("CONTOUR\n")
for r in np.array(P):
for c in r:
f.write("%f " % c)
f.write("\n")

#### radially undistort image and copy to visualize/%08d.jpg
orig_image = os.path.abspath(data.image_files[image]) #why is dataset.__image_file() method private?
new_image = os.path.join(output_path, "visualize", base + ".jpg")

im = cv2.imread(orig_image)
k1 = camera["k1"]
k2 = camera["k2"]
im = cv2.undistort(im, K, np.array([k1, k2, 0, 0]))
cv2.imwrite(new_image, im)
np.savetxt(f, P, '%f')

fvis.close()

Expand Down

0 comments on commit ffb2611

Please sign in to comment.