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 (최근 30일)
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
------------------------------

채택된 답변

DGM
DGM 2023년 5월 25일
편집: DGM 2023년 5월 25일
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
Saeed
Saeed 2023년 5월 27일
@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 2023년 5월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by