Clear Filters
Clear Filters

How can I extract frequency values of intensity from multiple image files to one Excel file

1 view (last 30 days)
Dear community,
I am trying to extract frequency values from histograms of multiple images (*.jpg) and then save data to an Excel file, using the following script.
However, it did not work. Please help me to extract data to one Excel file with data columns labled by the file name of image at first row.
Thank you very much,
// My code
path_directory='myfolder'; % 'myfolder' in my current directory
myfiles=dir([path_directory '/*.jpg']);
for k=1:length(myfiles)
filename=[path_directory '/' myfiles(k).name];
I=imread(filename);
I=rgb2gray(I);
[count,x]=imhist(I,25);
T=table(count);
writetable(T, 'frequency.xlsx');
end

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 18 Oct 2023
You have done almost everything correct. However, you are over-writing the excel file with each iteration, thus you will only get the values corresponding to the final file.
You can either store data for images in separate columns -
path_directory='myfolder'; % 'myfolder' in my current directory
myfiles=dir([path_directory '/*.jpg']);
%number of files
n = numel(myfiles);
%As it is known that there are 25 bins for categorizing data, the output
%will be a 25x1 vector, thus preallocate accordingly
count = zeros(25,n);
for k=1:n
filename=[path_directory '/' myfiles(k).name];
I=imread(filename);
I=rgb2gray(I);
count(:,k)=imhist(I,25);
end
T = table(count);
writetable(T, 'frequency.xlsx');
Or you can store the data for images in separate sheets -
path_directory='myfolder'; % 'myfolder' in my current directory
myfiles=dir([path_directory '/*.jpg']);
%number of files
n = numel(myfiles);
%preallocate worksheets
writetable(table(),'frequency.xlsx','Sheet', n);
for k=1:n
filename=[path_directory '/' myfiles(k).name];
I=imread(filename);
I=rgb2gray(I);
[count,x]=imhist(I,25);
T=table(count);
writetable(T, 'frequency.xlsx', 'Sheet', k);
end

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!