MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

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

Sam Thorpe さんによって質問されました 2019 年 3 月 9 日
最新アクティビティ Image Analyst
さんによって コメントされました 2019 年 3 月 10 日
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 件のコメント

Rik
2019 年 3 月 9 日
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.

サインイン to comment.

2 件の回答

回答者: Rik
2019 年 3 月 9 日

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 件のコメント

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
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.

サインイン to comment.


回答者: Image Analyst
2019 年 3 月 9 日
編集済み: Walter Roberson
2019 年 3 月 9 日

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 件のコメント

Not sure what this means. If you want a function taking in two images then just make one. Use imread twice and then pass them in
image1 = imread(filename1);
image2 = imread(filename2);
results = yourFunction(image1, image2);
and define your function
function whatever = yourFunction(imagea, imageb)
whatever = imabsdiff(imagea, imageb); % Or whatever you want to do.
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.
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

サインイン to comment.



Translated by