# matlab percentage always showing zero

조회 수: 2(최근 30일)
rakib mostafiz 2021년 7월 25일
답변: Jan 2021년 7월 25일
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 2021년 7월 25일
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!

Translated by