Automating free photogrammetry: Scripts I use

In a vain attempt to be useful, I want to share the script I’m currently using to automate my photogrammetry workflow.

I’m using COLMAP in conjunction with openMVS on Windows 10 at the moment, and I feel that gives me robust results, in a good amount of time.  You do need a decent computer with an Nvidia graphics card for this to work.

Essentially, I’ve automated my workflow so I don’t have to open up the COLMAP GUI each time, and then type in openMVS commands at the command prompt.  I’ve written the following script so that I can just take a folder full of photos, drop my batch file in there, double click it and come back a little later to a fully textured model (assuming it works without hiccup).

So, if you’d like to take advantage of this, the first thing you need to do is download both COLMAP (link) and the openMVS windows binaries (link). Oh, and install meshlab.

Let’s unzip the colmap and openMVS folders into our downloads folder, and install meshlab.

Create an empty text file with notepad, and paste in the following:

::These parameters are specific to computer 

::Store current Directory: 
set currDir=%CD% 

:: Set colmap directory:
set colDir=C:\Users\Peter\Downloads\COLMAP-dev-windows\bin

:: Set openMVS directory
set oMVS=C:\Users\Peter\Downloads\openMVS_sample-0.7a

:: Set Working Directory (windows)
set workDir=D:\Output

::Meshlab Directory:
set meshDir="C:\Program Files\VCG\MeshLab"

mkdir %workDir% 
copy *.jpg %workDir%\ 
cd /d %workDir%

%colDir%\feature_extractor --database_path database.db --image_path .
%colDir%\exhaustive_matcher --database_path database.db
mkdir sparse
%colDir%\mapper --database_path %workDir%\database.db --image_path . --export_path %workDir%\sparse
%colDir%\model_converter.exe --input_path sparse\0 --output_path model.nvm --output_type NVM
%oMVS%\InterfaceVisualSFM.exe model.nvm
%oMVS%\DensifyPointCloud.exe model.mvs
%oMVS%\ReconstructMesh.exe model_dense.mvs
%oMVS%\RefineMesh.exe model_dense_mesh.mvs
%oMVS%\TextureMesh.exe model_dense_mesh_refine.mvs

%meshDir%\meshlabserver.exe -i model_dense_mesh_refine_texture.ply -o model.obj -m wt

mkdir %currDir%\model\
copy *.obj %currDir%\model\
copy *.mtl %currDir%\model\
copy *.png %currDir%\model\

::Uncomment these lines in to delete working directory
::cd /d %currDir%
::RD /S /Q %workDir%

Alter the script accordingly (see below) then change the extension of the file from *.txt to *.bat.

What this script does, is copy all the images in the same directory as the script into a working directory, run the COLMAP commands there, then the openMVS commands, then uses meshlab to convert the ply to obj.  Let me talk you through it, because there’s some bits you’ll need to change according to your setup.

Variables

::These parameters are specific to computer

::Store current Directory:
set currDir=%CD%

:: Set colmap directory:
set colDir=C:\Users\Peter\Downloads\COLMAP-dev-windows\bin

:: Set openMVS directory
set oMVS=C:\Users\Peter\Downloads\openMVS_sample-0.7a

:: Set Working Directory (windows)
set workDir=D:\Output

::Meshlab Directory:
set meshDir=”C:\Program Files\VCG\MeshLab”

mkdir %workDir%
copy *.jpg %workDir%\
cd /d %workDir%

This sets the directories for the colmap and openmvs binaries so that I didn’t have to type them out multiple times in the script, and so it’s easier to change depending on which computer it’s running on.  If you’ve left the two folders in your Downloads folder, you can probably just change the username from ‘Peter’ to whatever your username is.

I also put in the default install directory for Meshlab, as we’ll be using the meshlabserver.exe in there later on.

I’ve set my working directory (%workDir%) to D:\Output.  The script will run everything from there so that a) I always know where intermediate files will be saved, b) by copying the current images, it reduces the chance of accidentally deleting something vital, and c) I keep my image sets in onedrive; by moving the working directory outside onedrive, I make sure that it’s not trying to upload all the intermediate steps.

Run COLMAP

%colDir%\feature_extractor –database_path database.db –image_path .
%colDir%\exhaustive_matcher –database_path database.db
mkdir sparse
%colDir%\mapper –database_path %workDir%\database.db –image_path . –export_path %workDir%\sparse
%colDir%\model_converter.exe –input_path sparse\0 –output_path model.nvm –output_type NVM

The next few lines work through the colmap executables in order to: Extract features, match features, create depth mapps, and save the matched cameras as an NVM file (VisualSFM file format, needed by openMVS below).  The instructions for this were taken from the COLMAP executables documentation.

Run openMVS

%oMVS%\InterfaceVisualSFM.exe model.nvm
%oMVS%\DensifyPointCloud.exe model.mvs
%oMVS%\ReconstructMesh.exe model_dense.mvs
%oMVS%\RefineMesh.exe model_dense_mesh.mvs
%oMVS%\TextureMesh.exe model_dense_mesh_refine.mvs

This runs, in turn, various commands to convert the nvm file to openMVS, create a dense point cloud, then reconstruct, refine, and texture the mesh.  On lower memory systems, it tends to be the refine mesh stage that really brings a computer to it’s knees, and if that’s happening you might want to look into the openMVS documentation to see how to scale down images for this step.

Convert to OBJ, and move the model into the original directory.

%meshDir%\meshlabserver.exe -i model_dense_mesh_refine_texture.ply -o model.obj -m wt

mkdir %currDir%\model\
copy *.obj %currDir%\model\
copy *.mtl %currDir%\model\
copy *.png %currDir%\model\

The first line here just gives meshlab the ply file, and asks it to output the same model as an OBJ.  I do this because I work in Autodesk Maya frequently, and it doesn’t have ply support.

The script then makes a directory ‘model’ in the original images directory, and moves the obj, material, and texture files there.

Clean up

The last few lines are optional:

::Uncomment these lines in to delete working directory
::cd /d %currDir%
::RD /S /Q %workDir%

But these will delete the working directory automatically.  It’s actually better to do this manually, because if something fails half way through, these commands will delete all those intermediate files and you won’t be able to see where it failed.

Have fun!

I hope this is useful to people that have a lot of photogrammetry to do, and don’t want to manually run every stage for every dataset.  I offer this of course with absolutely zero support!  If it doesn’t work or makes your computer blow up, not my fault!

Advertisements

2 thoughts on “Automating free photogrammetry: Scripts I use

  1. Nicely explained, I wrote a script to automatise openMSV workflow as well but ended up having to control each step on the way. My laptop is quite limited and I have to crop the mesh before refine otherwise it takes ages!
    But I really understand and approve the process!

    You might try to export directly the obj from openMSV without going through meshlab by adding the option “–export-type obj” at the Texturemesh.
    I don’t know if there is a difference with meshlab but it works fine for me.

    Next step: automating picture acquisition!

    1. Ah, you’re absolutely right – I found openMVS could output straight to obj a couple of weeks back and edited my own script accordingly. I’ll put up a new post soon summarizing everything and including it.

      Great idea cropping before refining – I’ve a couple of models even my workstation falls over with.

      Yes… I’ve been looking at automated turntables, but can’t seem to find the time (or a good connection to a sony nex-6) to set it up. If you find a good guide, let us know!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s