Radially undistort images before odm_texturing

Former-commit-id: 109eb800f1
pull/1161/head
Pau Gargallo 2016-03-03 12:58:25 +01:00
rodzic a74789e5ad
commit d8b0dc7376
4 zmienionych plików z 83 dodań i 2 usunięć

Wyświetl plik

@ -0,0 +1,53 @@
#!/usr/bin/env python
import argparse
import os
import cv2
import numpy as np
import opensfm.dataset as dataset
import opensfm.io as io
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]])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Undistort images')
parser.add_argument('dataset', help='path to the dataset to be processed')
parser.add_argument('--output', help='output folder for the undistorted images')
args = parser.parse_args()
data = dataset.DataSet(args.dataset)
if args.output:
output_path = args.output
else:
output_path = os.path.join(data.data_path, 'undistorted')
print "Undistorting images from dataset [%s] to dir [%s]" % (data.data_path, output_path)
io.mkdir_p(output_path)
reconstructions = data.load_reconstruction()
for h, reconstruction in enumerate(reconstructions):
print "undistorting reconstruction", h
for image in reconstruction['shots']:
print "undistorting image", image
shot = reconstruction["shots"][image]
original_image = data.image_as_array(image)[:,:,::-1]
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]))
new_image_path = os.path.join(output_path, image.split('/')[-1])
cv2.imwrite(new_image_path, undistorted_image)

Wyświetl plik

@ -300,6 +300,8 @@ class ODM_Tree(object):
self.odm_meshing_log = io.join_paths(self.odm_meshing, 'odm_meshing_log.txt')
# odm_texturing
self.odm_texturing_undistorted_image_path = io.join_paths(
self.odm_texturing, 'undistorted')
self.odm_textured_model_obj = io.join_paths(
self.odm_texturing, 'odm_textured_model.obj')
self.odm_textured_model_mtl = io.join_paths(

Wyświetl plik

@ -79,6 +79,8 @@ class ODMSlamCell(ecto.Cell):
map_points,
slam_config,
]))
# link opensfm images to resized images
os.symlink(tree.opensfm + '/images', tree.dataset_resize)
else:
log.ODM_WARNING('Found a valid OpenSfM file in: {}'.format(
tree.opensfm_reconstruction))

Wyświetl plik

@ -1,3 +1,5 @@
import os
import ecto
from opendm import log
@ -35,6 +37,28 @@ class ODMTexturingCell(ecto.Cell):
rerun_cell = args['rerun'] is not None \
and args['rerun'] == 'odm_texturing'
# Undistort radial distortion
if not os.path.isdir(tree.odm_texturing_undistorted_image_path) or rerun_cell:
system.run(' '.join([
'cd {} &&'.format(tree.opensfm),
'PYTHONPATH={}:{}'.format(context.pyopencv_path,
context.opensfm_path),
'python',
os.path.join(context.odm_modules_src_path,
'odm_slam/src/undistort_radial.py'),
'--output',
tree.odm_texturing_undistorted_image_path,
tree.opensfm,
]))
system.run(
'PYTHONPATH=%s %s/bin/export_bundler %s' %
(context.pyopencv_path, context.opensfm_path, tree.opensfm))
else:
log.ODM_WARNING(
'Found a valid Bundler file in: %s' %
(tree.opensfm_reconstruction))
if not io.file_exists(tree.odm_textured_model_obj) or rerun_cell:
log.ODM_DEBUG('Writting ODM Textured file in: %s' \
% tree.odm_textured_model_obj)
@ -44,7 +68,7 @@ class ODMTexturingCell(ecto.Cell):
'bin': context.odm_modules_path,
'out_dir': tree.odm_texturing,
'bundle': tree.opensfm_bundle,
'imgs_path': tree.dataset_resize,
'imgs_path': tree.odm_texturing_undistorted_image_path,
'imgs_list': tree.opensfm_bundle_list,
'model': tree.odm_mesh,
'log': tree.odm_texuring_log,
@ -64,4 +88,4 @@ class ODMTexturingCell(ecto.Cell):
% tree.odm_textured_model_obj)
log.ODM_INFO('Running OMD Texturing Cell - Finished')
return ecto.OK if args['end_with'] != 'odm_texturing' else ecto.QUIT
return ecto.OK if args['end_with'] != 'odm_texturing' else ecto.QUIT