Large number of Images to stack
Mostrar comentarios más antiguos
Hey there!
I have a large number of images (5000) to analyze by performing fast fourier transforms and other operations.
In particular these images are in another folder with respect to the .m file.
I want them to be saved in a vector (stack), so that calling
stack(i)
I can access the i-th image.
I tried to create a class but I'm not so skilled in OOP and the operation in a bit time consuming. Here is the code:
clc
clear all
imageNumber = 5000;
pixels = 800;
myFolder = 'C:\here i put the path folder containing the images';
files = dir( fullfile( myFolder, '*.bmp' ) );
stack = ImageStack( myFolder, files, imageNumber);
for i = 1:imageNumber
stack(i) = ImageStack( myFolder, files(i), imageNumber );
end
while in a separate file i defined the class:
classdef ImageStack
properties
img
end
methods
function object = ImageStack( myFolder, files, numImg )
for i = 1:numImg
ImNames = files(i).name;
fullImNames = fullfile( myFolder, ImNames );
object.img = imread( fullImNames );
end
end
end
end
and it took 18s.
I'm sure there is a clever way to perform this but I cannot see it. And, since I'm a beginner, I'm pretty sure as well that there is a better way to write this code.
Do you have any advice?
Thanks for your support!
Andrea
1 comentario
Walter Roberson
el 13 de Jul. de 2021
Is there a particular reason that you are passing in the number of images, instead of just using size() or length() to figure out how many elements are in the files structure ?
object.img = imread( fullImNames );
You are overwriting all of the img property of object each time through the loop.
Respuesta aceptada
Más respuestas (1)
Simon Chan
el 13 de Jul. de 2021
Editada: Simon Chan
el 13 de Jul. de 2021
Do not have 5000 images to try, just try because "cellfun" may be a bit slow.
myFolder = 'C:\here i put the path folder containing the images';
files = dir( fullfile( myFolder, '*.bmp' ) );
fileList = {files.name};
fileread = cellfun(@(x) imread(x), fileList, 'UniformOutput', false);
stack = cell2struct(fileread, 'object', 1);
2 comentarios
Andrea de Marco
el 13 de Jul. de 2021
Simon Chan
el 13 de Jul. de 2021
Add one more line and try again if you wish, because the code only allows the scipt to be run in the directory contains the images.
myFolder = 'C:\here i put the path folder containing the images';
files = dir( fullfile( myFolder, '*.bmp' ) );
fileList = {files.name};
cd(myFolder);
fileread = cellfun(@(x) imread(x), fileList, 'UniformOutput', false);
stack = cell2struct(fileread, 'object', 1);
Categorías
Más información sobre MATLAB Data API for C++ en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!