i need to convert a folder of 200 .jpg images to .mat (to create 200 separate .mat files). i know there should be an easy matlab for loop to do this but I need help (newbie). thanks!
40 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
i need to convert a folder of 200 .jpg images to .mat (to create 200 separate .mat files). i know there should be an easy matlab for loop to do this but I need help (newbie). thanks!
2 Commenti
Risposte (3)
Guillaume
il 23 Apr 2019
folder = 'C:\somewhere\somefolder';
filelist = dir(fullfile(folder, '*.jpg')); %get list of all jpg files in the folder
matnames = regexprep({filelist.name}, '\.jpg$', '.mat'); %replace .jpg by .mat in all file names
for fileidx = 1:numel(filelist)
img = imread(fullfile(folder, filelist(fileidx).name)); %read image
save(matnames{fileidx}, img); %save in respective mat file
end
As for saving in HDF5 format, it's unclear if you want one file per image or just one hdf5 file with a dataset per image. In any case, you wouldn't use save as you have written but h5create and h5write.
0 Commenti
Rik
il 21 Apr 2019
You're quite close. You were only missing the way to generate char arrays from a pattern. To do that you can use the sprintf function. You will find a small example below. If you don't understand the syntax I encourage you to read the documentation. Matlab has excelent documentation with many examples.
for n=1:200
im = imread(sprintf('%04d.jpg',n));
save(sprintf('%04d.mat',n),'im');
end
1 Commento
Kurt
il 11 Dic 2023
This conversion does not create the .mat structure that I am looking for. For example, if I feed it a 1024x2048x3 jpg file, I get a .mat file with an img structure that is 1024x2048x3 of class uint8.
As an example, the topo60c .mat file has this structure:
Name Size Bytes Class
description 1x1 342 string
topo60c 180x360 518400 double
topo60cR 1x1 128 map.rastereff.GeographicCellsReference
Image Analyst
il 21 Apr 2019
8 Commenti
Rik
il 23 Apr 2019
Apparently the file 00000001.jpg doesn't exist, so the underlying assumption of my code isn't true. You don't need the batch processor.
Did you make sure the current folder contains the files? Otherwise you will need to put the full path in your loader.
Also, the save function does not support saving a h5 file.
Image Analyst
il 23 Apr 2019
Use the other FAQ code -- the one that uses dir() to get the filenames.
Vedere anche
Categorie
Scopri di più su Convert Image Type in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!