필터 지우기
필터 지우기

read image from different folders?

조회 수: 20 (최근 30일)
Marry M
Marry M 2016년 2월 2일
댓글: Image Analyst 2021년 3월 25일
hi all, i have 20 folders which named with dates (i.e. 1.1.2016, 2.1.2016.....20.1.2016) and each folder has two images, please let me know how can i read each image. it is important for me to read images respectively. for example: read image 1 in day one then image 2 day one, image 1 day 2.....

채택된 답변

Stephen23
Stephen23 2016년 2월 2일
편집: Stephen23 2016년 2월 3일
This should get you started:
S = dir('*.2016');
D = [S.isdir];
N = {S(D).name};
V = datenum(N,'dd.mm.yyyy');
[~,X] = sort(V);
for m = X(:).'
S = dir(fullfile(N{m},'*.jpg'));
F = {S.name};
for n = 1:numel(F)
str = fullfile(N{m},F{n});
X = imread(str);
... do something with image data X
end
end
  댓글 수: 9
Stephen23
Stephen23 2016년 2월 4일
편집: Stephen23 2016년 2월 4일
This is my third and final attempt to get information on the image files:
  • what are their file extensions?
  • please show us a screenshot of inside one of the directories containing images.
I have clearly asked three times for information on the image files, in particular on their file extensions and file formats. Without this information I cannot help you any more. You don't have to use MATLAB to get this information.
I have no idea how you got that last workspace, so it does not tell my anything. Note that you do not need to run my code from inside those subdirectories, just from their root/parent directory. Running ls would be nice though, but that seems to be a disappearing dream.
Marry M
Marry M 2016년 2월 4일
thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 2월 4일
I know you already accepted a solution, but here's the code I usually post for questions like this, for whatever it's worth. Change the filepattern to get the file types you want, if they're not jpg:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
% % Get m files.
% filePattern = sprintf('%s/*.m', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Now we have a list of all files in this folder.
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing file %s\n', fullFileName);
figure; % Comment out this line if you want them all in the same axes.
imshow(fullFileName);
drawnow;
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end
By the way, if you're doing image analysis, you should not use jpg if at all possible because of JPG artifact corruption.
  댓글 수: 6
lydie Nsangou
lydie Nsangou 2021년 3월 25일
thanks a lot it was very helpful
@Image Analyst please is it possible to store all the images in a list or array? later on i would like to pick just a few a them .
Image Analyst
Image Analyst 2021년 3월 25일
Yes, but you may run out of memory:
rgbImage{k, f} = imread(fullFileName); % Store in cell array
imshow(rgbImage{k, f});

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by