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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 18 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Images en Help Center y File Exchange.

Productos


Versión

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by