Upload multiple images to MATLAB

Here is the desired workflow:

  • I want to upload 100 images to the MATLAB workspace
  • Run a bunch of code on 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 that preserves code output for 1-100 images.

How can i do this?

+7
image-processing matlab
source share
3 answers

If you know the name of the directory in which they are located, or if you are connected to this directory, use dir to get a list of image names.

Now it's just a for loop to load images. Store images in an array of cells. For example...

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

CAUTION that these 100 images take up too much memory. For example, for a single 1Kx1K image, 3 megabytes will be required for storage if it is uint8 RGB. This may not seem like a huge amount.

But then 100 of these images will require 300 MB of RAM. The real problem arises if your operations with these images turn them into double ones, then they will now occupy 2.4 GB of memory. This quickly eats up the amount of RAM, especially if you are not using the 64-bit version of MATLAB.

+8
source share

Assuming your images are named sequentially, you can 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 

Then the array of RESULT cells contains the output for each image.

Remember that depending on the size of your images, pre-fetching the images may result in memory exiting.

+4
source share

As many have said, this can become quite large. Is there a reason you need all this in mind when you're done? Could you write the individual results as files when you are done with them so that you never have more input and output images in memory at the moment?

IMWRITE would be nice to get rid of them when you are done.

+4
source share

All Articles