I need to calculate Entropy of picture in two directions(Horizental and vertical)? How can I implement it on matlab?
To be edited: Some possibly useful information.
Based on the document posted in the comment related to texture analysis. To find the horizontal and vertical energy for an image the statistics can be pulled from the GLCM (Gray-Level Co-Occurrence Matrix), specifically in this case the Energy
property. The direction of the nearest neighbour/values to check between the relationship. I would recommend looking more in-depth into all the properties of these functions due to my limited knowledge/experience using them.
The direction/offset is a vector defined as: [Vertical_Direction Horizontal_Direction]
MATLAB Documentation: Create gray-level co-occurrence matrix from image
MATLAB Documentation: Texture Analysis Using the Gray-Level Co-Occurrence Matrix (GLCM)
%Creating the sample image and plotting%
Sample_Image = imread("Greyscale_Image.png");
%Calculating the Gray-Level Co-Occurence Matrices%
Horizontal_Offset = [0 1];
Vertical_Offset = [1 0];
Horizontal_GLCM = graycomatrix(Sample_Image, 'offset', Horizontal_Offset, 'Symmetric', true);
Vertical_GLCM = graycomatrix(Sample_Image, 'offset', Vertical_Offset, 'Symmetric', true);
Horizontal_Statistics = graycoprops(Horizontal_GLCM);
Horizontal_Statistics.Energy
Vertical_Statistics = graycoprops(Vertical_GLCM);
Vertical_Statistics.Energy
Interestingly the entropy of the Horizontal_GLCM
and Vertical_GLCM
are equal
entropy(Horizontal_GLCM)
entropy(Vertical_GLCM)
Entropy for images and strings is typically defined in the following form:
Where, pi
is the entropy probability for a given pixel intensity I
and H(s)
is the entropy for the signal/image. The probability is the frequency of the pixel intensity/number of pixels. An example of this may include:
Number of Pixels = 8
Pixel Intensity: 20 → Frequency = 1
→ Probability = 1/8
→ Entropy Term = -(1/8
)×log2(1/8
)
Pixel Intensity: 80 → Frequency = 3
→ Probability = 3/8
→ Entropy Term = -(3/8
)×log2(3/8
)
Pixel Intensity: 120 → Frequency = 3
→ Probability = 3/8
→ Entropy Term = -(3/8
)×log2(3/8
)
Pixel Intensity: 160 → Frequency = 1
→ Probability = 1/8
→ Entropy Term = -(1/8
)×log2(1/8
)
Entropy of Image:
H(s) = [-(1/8
)×log2(1/8
)] + [-(3/8
)×log2(3/8
)] + [-(3/8
)×log2(3/8
)] + [-(1/8
)×log2(1/8
)]
H(s) ≈ 1.811278 (2-bits required to encode the image based on source coding)
entropy()
FunctionThe entropy()
function returns the entropy for the image and sets the lower bound for the number of bits required to encode the image. More specifically Number_Of_Bits_Required = ceil(entropy(image))
.
%Creating the sample image and plotting%
Sample_Image = uint8([20 80 80 80; 120 120 120 160]);
imshow(Sample_Image,'InitialMagnification',1500);
title("Test Image");
set(gcf, 'Position', [100, 100, 500, 400]);
axis on
xlabel('X-Axis'); ylabel('Y-Axis');
Image_Entropy = entropy(Sample_Image);
Uses a loop to go through all the unique pixel intensities. Counts the occurrences by using a conditionally set logical matrix and taking the sum of that matrix. The probability is then found by diving the occurrences by the number of pixels. The log2()
of the probability is then taken and added to the variable Calculated_Entropy
which will accumulate/add up all the entropy terms corresponding to each specific probability.
%Creating the sample image and plotting%
Sample_Image = uint8([20 80 80 80; 120 120 120 160]);
imshow(Sample_Image,'InitialMagnification',1500);
title("Test Image");
set(gcf, 'Position', [100, 100, 500, 400]);
axis on
xlabel('X-Axis'); ylabel('Y-Axis');
%Finding the image dimensions%
[Image_Height,Image_Width] = size(Sample_Image);
Number_Of_Pixels = Image_Height*Image_Width;
%Evaluating the unique intensity values%
Unique_Intensities = unique(Sample_Image);
%Initializing variables for later use%
Number_Of_Unique_Intensities = length(Unique_Intensities);
Probabilities = zeros(Number_Of_Unique_Intensities,1);
Calculated_Entropy = 0;
%Scanning through the unique intensities and evaluating the probabilities%
for Intensity_Index = 1: Number_Of_Unique_Intensities
%Grabbing a unique intensity value%
Intensity_Value = Unique_Intensities(Intensity_Index,1);
%Evaluating the frequency of the unique intensity value%
Check_Array = (Sample_Image == Intensity_Value);
Probabilities(Intensity_Index,1) = sum(Check_Array,'all')/Number_Of_Pixels;
Calculated_Entropy = -Probabilities(Intensity_Index,1)*log2(Probabilities(Intensity_Index,1))+Calculated_Entropy;
end
Image_Entropy = entropy(Sample_Image);
fprintf("Calculated entropy: %f\n",Calculated_Entropy);
fprintf("Using the entropy function: %f\n",Image_Entropy);
Ran using MATLAB R2019b