This is the 3rd part in a series on interactive image segmentation. In part 1 I looked at how thresholding an image of coins has the potential to help map circular fields in the desert. In part 2 I applied this watershed algorithm to satellite data and created an output shapefile. In part 3 I am going to tie all the code together from part 2 and automate.
http://www.acgeospatial.co.uk/blog/interactive-segmentation-1/
http://www.acgeospatial.co.uk/blog/interactive-segmentation-2/
Automation
When I think back to my first experiences with GIS I was basically automating from day 1. Command line driven AML was pretty much my first experience of GIS software. We would take data, manipulate it and then use ArcPlot to see the results. I think from memory I could just store AML in an Ascii file and run or copy and paste into the command prompt.
Automating mapping of circular fields
One of the beautiful things about automating processes is that it allows a review of a chain of methods/steps that have been used in prototyping. Often automation provides a fresh perspective on how best to run a process.
I am going to assume that you have a satellite image and its clipped to the area you are working in (step 1 from part 2). Step 2 was to use GDAL; there is a super simple way of calling GDAL in Python (windows OS at least) and that is using subprocess.
import subprocess
filein = ".../out_clip.tif"
fileout = ".../out_image1.jpg"
subprocess.call('gdal_translate -b 1 -b 2 -b 3 -of JPEG -scale -co worldfile=yes ' + filein + " " + fileout)
This is a neat way of calling GDAL within your Python script. The advantage being just one line, rather than tens of lines. It should be noted that this will not work on Linux based OS. I also call gdal_polygonize this way later in the script.
All the code is on my github page, have a look.
Why automate?
The image on the right is the Landsat 8 image from April, on the left is the Bing maps data (the Google Satellite data does far much better). This data is from the OpenLayers plugin in QGIS. Why automate? Because this area is going to change and because the code runs in only a few seconds.
What else?
Once automated, the code can be run on other images in other locations. The projection will need changing to fit the location (EPSG code in the script).
For example I also looked at some cloud free imagery from Sentinel 2a over fields in mid western US.
Thresholded image
Fields
Some cleaning up needs to be done, but no doubt the combination of OpenCV with the watershed algorithm and GDAL to add the ‘geospatial’ information is an excellent first pass at mapping. Getting the input data in the right condition for processing is so critical, in any image processing or any data analysis.
I hope this 3 part blog series has proved useful; it’s been fun bringing it all together.