Clear Filters
Clear Filters

How can I read in 100 image files from my folder titled "Images"?

93 views (last 30 days)
Hello,
Currently, I have my code working so that I can import an image and convert/ do image analysis with one at a time. I would like to make it so that I can read in 100+ images at a time and store each images data. This is currently how I'm importing the single image file to gather the data, any suggestions how I can pull and save all the data from my images in my "Image" folder?
Image = im2double(imread('image_01800.jpg'));
I = Image(:,:,3);

Accepted Answer

Hassaan
Hassaan on 11 Jan 2024
Edited: Hassaan on 11 Jan 2024
Approach 1:
  1. Use dir or fullfile combined with dir to create a list of all the files in the directory.
  2. Loop through each file, checking if it's an image.
  3. Read each image with imread.
  4. Convert the image with im2double.
  5. Perform your image analysis.
  6. Store the data in an appropriate data structure.
% Define the folder where the images are stored
imageFolder = 'path_to_your_image_folder'; % Replace with the path to your 'Images' folder
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(imageFolder, 'image_*.jpg'); % Adjust the pattern to match your files
theFiles = dir(filePattern);
% Initialize a structure array to hold image data
imageData = struct('filename', {}, 'data', {}, 'analysisResults', {});
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read the image file
Image = im2double(imread(fullFileName));
% Extract the third channel or perform any analysis
I = Image(:,:,3);
% Perform your image analysis here
% For example, let's assume analyzeImage is a function you wrote for analysis
analysisResults = analyzeImage(I); % You need to create the analyzeImage function
% Store the data in the imageData structure
imageData(k).filename = baseFileName;
imageData(k).data = I;
imageData(k).analysisResults = analysisResults;
end
% Now, imageData contains all the information you need for each image
Replace path_to_your_image_folder with the actual path to your 'Images' folder, and replace 'image_*.jpg' with the appropriate pattern if your images have a different naming convention. Also, analyzeImage is a placeholder for whatever image analysis function you might have; you'll need to replace this with your actual analysis code.
Approach 2:
  1. Create an ImageDatastore object pointing to your collection of images.
  2. Use the readimage function to read individual images from the datastore.
  3. Process the images one by one in a loop.
  4. Optionally, you can use the write function to save any modifications back to disk.
% Define the folder where the images are stored
imageFolder = 'path_to_your_image_folder'; % Replace with your folder path
% Create an ImageDatastore object for the images in your folder
imds = imageDatastore(imageFolder, 'FileExtensions', '.jpg', 'LabelSource', 'foldernames');
% Initialize a cell array to hold image data if necessary
imageData = cell(numel(imds.Files), 1);
analysisResults = cell(numel(imds.Files), 1);
% Process each image in the ImageDatastore
for idx = 1:numel(imds.Files)
% Read the image
Image = im2double(readimage(imds, idx));
% Extract the third channel or perform any other operation
I = Image(:,:,3);
% Perform your image analysis function on I
% Replace 'yourImageAnalysisFunction' with your actual function
results = yourImageAnalysisFunction(I);
% Store the data and results
imageData{idx} = I;
analysisResults{idx} = results;
end
% Now imageData and analysisResults have the data for all images
Make sure to replace 'path_to_your_image_folder' with the actual path to your images folder. Also, replace 'yourImageAnalysisFunction' with the actual function you're using to analyze the images.
If the image processing function you're using is compatible with batch processing (i.e., it can accept an array of images), you might use the readall function instead, which reads all the images from the ImageDatastore into memory at once. However, since you mentioned that the entire collection does not fit into memory, you should stick with processing them one by one, as shown above
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (1)

Cris LaPierre
Cris LaPierre on 11 Jan 2024

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!