1
\$\begingroup\$

I am attempting to plot histograms for batch of images in Matlab. The file structure of input images and the experimental implementation are listed as below.

  • File structure of input images

    The matlab code HistogramPlotting.m file is placed in the project root folder and there are multiple folders which is named by its data index number for placing input images. For example, there are 1/1.bmp, 1/2.bmp, 1/3.bmp, ..., 2/1.bmp, 2/2.bmp, ..., etc. The output histograms for the same data index are in a folder "<data_index>/Histograms/". For example, the results of images in folder 1/ are placed in folder 1/Histograms/.

    - /
      | - HistogramPlotting.m
      | - 1/
            | - Histograms/                <= This folder will be created for placing histogram outputs
            | - 1.bmp
            | - 2.bmp
            | - 3.bmp
            | - 4.bmp
            ...
    
  • Histogram plotting implementation (HistogramPlotting.m):

    VideoLength = 100;
    
    for DataIndex = 1:10
        for i = 1:VideoLength
            InputFileName = sprintf("%s%d%s%d%s", "./", DataIndex, "/", i, ".bmp");
            if (isfile(InputFileName) == false)
                continue;
            end
            image = imread(InputFileName);
    
            f = figure;
            f.Position = [100 100 540 400];
            histogram(image);
            xlabel('Intensity');
            ylabel('Count');
            title(sprintf("Histogram (%d)", i));
            OutputFoldername = sprintf("%s%d%s", "./", DataIndex, "/Histograms/");
            mkdir(sprintf("%s", OutputFoldername));
            OutputFilename = sprintf("%s%d%s", OutputFoldername, i, ".bmp");
            saveas(gcf, OutputFilename);
            close(f);
        end
    end
    

All suggestions are welcome.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

There's not a whole lot of things to comment on this code. My biggest gripe with it is the comparison of a boolean value with false. I see this a lot, it is a common "mistake". There always exist the following two equivalences, if b is a logical (Boolean) value:

b == true     <==>     b
b == false    <==>     ~b

Thus, instead of writing

if (isfile(InputFileName) == false)

write

if (~isfile(InputFileName))

Not only is this more concise, but it also "sounds" better when read, it maps directly to how we'd say the condition in English, and therefore it takes less effort to understand: "is file is equal to false" vs "not is file".

The other strange thing is this line:

mkdir(sprintf("%s", OutputFoldername));

which is the same as

mkdir(OutputFoldername);

Then I have two nit-picky things.

  1. Instead of "/", use filesep. That makes your code platform independent. filesep is / on Linux or macOS, and \ on Windows. You can even use fullfile to replace some of your use of sprintf. For example: InputFileName = fullfile('.',num2str(DataIndex),[num2str(i),'.bmp']) (not sure if this is better readable or what, but it's an option).

  2. Some of your graphics commands explicitly use handle f, and some use gcf. In saveas you explicitly use gcf, which should be replaced by f. In other cases you implicitly use gcf or gca (e.g. histogram, xlabel, title, ...). I think code looks nicer when you use gcf implicitly or explicitly everywhere, or use explicit handles everywhere. For example, I would write this as follows:

    f = figure('Position',[100 100 540 400]);
    ax = axes('parent',f);
    for DataIndex = 1:10
      for i = 1:VideoLength
        % ...
        cla(ax)
        histogram(ax,image)
        xlabel(ax,'Intensity')
        ylabel(ax,'Count')
        title(ax,sprintf("Histogram (%d)", i))
        % ...
        saveas(f, OutputFilename)
      end
    end
    close(f)
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.