
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.
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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!