# matlab percentage always showing zero

1 view (last 30 days)
rakib mostafiz on 25 Jul 2021
Answered: Jan on 25 Jul 2021
I want to calculate how much image has higher 'red channel entropy' than 'blue channel entropy'. i wanted the result in percentage. But it is always showing zero. But i have checked random single image, and some of them satisfy the conditon. how can i solve this error?
folder = 'F:\raw-890\';
filePattern = fullfile(folder, '*.png');
myFiles = dir(filePattern); % the folder inwhich ur images exists
for k = 1 : length(myFiles)
fullFileName = fullfile(folder, myFiles(k).name);
Red = I(:,:,1);
Green = I(:,:,2);
Blue = I(:,:,3);
%I = I(:); % Vectorization of RGB values
p = imhist(Red); % Histogram
p(p == 0) = [ ];% remove zero entries in p
p = p ./ numel(I); % normalize p so that sum(p) is one.
Er = round(-sum(p.*log2(p)),3);
p = imhist(Blue); % Histogram
p(p == 0) = [ ];% remove zero entries in p
p = p ./ numel(I); % normalize p so that sum(p) is one.
Eb = round(-sum(p.*log2(p)),3);
end
percentage = sum(Er > Eb) / numel(Er) * 100; % Percentage of images with red entropy higher than blue entropy
disp(['Percentage of images with red entropy higher than blue entropy: ' num2str(percentage)])

Jan on 25 Jul 2021
After: p = p ./ numel(I), sum(p) is not 1.0, but 0.333. You do not want to divide by numel(I), but by numel(Red), or equivalently: size(I,1)*size(I,2).
I = rand(640, 480, 3);
Red = I(:,:,1);
Green = I(:,:,2);
Blue = I(:,:,3);
p = imhist(Red); % Histogram
p(p == 0) = []; % remove zero entries in p
p = p ./ numel(Red); % normalize p so that sum(p) is 1,0
Er = round(-sum(p .* log2(p)), 3)
Er = 7.9990
p = imhist(Blue); % Histogram
p(p == 0) = []; % remove zero entries in p
p = p ./ numel(Blue); % normalize p so that sum(p) is one.
Eb = round(-sum(p .* log2(p)), 3)
Eb = 7.9990
You see, that the results are not 0 for random inputs.
nFile = numel(myFiles);
Er = zeros(1, nFile);
Eb = zeros(1, nFile);
for k = 1 : nFile
...
Er(k) = round(-sum(p .* log2(p)), 3);
...
Eb(k) = round(-sum(p .* log2(p)), 3);
end
percentage = sum(Er > Eb) / nFile * 100;
fprintf('Percentage of images with red entropy > blue entropy: %f\n', percentage);

R2015b

### Community Treasure Hunt

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

Start Hunting!