Showing posts with label 3d reconstruction. Show all posts
Showing posts with label 3d reconstruction. Show all posts

Thursday, July 4, 2013

A simple ray tracer in Python - with CGKit and RTree

First of all I am over-reaching by calling this a ray-tracer, it is more appropriately called a "Splatter". I am simply projecting all the top-triangles in an OBJ model onto a raster for True Ortho generation purposes. It was a fun project for playing with Kd-trees and line-triangle intersections. The OBJ models are geo-referenced so it does not make sense to take them into a purely CG/Game oriented ray-tracer like Blender and lose the geographic context, as well as fiddle around with setting up orthographic cameras and render resolution to match ground-sampling distance. Doing it through a proper ray-tracer typically also requires setting up appropriate lighting, while I just want to sample the pre-exisiting texture in the model.
Direct vertical projection render from script
Properly lit render using Terragen

As usual a bit of googling located the python batteries that will perform the intersection as well as parse the obj mesh I have at hand. CGKit fits the bill perfectly both in terms of parsing as well as ray-triangle intersection.

However testing every triangle for intersection for every grid-cell quickly becomes horridly slow and begs for a hierarchial index to speed up the look-up and only process the triangles in the path of the ray. Here I picked up RTree to build a 3D index , a kD-Tree of Bounding volume hierarchy to speed up intersection testing.

After building the tree I went back to only testing relevant triangles in CGKit and dug through the parsed OBJ data structure to extract the texture co-ordinates of the intersected face. The barycentric description of the intersection point is sufficient to sample the texture for the right pixel colour and transfer it to the raster grid. For those interested in the implementation, the code follows, improvement suggestions are welcome. Multiprocessing is thrown in for tile-by-tile processing.

The point of the ray-tracer was to generate true-orthos for GIS use, as a side effect of implementing the ray tracer we can now generate true-orthos from both the top and the bottom of the model. The main article about true-orthos can be found here.

Friday, July 13, 2012

Lots of images to lots of OBJ to lots of images

Working with Structure from Motion and Dense matching can be a lot of fun. If the project get sufficiently big though, the reconstructed models is broken up into multiple Wavefrom Object files and can be difficult visualise and render. I like doing most of my rendering in Blender, and the handy Python API for loading OBJ files makes the job of getting all the files in easy.

To grab a bunch of obj files from a folder into Blender simply script this in the console:

import glob
objects = glob.glob("Filepath\*.obj")
for obj in objects:
    try:
        bpy.ops.import_scene.obj(filepath=obj, axis_forward='X', axis_up='Z')
    except:
        print("Failed "+obj)

Just like that you create a scene with 100's of object tiles. Make sure they are viewed in outline mode, so that the display is zippy while setting up lights, cameras and animation action. Start off the render and go get a beverage.


Sunday, May 27, 2012

Content supply for Apple's venture into mapping

This suspense is killing me. Hope it will last. - Oscar Wilde

Apple put up a big fight against the Big Blue to establish itself in the market of personal computers. Now it has to put up a similar brave face to the big blue, red, yellow and green.


Apple acquires C3, now needs oblique and ground based photography to populate the world. While keeping bandwidth usage low and discretional. Apple will be requiring lots of data to populate its more detailed world and there will be rush of data acquisition coming up to create a multiple view representation of the static world.

The most interesting aspect of 3D reconstruction I find is the temporal decorrelation. We can never know all aspects of an object from multiple perspectives simultaneously without violating relativity. A fun aspect of this is the modelling of clock-towers. The reconstructed view always has slightly different times in the clock faces.

Causality is much more absolute than simultaneity, in our frame of reference we might be experiencing events such as the launch of Apple mapping platform whose cause is hidden in some future outcome. Meanwhile the suspense of seeing the big launch from the big fruit in a post-Jobs era is killing me.