开发者

Loading multiple images in MATLAB

Here is the desired workflow:

  • I want to load 100 images into MATLAB workspace
  • Run a bunch of my code on the images
  • 开发者_开发问答
  • Save my output (the output returned by my code is an integer array) in a new array

By the end I should have a data structure storing the output of the code for images 1-100.

How would I go about doing that?


If you know the name of the directory they are in, or if you cd to that directory, then use dir to get the list of image names.

Now it is simply a for loop to load in the images. Store the images in a cell array. For example...

D = dir('*.jpg');
imcell = cell(1,numel(D));
for i = 1:numel(D)
  imcell{i} = imread(D(i).name);
end

BEWARE that these 100 images will take up too much memory. For example, a single 1Kx1K image will require 3 megabytes to store, if it is uint8 RGB values. This may not seem like a huge amount.

But then 100 of these images will require 300 MB of RAM. The real issue comes about if your operations on these images turn them into doubles, then they will now take up 2.4 GIGAbytes of memory. This will quickly eat up the amount of RAM you have, especially if you are not using a 64 bit version of MATLAB.


Assuming that your images are named in a sequential way, you could do this:

N = 100
IMAGES = cell(1,N);
FNAMEFMT = 'image_%d.png';

% Load images
for i=1:N
  IMAGES{i} = imread(sprintf(FNAMEFMT, i));
end

% Run code
RESULT = cell(1,N);

for i=1:N
  RESULT{i} = someImageProcessingFunction(IMAGES{i});
end

The cell array RESULT then contains the output for each image.

Be aware that depending on the size of your images, prefetching the images might make you run out of memory.


As many have said, this can get pretty big. Is there a reason you need ALL of these in memory when you are done? Could you write the individual results out as files when you are done with them such that you never have more than the input and output images in memory at a given time?

IMWRITE would be good to get them out of memory when you are done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜