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

1 次查看(过去 30 天)
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

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-10-18
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by