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

1 view (last 30 days)
Hi.
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?
thanks
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
k=rgb2gray(i)
l=rgb2gray(j)
imshowpair(k,l,'montage')
totalintensityvalueG=sum(sum(k))
totalintensityvalueR=sum(sum(l))
format rat
ratio=totalintensityvalueG/totalintensityvalueR
  1 Comment
Rik
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
Rik on 9 Mar 2019
To give an example of how you could do it with a cell array:
images=cell(2,10);
ratios=zeros(1,size(images,2));
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
totalintensityvalueG=sum(k(:));
totalintensityvalueR=sum(l(:));
ratios(n)=totalintensityvalueG/totalintensityvalueR;
end
format rat
ratios
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.
  2 Comments
Sam Thorpe
Sam Thorpe on 10 Mar 2019
I can't thank you enough Rik. I have got the function to generate the green to red ratios for each data pair. I have one more question if you have any idea. Is it possible to call the function in a script? So when I set x to 1 it uses the function to perform the process on the first set of images, then using a while or for loop, it repeats the process for the remaining data sets?
I tried starting with the following code but I does not like the fact I am trying to call the function up and just returns an error message
x=1 %number of first data set
myfun1(x) %function for image processing
while x>10 %while loop
x=x+1 %next value of x
end
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().
  4 Comments
Sam Thorpe
Sam Thorpe on 9 Mar 2019
I cant go down this path as once the function has been called in the script, it has to repeat the process on the other image sets using a loop. I need each image to have an input value assigned to it so I can call them up in a loop. So the first loop would call image one red and image one green, process the images and store the results. Then repeat for image set two and so on.
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 = {fileInfo.name}
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;
end

Sign in to comment.

Categories

Find more on Images 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!