Skip to content
← Back to Remote Sensing
remote-sensingIntermediate· 14 min read

Image Classification Methods: K-means, Maximum Likelihood, Random Forest and Accuracy Assessment

How a satellite image becomes a land use and land cover map: the unsupervised and supervised methods compared, what training samples need, and how to measure accuracy with a confusion matrix.

What a classifier does

Every pixel in a multiband image is a list of numbers, one per band. A Landsat 8 pixel with six bands is a point in a six-dimensional space, which is often called the feature space. Pixels of the same surface land close together in that space, and pixels of different surfaces land apart. A classifier draws boundaries in the feature space and gives each pixel the label of the region it falls in.

Two things decide the quality of the result before any algorithm is chosen: the classes you ask for, and the numbers you give the algorithm to separate them. The method matters less than most people expect.

Choosing the classes

A land use and land cover scheme is a list of classes. A usable scheme follows three rules.

  • The classes do not overlap, so a pixel belongs to one class only.
  • The classes cover the whole area, so every pixel has a class. A class such as "other" or "open land" takes the leftovers.
  • The classes can be told apart in the imagery you have. "Residential" and "commercial" are land uses that look alike from space. "Built-up" is a land cover that does not.

Published schemes give a starting point. The Anderson scheme from the US Geological Survey has levels from broad to detailed. The National Remote Sensing Centre scheme is used for national land use and land cover mapping in India. CORINE is used in Europe, and ESA WorldCover uses eleven classes at 10 m for the whole world. A tutorial with one scene usually needs four to eight classes, because every added class needs training samples and brings more confusion.

Resolution limits the scheme. At 30 m a pixel covers 900 square metres, so a pixel over a street with trees and houses is a mixture, and no classifier can give it one true label.

Unsupervised classification

An unsupervised method groups pixels by similarity and asks you to name the groups afterwards. It needs no training samples, so it is a good first look at an unfamiliar scene.

K-means

K-means is the standard method. You choose the number of clusters k and the algorithm runs these steps.

  1. Place k starting centres in the feature space, either at random or spread along the data.
  2. Assign every pixel to the nearest centre, using straight line distance across all bands.
  3. Move each centre to the mean of the pixels assigned to it.
  4. Repeat steps 2 and 3 until fewer than a set share of pixels change cluster, or a maximum number of rounds is reached.

The number of clusters is the main decision. If you ask for exactly as many clusters as classes, one cluster often mixes two surfaces. The usual practice is to ask for two to four times as many clusters as classes, for example ten to twenty, and then merge clusters into classes by looking at them over the imagery. A cluster is only a group of similar pixels, and "similar" does not always mean the same land cover.

The GRASS tool i.cluster in QGIS and KMeans in scikit-learn both follow this pattern. The GRASS tool adds a minimum cluster size and a separation setting.

ISODATA

ISODATA is K-means with extra rules. It splits a cluster that is large and spread out, merges two clusters whose centres are close, and removes clusters that are too small. The number of clusters therefore changes while it runs, within a range you set. It is available in ERDAS Imagine and ENVI. The results are close to K-means when the settings are sensible.

Strengths and limits

Reading
StrengthsNo samples, fast, repeatable, shows the natural spectral groups of a scene
LimitsThe clusters may not match the classes you want, the result changes with k and with the starting centres, and naming clusters takes judgement

Supervised classification

A supervised method learns from training samples, which are pixels or polygons whose class you know. The learning step builds a model, and the classification step applies the model to every pixel.

Training samples

The samples matter more than the algorithm.

  • Draw samples in places you can identify with certainty, using a higher resolution basemap or a field visit.
  • Cover the variety of each class. A built-up class made only of grey roofs will miss red tiled roofs and dense slums.
  • Use enough pixels per class. A common rule for parametric methods is at least ten times the number of bands, and tree-based methods like hundreds or thousands.
  • Keep the class sizes similar, or the classifier favours the large classes.
  • Keep a separate set of reference points for testing, and never train on them.

Methods compared

MethodIdeaStrengthsLimits
ParallelepipedA box around the range of each class in every bandVery fast and simpleBoxes overlap, and many pixels stay unclassified or get the wrong class
Minimum distanceEach pixel gets the class with the nearest meanFast, works with few samplesIgnores how spread out a class is, so a variable class such as built-up is under-captured
Maximum likelihoodEach class is a bell-shaped cloud with a mean and a covariance, and the pixel goes to the class most likely to have produced itThe classic method, uses spread and band correlation, available in QGIS through GRASSAssumes a normal distribution, needs many samples per class, and fails if a class has little variation
Spectral angle mapperCompares the shape of the spectrum, not its brightnessReduces the effect of shadow and illuminationWeak when classes differ mostly in brightness
k-nearest neighboursThe label comes from the k closest training pixelsNo training step, easy to understandSlow on large images, sensitive to noisy samples
Decision treeA series of yes or no questions on band values, such as NIR above a thresholdReadable rules, handles any distributionA single tree overfits
Random ForestHundreds of trees, each trained on a random part of the samples and bands, and the pixel takes the majority voteAccurate, robust to noise and to a modest number of samples, reports which features matterPredictions are flat and can look blocky, and the model is harder to explain than one tree
Support vector machineFinds the boundary with the widest margin between two classes, using a kernel for curved boundariesStrong with small training setsNeeds tuning, slow on large training sets
Gradient boostingTrees built one after another, each correcting the lastOften the most accurate on tables of numbersMore settings to tune
Neural networks and deep learningLayers of weights learned from data. Convolutional networks read a window of pixels, not one pixelBest results on high resolution imagery and large labelled datasetsNeeds thousands of samples, a GPU, and care to avoid overfitting

How maximum likelihood decides

For each class the method stores the mean of every band and the covariance between bands from the training pixels. For each pixel it calculates the probability density of that pixel under every class and picks the highest. Two classes with the same mean but different spread get different boundaries, which is the advantage over minimum distance. The method works when the classes have sample sizes well above the number of bands, since the covariance matrix needs enough pixels to be estimated.

How Random Forest decides

Each tree sees a random sample of the training pixels, drawn with replacement, and at each split it looks at a random subset of the features. The trees therefore disagree on details but agree on the broad pattern. A pixel passes through every tree, each tree votes, and the class with the most votes wins. The share of votes is a rough confidence, which can be mapped. Pixels that are left out of a tree's sample are used to estimate the error without a separate test set, which is called the out-of-bag score. Treat it as a quick check, because it still comes from the same area as the training samples.

Seeing the methods on one image

The figure below shows a false colour image of eastern Bengaluru next to the maps that K-means and a Random Forest made from the same six Sentinel-2 bands and three indices. Drag the white line to compare, and switch the method. The two maps share the same broad pattern and differ where built-up land, dry fields and sparse trees mix. The Python tutorial builds both.

Drag to compare the satellite image with the map made from it
A 20 km square in eastern Bengaluru, dry season 2026, 10 m cells.
Satellite image of the square
Land cover map of the square
FALSE COLOUR IMAGERANDOM FOREST MAP
False colour shows shortwave infrared, near infrared and red as red, green and blue: water is black, vegetation green, built-up land purple-grey, dry fields pink. Contains modified Copernicus Sentinel data 2026.

Features beyond the raw bands

The input to the classifier does not have to be only the bands. Adding derived layers often helps more than changing the algorithm.

  • Spectral indices such as NDVI, NDWI and NDBI turn band differences into single layers. A tree-based method gains most because it can use the ratio directly.
  • Texture measures such as the local standard deviation separate a smooth bare field from a built-up block with the same average colour.
  • Terrain layers such as slope and elevation separate classes that follow the land, for example plantations on hills.
  • Several dates, such as a wet season and a dry season image, separate crops from fallow land and evergreen from deciduous trees. Much of the confusion in a single dry season image comes from dry fields looking like concrete.

Pixel-based, object-based and sub-pixel methods

Everything above classifies one pixel at a time, which is the pixel-based approach. It gives the salt and pepper look in a heterogeneous area.

Object-based image analysis first groups neighbouring pixels into segments, and then classifies segments using their mean colour, shape, size and texture. It suits high resolution imagery, where a roof is many pixels, and it gives cleaner maps. It is available in Orfeo Toolbox, eCognition and as segmentation tools in Python.

Sub-pixel methods such as spectral unmixing estimate the share of each class in a pixel, for example 40 percent built-up and 60 percent vegetation. They suit coarse pixels over mixed ground.

After classification

A pixel-based map often has isolated pixels of a different class. A sieve filter removes groups smaller than a chosen number of pixels, and a majority filter gives each pixel the most common class in its window. Both make the map easier to read and do slightly change the areas, so say which you used.

Accuracy assessment

A map without an accuracy figure is an unchecked claim. The assessment compares the map with reference data that was not used for training.

The confusion matrix

A confusion matrix has the reference class in the columns and the mapped class in the rows. The small example below has three classes and 100 reference points.

Mapped \ ReferenceWaterBuilt-upVegetationRow total
Water181120
Built-up2361250
Vegetation042630
Column total204139100

The diagonal holds the points where map and reference agree.

  • Overall accuracy is the diagonal total divided by all points: (18 + 36 + 26) / 100 = 80 percent.
  • User's accuracy is the diagonal cell divided by the row total. It answers the question of a map reader: if the map says built-up here, how often is it built-up on the ground? For built-up it is 36 / 50 = 72 percent.
  • Producer's accuracy is the diagonal cell divided by the column total. It answers the question of the map maker: of the real built-up points, how many did the map find? For built-up it is 36 / 41 = 88 percent.
  • The commission error is 100 percent minus the user's accuracy, and the omission error is 100 percent minus the producer's accuracy.

A class can score high on one and low on the other. A classifier that labels everything uncertain as built-up has a high producer's accuracy for built-up and a poor user's accuracy.

A real confusion matrix

The matrix below is from the same two maps, checked against 87 independent reference points. Switch the method and hover a cell. A class can score high on user's accuracy and low on producer's accuracy, or the reverse, and the chart shows where each method does that. The sample is small, so read the numbers as indicative.

Random Forest: 65 of 87 reference points are right (75%)
Independent reference points. Rows: what the map says. Columns: what the point really is. Hover a cell.
What the point really isWaterBuilt-upVegetationOpen landWater901090%Built-up1225469%Vegetation0121384%Open land0161365%User's accuracy90%92%64%65%Producer's
Darker cells hold more points. The bars on the right are user's accuracy: how often the map is right when it says this class. The bars below are producer's accuracy: how often a point of this class was found. The weakest user's accuracy is open land (65%). 87 points is a small sample, so the numbers are indicative.

Kappa and F1

The kappa coefficient compares the overall accuracy with the agreement expected by chance. It was the standard for decades and is still printed by most software, including the GRASS tool r.kappa. Recent guidance argues that it adds little beyond the overall, producer's and user's accuracy, so report those first. The F1 score is the harmonic mean of the user's and producer's accuracy for one class, and it gives a single number per class.

Reference data and sampling

  • Use points, or small polygons, that were not part of the training set, and keep them well away from the training areas. Neighbouring pixels are similar, so a test point next to a training pixel flatters the model.
  • Label the reference points from a source better than the imagery being classified, such as a high resolution basemap, without looking at the map first.
  • Draw the points by stratified random sampling, with a minimum number per class, often 50. A rare class needs its own quota, since simple random sampling may place none in it.
  • When the points were drawn in equal numbers per class, the overall accuracy is not an estimate for the whole map. Area-weighted estimators, such as those in the Olofsson guidance, correct for this, and they also give confidence intervals for the class areas.

Why accuracy from a training split is optimistic

Splitting the training samples into a training part and a test part, which most machine learning tutorials do, gives a figure that is nearly always much higher than the accuracy against independent points. The samples were picked as clear examples, and the test pixels sit in the same polygons as the training pixels. The fair figure is the one from points drawn independently across the whole area. A large gap between the two numbers is a signal that the training samples do not represent the map.

Common problems

ProblemCauseWhat helps
Built-up and bare soil confusedDry soil and concrete reflect alike in the visible and near infrared bandsSWIR bands, a second season, texture, more samples of both
Shadows labelled as waterBoth are dark in every bandA shadow class, or masks from a terrain model
Mixed pixelsOne pixel covers several surfacesFiner imagery, sub-pixel methods, or fewer and broader classes
One class dominatesUnequal training sizesBalance the samples or weight the classes
Clouds and hazeNot removed before classificationCloud mask and a median composite
Map looks good, accuracy lowClasses were judged by eye, not testedIndependent reference points

Key takeaways