Search code examples
imagematlabentropy

Taking multiple images from a folder and perform specific operation in MATLAB


I know how to calculate the entropy of a single image channel. But I want to calculate entropy for each image from a dataset (almost 800), so that the output shows "what percent of images" are in some specific entropy range.

my entropy code: (I'm using MATLAB 2015b)

I= im;
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);

figure(1),imshow(im),title(['Entropy for R channel = ', num2str(Er),', Entropy for B channel = ', num2str(Eb)]);

Solution

  • You can put your code in a for loop:

    files = dir('c:\data\*.jpg');  % Or whatever filter will pick your images
    for k = 1 : length(files)
       im = fullfile(files(k).folder, files(k).name)
       im = imread(im);
      
       % Your code %
       % but change Er = ... in Er(k) = ... and Eb(k) = ... 
       % so you can store the results
    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)])
    

    Be careful because if you post your code as it is, it will try to open 800 figures!!