This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Plot Land Classification with Color Features and Superpixels

This example shows how to perform land type classification based on color features using K-means clustering and superpixels. Superpixels can be a very useful technique when performing segmentation and classification, especially when working with large images. Superpixels enable you to break an image into a set of structurally meaningful regions, where the boundaries of each region take into account edge information in the original image. Once you break an image into superpixel regions, classification algorithms can be used to classify each region, rather than having to solve the classification problem over the full original image grid. The use of superpixels can provide large performance advantages in solving image classification problems while also providing a high quality segmentation result. This example requires the Statistics and Machine Learning Toolbox™

Read an image into the workspace. For better performance, the example reduces the size of the image by half.

A = imread('');

A = imresize(A,0.5);

Convert the image to the L*a*b* color space.

Alab = rgb2lab(A);

Compute the superpixel oversegmentation of the original image and display it.

[L,N] = superpixels(Alab,20000,'isInputLab',true);
BW = boundarymask(L);

Create a cell array of the set of pixels in each region.

pixelIdxList = label2idx(L);

Determine the median color of each superpixel region in the L*a*b* color space.

meanColor = zeros(N,3);
[m,n] = size(L);
for  i = 1:N
	   meanColor(i,1) = mean(Alab(pixelIdxList{i}));
    meanColor(i,2) = mean(Alab(pixelIdxList{i}+m*n));
    meanColor(i,3) = mean(Alab(pixelIdxList{i}+2*m*n));

Cluster the color feature of each superpixel, using the kmeans function. Visually, there are 4 types of land that are distinguishable in the blue marble image based only on color features: forested regions, dry/desert regions, ice covered regions, and water. When solving the clustering problem with kmeans, use four clusters. This step requires the Statistics and Machine Learning Toolbox.

numColors = 4;
[idx,cmap] = kmeans(meanColor,numColors,'replicates',2);
cmap = lab2rgb(cmap);
Lout = zeros(size(A,1),size(A,2));
for i = 1:N
    Lout(pixelIdxList{i}) = idx(i);

Use cluster centers as the colormap for a thematic map. The mean colors found during kmeans clustering can be used directly as a colormap to give a more natural visual interpretation of the land classification assignments of forest, ice, dry land, and water.