Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Grey Level Co-occurrence Matrix (GLCM) in MATLAB


Let’s see in these series of posts on how to extract the texture features from Grey Level Co-occurrence Matrix (GLCM) in MATLAB.  In simple terms, GLCM gives the spatial relationship between adjacent or neighbouring pixels. And from this GLCM Matrix, we will measure some texture features.

Let’s consider a simple example and start coding the steps in terms of algorithmic as well as programming in MATLAB.

Procedure to Generate GLCM Matrix:








MATLAB CODE:

clear all
%GLCM MATRIX

A=[1 2 3 2 3; 1 2 3 4 5; 4 4 1 1 4; 6 7 6 7 6; 1 8 1 8 8];

Displacement = 1; %NUMBER OF STEPS BETWEEN TWO PIXELS

NumQuantLevels = 8; %QUANTIZATION LEVEL
glcm = zeros([NumQuantLevels,NumQuantLevels]); %PREALLOCATE GLCM MATRIX

for i = 1:size(A,1)
    for j = 1 :size(A,2)-1
        glcm(A(i,j),A(i,j+1))=glcm(A(i,j),A(i,j+1))+1; %INCREMENT BY 1
    end

end


EXPLANATION:
The number of occurrences of a pixel and its neighboring pixel is calculated. The offset is 1 and it can be incremented based on the requirement. Here the direction of the pixel movement is towards the right. The maximum value of the pixel in the matrix A is 8 so the quantization level is fixed to 8. For instance, in the example given above, the pixel 2 and its neighbor 3 occurs 3 times in matrix A and it is updated in the GLCM matrix at the position (2,3) as 3.


Continue Reading: Texture Measures from GLCM - MATLAB CODE



like button Like "IMAGE PROCESSING" page

RGB Image clustering using Mahalanobis distance


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.


like button Like "IMAGE PROCESSING" page

Frost Filter


Based on the local statistics in a sliding window, the frost filter works on preserving the edges while suppressing the noise. The Damping factor which is an exponential damping is the key factor in controlling the smoothness of the filter. When damping factor is small, the image tends to be smooth.

ALGORITHM:









MATLAB CODE:
%FROST FILTER

%LENNA IMAGE - SPECKLE REDUCTION

% INPUT PARAMETERS - NOISY IMAGE, DAMPING FACTOR, WINDOW SIZE
load('Noise_image1.mat')
ima_nse = double(ima_nse);

%Damping factor
Damp_fact = 1;

%window size
sz = [5,5];

%Preallocate the Output Matrix
ima_fi = zeros(size(ima_nse));
if(mod(sz,2)~=1)
    sz=sz+~mod(sz,2);
end

mn = round((sz-1)/2);

%Padding with zeros around the border
EImg = padarray(ima_nse,mn);


[x,y]= meshgrid(-mn(1,1):mn(1,1),-mn(1,2):mn(1,2));
S = sqrt(x.^2+y.^2);

for i = 1:size(ima_nse,1)
    for j = 1:size(ima_nse,2)
        %Local Window
        K = EImg(i:i+sz(1,1)-1,j:j+sz(1,2)-1);
       
        %Mean value of the pixels in the local window
        meanV = mean(K(:));
       
        %variance of the pixels in the local window
        varV = var(K(:),1);
       
        %Weight for each pixel in the local window
        B =  Damp_fact*(varV/(meanV*meanV));
        Weigh = exp(-S.*B);
       
        % Filtering
        ima_fi(i,j) = sum(K(:).*Weigh(:))./sum(Weigh(:));
       
    end
end

figure,subplot(121),imagesc(ima_nse);colormap(gray);title('Original Image');
subplot(122),imagesc(ima_fi);colormap(gray);title('After Despeckling - Frost Filter');





EXPLANATION:
Here in the example given above, the damping factor = 1 and the size is 5x5.

Let’s consider another example, where damping factor =1 and the local window size is 11x11
The image becomes smooth as well as the edges.


This is the parameter, S that has the distance from centre of the pixel to its neighbours in the local window. See that the distance at the centre for the centre pixel is zero, while the distance from the centre to the adjacent pixel is 1 and the increase in value based on the distance between the pixels is evident in the figure.


While in this example, where damping factor = 3 and the local window size is 11x11, the edges are preserved. By controlling the damping factor, a trade-off between the smoothness and preservation of the edges can be done.



like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com