Image Segmentation with Machine Learning Made Easy in QGIS

The use of machine learning in GIS is increasingly transforming how we analyze and interpret spatial data, making complex tasks like image segmentation more accessible than ever. By leveraging the power of QGIS, an open-source geospatial platform, users can now integrate machine learning techniques to segment satellite imagery, aerial photos, or other raster data with remarkable ease. Whether you're a GIS professional mapping land cover or a researcher studying urban sprawl, this blog post will guide you through the process of using machine learning for image segmentation in QGIS.

In this post, I’d like to discuss how to do image segmentation with the ML4QGIS plugin, a powerful tool that simplifies the application of machine learning models directly within the QGIS environment, enabling users to classify and segment images without needing advanced programming skills.

Installing ML4QGIS Plugin

Let's start this tutorial by installing the ml4qgis plugin. The plugin can be downloaded at QGIS Plugin Repository as seen in the figure 1. You'll get a zip file which is called ml4qgis-xxx.zip.

Download ml4qgis Plugin
Figure 1. Download ml4qgis Plugin

Then open QGIS, go to Plugins > Manage and Install Plugins... The installation plugin window will appear as in figure 2. Select Install from ZIP. Browse the downloaded zip file and push Install Plugin button.

ML4QGIS Plugin Installation
Figure 2. ML4QGIS Plugin Installation

Machine Learning Training Points Preparation

To do the segmentation, we need a raster file and training points in a vector layer. In this tutorial I used a satellite imagery that was downloaded from Google Satellite Layer. Please check out this tutorial How to Save Google Map Satellite Imagery in QGIS.

ML4QGIS plugin works using Random Forest Algorithm. Random Forest is a robust and versatile machine learning method that excels in handling complex geospatial datasets. By training the algorithm on user-defined regions of interest, such as different land cover types, the plugin can efficiently classify pixels in an image, producing segmented outputs that clearly delineate features like forests, water bodies, etc.

To training the algorithm we must provide a set of data points with label. For that I created a point vector layer with two column fields namely label (numeric) and description (text). The attribute table of the points training file can be seen as in figure 3.

Training Point's Attribute Table
Figure 3. Training Point's Attribute Table

Take a reasonable training points for each class because 2/3 points will be used as training points and 1/3 as testing points for accuracy calculation. More points is better. In my case There were four classes of land cover: vegetation, water body, empty land and developed area with label 1, 2, 3 and 4. Figure 4 shows the satellite imagery and the training points.

Raster Data and Training Points
Figure 4. Raster Data and Training Points

Machine Learning Segmentation Process

Now it's time to do the segmentation process with machine learning using the ml4qgis plugin. The plugin can be accessed from processing toolbox. In the toolbox look for Machine Learning > Image Classification > Random Forest. See figure 5.

Random Forest Tool
Figure 5. Random Forest Tool

The plugin window will appear as in figure 6. In the respected field select Training data and Image to process. After pushing the Run button,  the plugin will run depends on how big the image is and also your machine specification. But In my opinion it works quite fast.

ML4QGIS Random Forest Plugin
Figure 6. ML4QGIS Random Forest Plugin

After finishing the segmentation process, the result will be added into your map in grayscale color. For better visualization you can customize the symbology color for the output layer as in seen in the figure 7.

Image Segmentation Result
Figure 7. Image Segmentation Result

Segmentation Result Accuracy

Accuracy is a measure how good is a result in this term how good is Machine Learning-Random Forest predict a land cover class for an image's pixel. The accuracy of the output can be seen in the Log tab as seen in the figure 8.

Image Segmentation Output Accuracy
Figure 8. Image Segmentation Output Accuracy
 

Take a look at the red box. It's written OA0.6, CFM.... It means the overall accuracy is 0.6 or 60% with confusion matrix (CFM) values: [[1 0 0 0], [0 8 0 1], [0 3 1 2], [0 4 0 5]]. Getting 60% accuracy is not good enough, it could happen mostly in unbalance training points for each class. So I should take more points to improve the accuracy. 

That's all this tutorial how to do image segmentation in QGIS with Machine Learning-Random Forest Algorithm using ml4qgis plugin. In this tutorial we learn how to download and install ml4qgis plugin, training points preparation, doing segmentation and access the segmentation result. I hope this tutorial is useful. Thank you!


Related Posts

Disqus Comments
-->