MATLAB Answers

Is it possible to store jpg images in an array to use in a function?

1 view (last 30 days)
Sam Thorpe
Sam Thorpe on 9 Mar 2019
Commented: Image Analyst on 10 Mar 2019
I am currently working on mini project where I have to compare images from a drug screen. I currently have 2 folders, one with untested images and one with the drug administered. Each folder has 20 images, ten in green and 10 in red. I currently have made the code where I manually input the filename and achieve the desired result for one set of images. What I need to be able to do is possibly call the folders up and store the images in 2 seperate arrays, then I can just call the number of the 2 images in the array when using the function. Is this possible?
the current code is
i=imread('group1 g 01.jpg'); %selects green image (n) from list
j=imread('group1 r 01.jpg'); %selects red image (n) from list
format rat
  1 Comment
Rik on 9 Mar 2019
I don't think there is a fundamental reason why you wouldn't be able to do this with a cell array.
Depending on the size of your images, those 40 images might exceed the array size that fits in your memory (the limit is typically much smaller than you RAM size, as an array needs a contiguous block of memory). If that is the case you might consider a datastore object.

Sign in to comment.

Answers (2)

Rik on 9 Mar 2019
To give an example of how you could do it with a cell array:
for n=1:size(images,2)
images{1,n}=imread(sprintf('group1 g %2d.jpg',n)); %selects green image (n) from list
images{2,n}=imread(sprintf('group1 r %2d.jpg',n)); %selects red image (n) from list
k=double(rgb2gray(images{1,n}));%convert to double to prevent overflow of the uint8
l=double(rgb2gray(images{2,n}));%convert to double to prevent overflow of the uint8
format rat
If you only need the ratios, you don't need to store the images themselves. If you don't understand my code, read the documentation pages for them. One of the major advantages over competitors is the good documentation.
Image Analyst
Image Analyst on 10 Mar 2019
Yes, it is possible to call a function in a script. Just make sure the script is at the top of the m-file, and the function closes with an "end" statement.
It seems like the original question, and the question I'm now answering below about calling a function and keeping track of what images were used, and this question with Rik are all different questions.

Sign in to comment.

Image Analyst
Image Analyst on 9 Mar 2019
Edited: Walter Roberson on 9 Mar 2019
See the FAQ: Click Here
to process a sequence of files. If you want to process more than one folder in the loop, use ** in dir(), or use imageDatastore().
Image Analyst
Image Analyst on 9 Mar 2019
Then you can read all the filenames of the images (not the images themselves because you'd likely run out of memory). Try this:
filenamePattern = '**\*.png';
fileInfo = dir(filenamePattern);
allFileNames = {}
allFolders = {fileInfo.folder}
fileHasBeenUsed = false(1, length(allFileNames));
for k = 1 : length(allFileNames)
fullFileName = fullfile(allFolders{k}, allFileNames{k});
fprintf('Now Processing %s\n', fullFileName);
% Call some function with this filename, that will do something.
% Poster says it needs a list of which filenames have been used so far.
yourFunction(fullFileName, allFileNames, fileHasBeenUsed);
% Mark this file as having been used
fileHasBeenUsed(k) = true;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!