Wolfram Language

Extended Probability & Statistics

Image Segmentation with Gaussian Mixture Model

Images are represented as arrays of pixels. A pixel is a scalar (or vector) that shows the intensity (or color). A Gaussian mixture model can be used to partition the pixels into similar segments for further analysis.

In[1]:=
Click for copyable input
pic = ExampleData[{"TestImage", "Aerial"}]
Out[1]=
In[2]:=
Click for copyable input
pixels = Flatten[ImageData[pic]];

Visualize the distribution of pixel values.

show complete Wolfram Language input
In[3]:=
Click for copyable input
Histogram[pixels, ImageSize -> Medium, PlotTheme -> "Detailed"]
Out[3]=

Fit the pixel values to a three-component Gaussian mixture model.

In[4]:=
Click for copyable input
gmm = Quiet@ EstimatedDistribution[pixels, MixtureDistribution[{p1, p2, p3}, {NormalDistribution[a1, a2], NormalDistribution[b1, b2], NormalDistribution[c1, c2]}]];

Label each pixel with the most probable component with a maximum a posteriori probability (MAP) estimate.

In[5]:=
Click for copyable input
pxi = Table[PDF[dist, pixels], {dist, Last[gmm]}]; pxi = Transpose[pxi]/Total[pxi First[gmm]]; labels = ArrayReshape[Ordering[#, -1] & /@ pxi, ImageDimensions[pic]];

Visualize the segmented image, and compare it with the original one.

show complete Wolfram Language input
In[6]:=
Click for copyable input
GraphicsRow[{Image[0.5 (labels - 1)], pic}, ImageSize -> Large]
Out[6]=

Related Examples

de es fr ja ko pt-br ru zh