How to get the entropy of an image in matlab?
6 views (last 30 days)
Show older comments
Please i'm trying to get the entropy of an image in matlab. This was the code i used but i'm still getting error.
Can anyone help?
A=imread('cameraman.tif');
size(A)
A=rgb2gray(A);
B=imresize(A, [64 64]);
size(B)
C=entropy(B);
0 Comments
Answers (1)
Image Analyst
on 10 Dec 2020
You should only call rgb2gray() on an RGB image, not one that is already gray scale. See below for corrected and improved code:
% Read in the image from disk.
grayImage=imread('cameraman.tif');
% Display the image.
subplot(1, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Image');
impixelinfo;
% Convert to gray scale but only if needed.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Resize the image.
resizedImage = imresize(grayImage, [64, 64]);
% Display the image.
subplot(1, 2, 2);
imshow(resizedImage, 'InitialMagnification', 400);
axis('on', 'image');
title('Resized Image');
impixelinfo;
[rows2, columns2, numberOfColorChannels2] = size(resizedImage)
% Compute the entropy of the whole image.
% If you want local entropy, use entropyfilt().
totalEntropy = entropy(resizedImage);
message = sprintf('The entropy of the image is %f.', totalEntropy);
fprintf('%s\n', message);
msgbox(message);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!