How to select images from nested folder?

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?

 采纳的回答

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 个评论

Hello sir Thanks for your help. i really appreciate this. The first code is good, but what if i want the images names be like 1,2,3,...30 in the folder C? Also this is not a limited case for two or three folders
i have a lot of other folders in the same manner and from every sub folder the 10th image will be selected and the code will name it as 1,2,3,4...30 then it may go into the second folder after AAA , suppose BBB and inside the BBB there is folder B and iside this B there is also 300 images,(the images can be less or greater in number ) the code may again select the 10th image from this folder and wirte into into a new folder (can be created by the code itself) and name it 31,32,33,34.....60 and the process goes upto the folder ZZZ inside this a sub folder Z and inside this 300 images and select the 10th...
Thank you..
So far i have tried this code.. but it works in wrong direction. It searches for images inside the destination folder (off course can not find any image).
t=1;
% alphas are names of the folders
alphas = {'Ain';'Alif';'Bay';'Byay';'Chay';'Dal';'Ddal';'Dwad';'Fay';'Ghaf';'Ghain';'Hamza';'Hay';'Hhhay';'Kaf';'Khay';'Lam';'Meem';'Noon';'Noonghuna';
'Pay';'Qaf';'Ray';'Say';'Seen';'Sheen';'Ssay';'Swad';'Tay';'Touy';'TTay';'Wow';'Yay';'Zal';'Zay';'Zouy'}
for i=1:36 % number of folders
for j=1:1 % this is the person 1 folder named with 1 so that's why the j goes from 1:1
jj=sprintf('%d',j);
name=alphas(i);
name1 = char(name);
mkdir(jj, name1);
n=sprintf('%d',j);
mn=strcat(name,'00',n);
% number of sub folder inside each of the 36 folders..k=1 means each
% folder is having one folder inside means for folder Ain there will
% is a folder named Ain001 (1)
for k=1:1
d=sprintf('%d',k);
sf=strcat(mn,{' '},'(',d,')');
sf1=char(sf);
ffile=fullfile(jj,name1);
mkdir(ffile,sf1);
bc1=strcat(sf1,'Selected'); % this should be the new folder (Concat the Selected with the name of the folder)genrated INSIDE Ain folder
% (Remember Ain is also having the Ain001 (1)folder in it)
% From Ain001 (1) I need to select image 10,20,30,40.... means
% every 10th image and write those images inside Ain001 (1)Selected
bc1=char(bc1);
bc=strcat(ffile,'\',bc1);
bc=char(bc);
mkdir(bc);
fullname2 = fullfile(ffile,'\',sf1);
fullname4=fullfile(ffile,'\',ffsc1);
fullname5=fullfile(ffile,'\',bc1);
numf=dir([fullname5 '/*.jpg']);
numfs=size(numf,1);
nums=numfs/10;
for ii=1:nums
nn=sprintf('%d',ii*10);
filename1 = [sprintf('%d',ii) '.jpg'];
path = fullfile(fullname5,'',filename1);
im=imread(path);
nn=sprintf('%d',t);
filename1=strcat(nn,'.jpg');
pathsel=fullfile(fullname2,filename1);
imwrite(im,pathsel);
t=t+1;
end
end
end
end
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?
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 个)

Have you looked at imageDatastore()? Or dir() using two asterisks as the file pattern?

Community Treasure Hunt

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

Start Hunting!

Translated by