How can I vectorize Imread for loop to run faster?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have a piece of code that looks like this:
for k=1:numel(file_names)
Img{1,k}=imread(file_names{k});
end
However, because k can be very long, it takes a long time for the loop to run. Is there a way I can achieve the same result (i.e. a vector Img which contains k cells in everyone of which a single 512x512 uint8 image is contained) by vectorization? If not, how can I make this run faster?
Thanks,
0 commentaires
Réponses (2)
OCDER
le 2 Août 2018
Modifié(e) : OCDER
le 2 Août 2018
Your current method for using for loop is okay, as vectorizing is more for matrix manipulation + math. This for loop could be slow if you do not initialize Img before the loop, as that'll cause matlab to grow Img by creating a temp array, copying Img to this temp array, replacing old Img with new Img.
Did you initialize Img before hand like this, and it's still slow? And how many images are you trying to read at once?
Img = cell(1, numel(filenames)
for k=1:numel(file_names)
Img{1,k}=imread(file_names{k});
end
Some more useful suggestion and file links here:
5 commentaires
OCDER
le 2 Août 2018
Hmm, 512x512 size tiff should load pretty fast... If you have a solid state drive, then you're set - do not change SSD to HDD.
If you have an old spinning HDD, then the solid state drive would be a huge upgrade, not only for matlab but for your OS. https://en.wikipedia.org/wiki/Solid-state_drive
It was ~$80 for 250GB, and it comes with a cloning software to help you transfer your old OS to the new OS. I believe you need some sort of USB cable + adapter to plug into your SSD before replacing. But then the topic is no longer about matlab... You'll have to look on the web to see if it's worth the upgrade. RAM is another topic for speeding things up.
If you have access to another computer with matlab with more RAM or SSD, test out the imread there to see if it's worth that few sec speed gain. Don't want you upgrade and still suffer from slow speeds o_O
OCDER
le 2 Août 2018
For my test, it took 0.63 s to read 150 tiff images. I do have SSD , 12GB RAM, 3.2GHZ quad core. Here's script so that anyone can test it.
filenames = cell(1, 150);
for j = 1:numel(filenames)
filenames{j} = sprintf('test%d.tiff', j);
imwrite(rand(512, 512, 3), filenames{j});
end
tic
Img = cell(1, numel(filenames));
for k=1:numel(filenames)
Img{1,k}=imread(filenames{k});
end
toc %0.63s
Image Analyst
le 3 Août 2018
I guarantee the for loop is not the bottleneck here. I doubt you have more than a few thousand images in a folder, and a for loop of only a few thousand iterations is insanely fast.
tic
for k = 1 : 150;
k; % Basically do nothing but iterate.
end
toc
Shows:
Elapsed time is 0.000008 seconds.
I agree with OCDER that getting an SSD is the most likely solution to speed things up.
2 commentaires
Voir également
Catégories
En savoir plus sur Read, Write, and Modify Image dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!