Why my code calculates values for image statistics, which are far greater or less than the standard ones? Both for encrypted and decrypted images.
1 view (last 30 days)
Show older comments
clc, clear all, close all
% Read image in different formats (JPG, PNG, TIFF)
%imageFormats = {'jpg', 'png', 'tif'};
imageFormats = {'jpg'};
for i = 1:numel(imageFormats)
format = imageFormats{i};
imageFileName = sprintf('image.%s', format);
image = imread(imageFileName);
% Convert image to grayscale if it has multiple channels
if size(image, 3) > 1
image = rgb2gray(image);
end
% Compute image statistics
entropyValue = entropy(image);
contrastValue = std2(image);
correlationValue = corr2(image, image);
energyValue = sum(sum(image.^2)) / numel(image);
homogeneityValue = sum(sum(1 ./ (1 + (image - image).^2))) / numel(image);
meanAbsDeviationValue = mad(double(image(:)));
% Display the results
fprintf('Image Format: %s\n', format);
fprintf('Entropy: %.4f\n', entropyValue);
fprintf('Contrast: %.4f\n', contrastValue);
fprintf('Correlation: %.4f\n', correlationValue);
fprintf('Energy: %.4f\n', energyValue);
fprintf('Homogeneity: %.4f\n', homogeneityValue);
fprintf('Mean of Absolute Deviation: %.4f\n', meanAbsDeviationValue);
fprintf('------------------------------\n');
% Display the image
figure;
imshow(image);
title(sprintf('Image Format: %s', format));
end
0 Comments
Accepted Answer
DGM
on 25 May 2023
Edited: DGM
on 25 May 2023
Let me guess, they're off by a factor of 255?
Pay attention to data scaling and class. Most images you load will be unsigned integer-class -- typically uint8. This is what you'll get.
% avoid shadowing core functions like image()
inpict = imread('peppers.png'); % uint8
% Convert image to grayscale if it has multiple channels
if size(inpict, 3) > 1
inpict = rgb2gray(inpict); % uint8
end
% Compute image statistics
entropyValue = entropy(inpict) % this expects data to be correctly-scaled for its class
contrastValue = std2(inpict) % this is dependent on scale
correlationValue = corr2(inpict, inpict) % this is always 1 (or NaN if variance is zero)
energyValue = sum(sum(inpict.^2)) / numel(inpict) % this is dependent on scale and will be wrong if fed unsigned integer data
homogeneityValue = sum(sum(1 ./ (1 + (inpict - inpict).^2))) / numel(inpict) % this is always 1
meanAbsDeviationValue = mad(double(inpict(:))) % this is dependent on scale
If you put the image in unit-scale, this is what you'd get:
% cast and rescale to unit-scale float
inpict = im2double(inpict);
% Compute image statistics
entropyValue = entropy(inpict) % this expects data to be correctly-scaled for its class
contrastValue = std2(inpict) % this is dependent on scale
correlationValue = corr2(inpict, inpict) % this is always 1 (or NaN if variance is zero)
energyValue = sum(sum(inpict.^2)) / numel(inpict) % this is dependent on scale and will be wrong if fed unsigned integer data
homogeneityValue = sum(sum(1 ./ (1 + (inpict - inpict).^2))) / numel(inpict) % this is always 1
meanAbsDeviationValue = mad(double(inpict(:))) % this is dependent on scale
Note that Contrast and MAD are smaller by a factor of 255, while Energy is not smaller by a factor of 255^2. That's because trying to do those calculations in uint8 results in severe truncation due to the limited range of the class, so the result is not just scaled wrong, it's simply wrong.
Casting/rescaling using im2double() makes sure all inputs are on a consistent scale. Unless they are on a consistent scale, you're going to end up with results that aren't comparable to each other.
Entropy() already takes the class of the input into consideration, and so the scale of the result is consistent, so long as its input is correctly scaled for its class.
The way you're calculating correlation and homogeneity will always yield a result of 1.
2 Comments
DGM
on 28 May 2023
It's hard to know what different values are expected or how under what circumstances those were obtained. You'd have to give an example.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!