How to select images from nested folder?

조회 수: 1 (최근 30일)
Farman Shah
Farman Shah 2019년 7월 16일
댓글: Farman Shah 2019년 7월 20일
I have a folder named AAA. Inside folder AAA there is another folder name A, inside A there is another folder B. folder B consist of 300 images. I want to get every 10th number image from folder B ( It will be 30 images) and place these images in another folder C. I need a Matlab code for this Scenario. Any help?

채택된 답변

Joel Handy
Joel Handy 2019년 7월 16일
편집: Joel Handy 2019년 7월 16일
Something like this should work. You will possibly need to add code to appropriately sort your list of images. I've marked where you would do it.
imageFolder = uigetdir('', 'Select Image Folder');
destinationFolder = uigetdir(imageFolder, 'Select Destination Folder');
contents = dir(imageFolder);
fileList = contents(~[contents.isdir]);
% Sort list as needed here
filesToMove = fileList(1:10:end);
fromPaths = fullfile(imageFolder,{filesToMove.name});
toPaths = fullfile(destinationFolder,{filesToMove.name});
for fileIdx = 1:numel(fromPaths)
copyfile(fromPaths{fileIdx}, toPaths{fileIdx})
end
Also I'm note sure the specific relevance of folders AAA and A. If AAA, A, and B are all known folders and you want to create C based on them, you could replace the uigetdir code with something like
imageFolder = fullfile(AAA,A,B);
destinationFolder = fullfile(AAA,A,C);
if ~exist(destinationFolder, 'dir')
mkdir(festinationFolder);
end
  댓글 수: 4
Joel Handy
Joel Handy 2019년 7월 18일
I'm having a little trouble following this. There is a lot of extra code. For example:
jj=sprintf('%d',j);
name=alphas(i);
name1 = char(name);
mkdir(jj, name1);
could just be
% Make a folder '1' inside user folder
mkdir(num2str(j),alphas{i});
Another problem I think you're having is that your inputs to mkdir appear to be backwords. The format mkdir is mkdir(parent directory, new folder) so mkdir('C:\MyExistingFolder', 'NewFolder') would create 'C:\MyExistingFolder\NewFolder';
You could also just do mkdir(['C:\MyExistingFolder' filesep 'NewFolder']) or mkdir(fullfile('C:\MyExistingFolder', 'NewFolder').
Why are you using imread and imwrite instead of copyfile to move the image files? Are you actually doing some processing on these images in your real application?
Farman Shah
Farman Shah 2019년 7월 20일
Yeah I was doing a project and it is done now. Thank you
your code was good. For those others comming on this thread, it is helpfull.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 7월 18일
Have you looked at imageDatastore()? Or dir() using two asterisks as the file pattern?

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by