I try to read a file from my workspace,and to calculate the entropy of order one of each image band using the histogram of the gray levels this is my try and I'am enable to read the .mat file with the load function
clc
I = load('file.mat');
E = entropy(I);
hist(E);
figure
plot(E);
Update: this is the code:
I = load('file.mat');
E = zeros(1,size(I,3));
for idx = 1 : size(I,3)
%// Calculate PDF
chan = I(:,:,idx);
h = imhist(chan);
end
Now,I get this error:
thanks again for any help
Recall from definition that the entropy (Shannon) is defined as:
In this case, b = 2
. What you need to do is assuming we have a grayscale image, we need to find the probability distribution function of the image, then use the above definition to calculate our entropy. The entropy
command in MATLAB already does this for us, but from your comments, you want to do this from first principles. Just follow the procedure I outlined above. As such, for a grayscale image, we first need to grab the histogram of the image through imhist
, then normalize this by the total number of pixels in your image to get the probability distribution function.
Therefore, if your image is loaded into im
and is grayscale, this is how you calculate the entropy:
%// Grab image data
im = I.indian_pines_corrected;
%// Calculate PDF
h = imhist(I);
h = h / numel(I);
%// Set any entries in the PDF that are 0 to 1 so log calculation works
h(h == 0) = 1;
%// Calculate entropy
E = -sum(h.*log2(h));
The above code goes through the logic we have outlined. However, look at the third line of code. Any entries in the probability distribution function that are 0, the log
calculation would generate an invalid number, so in order for the log
function to safely make this 0, we set any values in the PDF that are 0 to 1, so that log(1) = 0
. Essentially, the entropy calculation will ignore these entries so that we can properly calculate the value.
Because you have a colour image, all you have to do is treat each colour channel as a separate grayscale image, so we can just use the above code and loop over each channel. Just extract each colour channel as a grayscale image, and calculate the entropy of each colour channel. Therefore, assuming I
is a colour image:
%// Grab image data
im = I.indian_pines_corrected;
E = zeros(1,size(I,3));
for idx = 1 : size(I,3)
%// Calculate PDF
chan = I(:,:,idx);
h = imhist(chan);
h = h / numel(chan);
%// Find any entries in the PDF that are 0 to 1 so log calculation works
h(h == 0) = 1;
%// Calculate entropy
E(idx) = -sum(h.*log2(h));
end
E
would be an array where each element would tell you the entropy for each colour channel. Therefore, the first element would be the entropy for the first channel (red), the second element would be the second (green), and so on.