OpenStreetMap

blkatbyhh's Diary

Recent diary entries

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.

  1. Raster image acquiring and segmentation
  2. Raster image preparation
  3. Vectorization
  4. 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.

使用图像识别法进行半自动开荒

Posted by blkatbyhh on 22 November 2022 in Chinese (China) (‪中文(中国大陆)‬). Last updated on 27 November 2022.

前言

虽然Openstreetmap是不面向渲染绘图,但日常看到地图上欧洲一片绿意盎然而东亚地区即使在胡焕庸线以东也是一片空白,实在是有碍观瞻。本地已有的大片自然地貌有国际友人协助开荒,但毕竟不是长久之计。然则大陆地区绘图者寥寥,很难做到大面积开荒,如能使用一些半自动化工具或可以加快开荒速度。故此摸索了一套工具链,希望能有所帮助。

方法

Openstreetmap的数据是矢量路径,手动绘制的过程是以卫星和航拍图为底图进行路径描摹后修整,这一过程最费时费力的是照片判读和路径描摹。本文提出的方法就是尝试用自动化/半自动化的方式减轻这个过程的劳动强度,缩短工时。本方法包括以下几个流程

  1. 图像获取和自动化识别
  2. 位图预处理
  3. 矢量化
  4. 绘图

图像获取和自动化识别

使用的工具

  • JOSM

  • Fiji (Fiji Is Just ImageJ)

JOSM使用方法不赘述。选择合适的卫星图做为底图,隐藏数据层后截图获得位图(raster image)保存。

在Fiji中打开图片,使用Fiji提供的plugin, Segmentation, Trainable Weka Segmentation工具进行图像识别。启动该工具后,先按需建立分类(class),预设有2个分类class 1和class 2。如图像中包含树林,河流,村庄和农田,则需新建两个分类,再去setting里将class 1, 2更名(不更名也没关系,但有明确含义的名字可以避免自己犯错)。用鼠标按住左键画任意曲线的方式在卫星图中指示特征后,点击右侧分类的按钮将曲线加入该类的列表,如有分类失误可以双击列表项移除。指示特征不必精确画出边界,在特征范围内随意标注既可。每个分类都添加若干条目后,点击左上的train classifier进行分类器训练。计算结束后会在图片上叠加用不同颜色标识的分类结果,在这个阶段可以评估分类器有效性,对分类识别失败的地方进行修正标注后重新训练分类器,直到结果满意,在这个阶段不必追求严格精准,因为精准的曲线需要大量控制点描述,而JOSM限制单一路径控制点不能超过2000个,后序的矢量化处理也会精简掉大量控制点。在软件界面点击create result获取结果图片。

位图预处理

使用的工具

  • Fiji (Fiji Is Just ImageJ)

使用Weka Segmentation获得的结果图片包含多个分类,以上述四个分类为例,各个分类的像素点赋值为0, 1, 2和3,下一步是选出需要的分类。使用Fiji的内嵌功能Image, Adjust, Threshold,在弹出的对话框中,滑动上下两个阈值滑块,选出分类的像素点赋值,同时图片中会显示出当前阈值选区。选好后点击Apply应用阈值选择结果。

通常在此步获得的图片还有大量孤立小像素点,本着抓大放小的原则可以去除掉。通过执行N次Process, Binary, Erode功能腐蚀掉所有图形的边界吃掉小点,再执行N次Process, Binary, Dilate将存活的图形膨胀回来。此时得到的边界当然和最初识别的边界存在差异,但前述已说过,只要偏差不是太离谱,误差都应该在可控范围内。另外建议使用Binary下的fill holes功能避免区域嵌套,导入JOSM以后还得处理复合多边形,徒增工作量。如果不想保留贴边的区域,可以用Flood Fill Tool把黑区填充成白色,如果边缘部分和想保留的部分有连接,可以使用Process, Binary, Watershed功能把图形分割成小块再填充掉不要的部分,用Process, Binary, Close~功能把Watershed的切分拼回去。此时已完成位图的预处理,File, Save as PNG保存后就可以进入下一步矢量化了。

矢量化

使用的工具

  • inkscape

将预处理后的位图导入inkscape,使用Path, Trace Bitmap功能,Detection Mode选默认的Brightness cutoff,选中位图后Apply获得矢量路径。矢量路径是叠加在位图上的,拖动一下就可以看到结果。路径内部是填充的,可以在Object, Fill and Stroke功能里取消填充,绘制路径,得到图形干净的边界线。然后保存为svg格式的矢量图就可以了。

绘图

使用的工具

  • JOSM

  • JOSM插件importvec

安装importvec后,JOSM可以打开svg矢量图,导入svg后会问你比例,随便吧,反正也不知道具体是多少,需要缩放调整的。

在JOSM里File, New Layer新建一个图层,导入svg,就可以看到灰色的矢量路径了,全选,按住键盘上的ctrl+alt键,按住鼠标左键缩放路径,平移,缩放路径,平移,重复多次后将路径和卫星图对齐。删除掉错误的区域或者不想要的线段,区域等路径,全选路径validate一下检查是否有重叠点错误,有的话fix一下,忽略所有warning(笑)。最后目视一圈,满意了以后Tool, Simplify Way减少node数量,再进行最终手动调整,增删node,设置类型。完成以后将这个新图层merge到下载的数据图层里,进行最后的区域合并和边界优化等兼容性调整。大功告成可以上传了。

后记

关于软件使用有不清楚的地方请参照各软件的帮助文档,特别是Fiji的文档极为详尽,不用担心看不懂。

以上工序是进行二维区域识别,在地形不复杂的地方应该也可以用来识别道路这种一维线特征,工序中的区别是在inkscape的矢量化中使用Centerline tracing(autotrace)方法获得道路的中心线。目前我没有试验,当作业布置给有兴趣的各位。

更新

  • 位图预处理部分增加Fill holes步骤。
  • 位图预处理部分增加边缘不完整图形的去除步骤。