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)
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
Image Format: jpg
Entropy: 7.4713
Contrast: 48.4531
Correlation: 1.0000
Energy: 254.9664
Homogeneity: 1.0000
Mean of Absolute Deviation: 40.2068
------------------------------

Accepted Answer

DGM
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
entropyValue = 6.9917
contrastValue = std2(inpict) % this is dependent on scale
contrastValue = 46.5746
correlationValue = corr2(inpict, inpict) % this is always 1 (or NaN if variance is zero)
correlationValue = 1
energyValue = sum(sum(inpict.^2)) / numel(inpict) % this is dependent on scale and will be wrong if fed unsigned integer data
energyValue = 254.7627
homogeneityValue = sum(sum(1 ./ (1 + (inpict - inpict).^2))) / numel(inpict) % this is always 1
homogeneityValue = 1
meanAbsDeviationValue = mad(double(inpict(:))) % this is dependent on scale
meanAbsDeviationValue = 36.9394
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
entropyValue = 6.9917
contrastValue = std2(inpict) % this is dependent on scale
contrastValue = 0.1826
correlationValue = corr2(inpict, inpict) % this is always 1 (or NaN if variance is zero)
correlationValue = 1
energyValue = sum(sum(inpict.^2)) / numel(inpict) % this is dependent on scale and will be wrong if fed unsigned integer data
energyValue = 0.1359
homogeneityValue = sum(sum(1 ./ (1 + (inpict - inpict).^2))) / numel(inpict) % this is always 1
homogeneityValue = 1
meanAbsDeviationValue = mad(double(inpict(:))) % this is dependent on scale
meanAbsDeviationValue = 0.1449
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
Saeed
Saeed on 27 May 2023
@DGM, can you share a code with me which calculates exact and correct values for all these properties correctly? I read many papers in which maximum of these properties are given different values!!!
DGM
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!