Is it possible to store jpg images in an array to use in a function?
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
답변 (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
Image Analyst
2019년 3월 10일
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.
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
Image Analyst
2019년 3월 9일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!