A guide to mapping with the help of image segmentation
Posted by blkatbyhh on 26 November 2022 in English. Last updated on 27 November 2022.This guide is a translation of my previous diary written in Chinese. The motivation for developing this method is trying to find a way to map vast areas of forests with minimum human effort.
Methodology
The OpenStreetMap data is based on nodes and vector ways. Manual mapping requires a mapper to put nodes on the satellite pictures and connect nodes by mouse clicking, which is a time-consuming process. This guide proposes a software-assisted workflow to ease the mapping process, it includes the following steps.
- Raster image acquiring and segmentation
- Raster image preparation
- Vectorization
- Mapping to OSM
Raster image acquiring and segmentation
Software used in this step:
- JOSM
- Fiji (Fiji Is Just ImageJ)
Install the JOSM plugin importvec and restart JOSM, Download the map data of the region of interest (ROI), choose a satellite source and download the image. Hide the data layer and take a snapshot of the satellite image on (ROI). Save the snapshot as JPG or PNG file.
Open the snapshot image in Fiji. Turn to the menu: Plugins, Segmentation, Trainable Weka Segmentation. A new Weka window will pop up containing the image. On the right side of the window, there are two preset classes 1 and 2 representing features to be recognized in the image. Create more if needed with create new class button on the left side, and in settings button, you can rename the preset class 1 and 2 to meaningful names to avoid mistakes made by yourself.
Now you can use your mouse left click and hold to draw a curve on the image and click “add to class” button to tag the feature and repeat to create several items. Double-click an item to remove it if you tag it wrong. You don’t have to precisely draw along the edge of the feature, just draw a random curve inside it. When you have collected several items, you can click train classifier in the top-left corner to, well, train the classifier.
When training is finished, the image will be covered with different colors, each representing a feature. You can click toggle overlay to check if the classifier works well to identify the image features. At this step, you can refine the classifier by tagging the wrongly recognized area and train the classifier again until the result satisfy you. You don’t have to push the classifier to perfect because a precise vector requires unlimited control points but JOSM limits 2000 nodes to a way vector, there will be a simplification process to reduce the number of nodes. Now you can click create result and have a colored image.
Raster image preparation
Software used in this step:
- Fiji (Fiji Is Just ImageJ)
Now you have a classified (not that kind of classified :D) result image, each pixel of the image has a value of 0, 1, or 2, etc. representing its tag. The next step is to pick the area you want to map and remove the others. Go to the menu Image, Adjust, Threshold, in the popped-up dialog window, there are two slide bars, use them to pinpoint the target value, say 0 representing the forest. Click Apply and you will see your colored result image turn to a black-and-white image, and only the forest area is left.
This image usually contains a lot of tiny dots you may want to remove. you can use Process, Binary, Erode several times to remove them and then apply Process, Binary, Dilate several times to recover the edge lost of the survived area. Don’t over-use this as it will turn curves zig-zagged. Make peace with the errors introduced in this process as we are trying our best to minimize human efforts.
And also feel free to use Process, Binary, Fill holes to remove the hollow area in the ROI area, this will save our time in the mapping process by avoiding creating a lot of multipolygon relationships. You can add inner features back with another classified result.
You may also want to remove those areas cut by the edge of the snapshot by clicking Flood Fill Tool button on the Fiji. If the edge-cut areas are connected with the main ROI area, use Process, Binary, Watershed to cut all the area into small pieces, after removing the unwanted area, Process, Binary, Close~ once and all the pieces will be connected again.
Now you can save your image as PNG file, and it is ready for vectorization.
Vectorization
Software used in this step:
- Inkscape
Import the PNG file to Inkscape, use Path, Trace Bitmap function. Choose the default Brightness cutoff method for Detection Mode, and click apply. Now you will have an area-filled vector image, no more process is needed. Save it as an SVG file.
Mapping to OSM
Software used in this step:
- JOSM
- JOSM plugin importvec
Now go back to JOSM, and create a new layer. Believe me, this new layer is crucial because you don’t want to mix the imported multiple areas with the existing map. Open the SVG file to the new layer (If you didn’t install the importvec plugin before you take the snapshot, you will have to find your ROI back). A dialog window will ask you about 1 unit equals how many meters. I don’t know how to calculate (maybe I need to RTFM), just accept the default and use ctrl+alt with mouse left click and drag to scale the imported ways to match it with the satellite image. Then choose the ways containing over 2000 nodes and use Tool, Simplify Way to reduce node numbers. Make adjust until it satisfies you. And don’t forget to jump between different data layers to check if the imported ways are compactable with the existing data, then Merge the layers and final validate and check for errors.
Now you can upload your work and shift-ctrl-r the webpage and enjoy.
PostScript
Fiji and Inkscape are all open-source software,s and their manuals are well-written. Especially for Fiji, you don’t have to worry about its strange old fashion UI as it provides tons of manuals and examples.
This guide is mainly focused on area mapping. But the toolchain introduced here may have the potential to help draw highways, by applying Centerline tracing (autotrace) method in the Inkscape. Anyone who tried this is welcome to share their experiences.
Because sharing is good.