The Mahalanobis distance
classification is widely used in clustering. The equation has a covariance
matrix that works on the variation of the classes to create similarity.

In Matlab, we have the
function ‘mahal’ that can calculate the distance between a point and a sample
subset.

Let’s use the Mahal()
function to cluster a RGB image,

Let’s make four clusters,
for the image ‘flower8.jpg’ based on the colors(ie. RGB components).

Take a small subset of the
yellow color flower as shown in the picture.

We will use the Red,
Green and Blue components of this subset as the sample. The mean and the
covariance of this subset is calculated.

Now the distance of each
pixels in the image is computed from the sample subset.

The figure below shows the
distance of each pixels in the image computed from the subset(yellow color).

Note that the distance is
minimal for the yellow color and it is high for other colors.

Likewise, the subset of
the blue and pink color is also considered as samples and the computation is
done accordingly.

The minimum distance with
respect to the subsets is generated and the corresponding pixel position is
updated with a unique cluster number.

MATLAB CODE:

clear all

clc

%READ AN RGB IMAGE

A = imread('flower8.jpg');

R = A(:,:,1); %RED
COMPONENT

G = A(:,:,2); %GREEN
COMPONENT

B = A(:,:,3); %BLUE
COMPONENT

%PREALLOCATE THE VECTOR

mahal_dist = zeros([numel(B),4]);

%CROP THE SMALL SUBSET OF
THE YELLOW FLOWER

A1 = imcrop(A,[824.5 203.5 33
22]);

subset_R = A1(:,:,1);

subset_G = A1(:,:,2);

subset_B = A1(:,:,3);

%CALCULATE MAHALANOBIS
DISTANCE FOR THE RED, BLUE AND GREEN COMPONENTS OF THE WHOLE IMAGE AND ALSO THE

%RED,GREEN AND BLUE
COMPONENTS OF THE SMALL SUBSET

mahal_dist(:,1) =
mahal(double([R(:),G(:),B(:)]),double([subset_R(:),subset_G(:),subset_B(:)])); %Yellow

%CROP THE SMALL SUBSET OF
THE BLUE FLOWER

A1 = imcrop(A,[449.5 44.5 49
33]);

subset_R = A1(:,:,1);

subset_G = A1(:,:,2);

subset_B = A1(:,:,3);

mahal_dist(:,2) =
mahal(double([R(:),G(:),B(:)]),double([subset_R(:),subset_G(:),subset_B(:)])); %Blue

%CROP THE SMALL PORTION OF
THE PINK FLOWER

A1 = imcrop(A,[765.5 243.5 60
50]);

subset_R = A1(:,:,1);

subset_G = A1(:,:,2);

subset_B = A1(:,:,3);

%CALCULATE MAHALANOBIS
DISTANCE FOR THE RED, BLUE AND GREEN COMPONENTS OF THE WHOLE IMAGE AND ALSO THE

%RED,GREEN AND BLUE
COMPONENTS OF THE SMALL SUBSET

mahal_dist(:,3) =
mahal(double([R(:),G(:),B(:)]),double([subset_R(:),subset_G(:),subset_B(:)])); %Pink

%CROP THE SMALL SUBSET OF
THE OTHER PARTS

A1 = imcrop(A,[505.5 389.5 49
33]);

subset_R = A1(:,:,1);

subset_G = A1(:,:,2);

subset_B = A1(:,:,3);

%CALCULATE MAHALANOBIS
DISTANCE FOR THE RED, BLUE AND GREEN COMPONENTS OF THE WHOLE IMAGE AND ALSO THE

%RED,GREEN AND BLUE
COMPONENTS OF THE SMALL SUBSET

mahal_dist(:,4) =
mahal(double([R(:),G(:),B(:)]),double([subset_R(:),subset_G(:),subset_B(:)]));

%FIND THE MINIMUM VALUE AND GIVE A CLUSTER NUMBER

[val,ind]=min(mahal_dist,[],2);

mask = zeros(size(B));

for k=1:size(mahal_dist,2)

mask(ind(:)==k)=k;

end

figure, subplot(121),imagesc(A);
title('RGB IMAGE');
subplot(122),imagesc(mask);colormap(jet);colorbar;title('CLUSTERS');

figure,

for k=1:size(mahal_dist,2)

subplot(2,2,k);

imshow(uint8(double(A).*logical(mask==k)));

end

You can also
revisit ‘K_means Clustering for RGB image’ and compare both the methods.