Raster to Vector - Centerline Extraction

From fmepedia

Check this demo updated in 2010

At FME User Conference in 2008 I was asked by one of the users whether we can convert raster to vector. Just before the UC I made a small example showing how to convert the Safe logo in PNG to 3D PDF, so I could show a simple workspace performing this kind of operations. I must note though that the example deals with very simple case - separate raster areas with no gradients or adjacent areas with different colors. So, I decided to try something more realistic and useful. How about centerline extraction?

Before continuing I should mention that we don't have a true Vectorizer at this point. The only transformer we can use is RasterToPointCoercer, which simply converts raster cells to vector points.

I also should mention that I am quite skeptical about automated raster to vector translations, especially if we talk about photos or scanned maps. But there are some raster types that can be quite good (or rather not all too bad), for example, rasters generated within GIS packages from vector data. If the vector source is not available anymore, we can try vectorizing the raster.

I took a Google Maps image, because it is a good and widely available example of an online raster map:

The first step was easy - convert raster to a set of points with RasterToPointCoercer. The points we get don't know much about their parent cells, so I used PointOnRasterValueExtractor that transfers band values representing in this case RGB color from cells to points in a form of a list attributes band{}.value.

The next step takes those list attributes and converts them to attributes _red, _green, and _blue.

Now comes the most interesting step - selecting only cells that make roads - yellow for arterial routes, and white for the local roads. Simple Tester could do the job easily if the colors were constant, but there are two obstacles - road labels and anti-aliasing used on labels and arterial routes - there are a few transition pixels around labels and at the road intersections with different RGB values:

Road points (grayscale) can be selected by analyzing where R, G and B values are quite close (e.g, 156,156,158 or 254, 254, 253), but it's hard to do with the Tester transformer - we don't know all possible values. I used ExpressionEvaluator with the following formula:

(abs(@Value(_red)-@Value(_green))<=3?1:0)&&(abs(@Value(_red)-@Value(_blue))<=3)?1:0

It assigns 1 only to points where green and blue values are quite close to red:

red - 3 <= green <= red + 3 
red - 3 <=  blue <= red + 3 

The similar formula is used for selecting yellow colors:

(abs(@Value(_red)-@Value(_green))<=4?1:0)&&(abs(@Value(_red)-@Value(_blue))>20)?1:0

It assings 1 only to points where green is quite close to red, and blue is significantly less than red (and green).

This step will leave us only roads and a few black (grayscale) labels outside of the roads.

Knowing the original cell size (in my example it's 1 by 1 unit), we can replace points with squares:

The rest of the process is simple - Dissolver creates a continuous coverage. DonutHoleExtractor with AreaCalculator and Tester help get rid of small colorful artifacts within roads and from some labels, AreaSmoother and AreaGeneralizer eliminate zig-zagy look and simplify the road polygons, CenterLineReplacer makes centerlines, and LineGeneralizer simplifies them:

Further cleaning can be done with MRFCleaner, a separate extra-cost plug-in. It can help get rid of some short hanging endpoints and make intersections look better.

How good are these results (MRFCleaned results)? Well, they are definitely far from being perfect, but I think they are quite acceptable as a starting point for further editing, which definitely will take much less time thaт fully manual digitizing.

This technique can be used for extracting other areas based on their color. Text features seem to be the main problem for a nice smooth vectorization so they should be turned off whenever possible. The workspace may need tuning for extracting other colors, and the post-selection/dissolving part can be totally different.

You need to download the workspace and the raster to try the demo.

Attached Files
filesizedate
CenterlinesGeneralized.ffs2.6 kB03/18/08
MRFCleaned.ffs2.7 kB03/18/08
centerlineextractor3.fmw161.1 kB03/18/08
raster2.png201.9 kB03/18/08
raster3.png77.0 kB03/18/08
raster4.png7.2 kB03/18/08
raster5.png12.0 kB03/18/08
raster6.png78.0 kB03/18/08
User Comments Add a new comment