How to select every 54th image in the folder?

조회 수: 1(최근 30일)
Ammy 2021년 9월 17일
댓글: Ammy 2021년 9월 18일
Suppose we have two folders A and B , both contain images.
Let folder A have 54 images:a1,a2,...,a54
folder B have 540 images: b1,b2,...,b540.
I want to select 10 images in B for each image in A, in the following way
for a1 select b1,b55, b109, b163, b217, b271, b325, b379, b433, b487
for a2 select b2,b56, b110, b164, b218, b272, b326, b380, b434, b488
for a54 select b54, b108, b162, b216, b270, b324, b378, b432, b486,b540
that is select every 54th entry.
Now I want to find uaci and npcr such that for each 'image a' I want to use 10 'image b', so that corresponding to each 'image a' I will get 10 uaci and npcr, and the function is attached.

채택된 답변

Awais Saeed
Awais Saeed 2021년 9월 17일
편집: Awais Saeed 2021년 9월 17일
I modified the code that I wrote for your last question as it might be easy to follow the same structure.
% path to the folder A that contains .png files
Folder_A_location = 'C:\Users\Awais\Documents\MATLAB\A';
% Get paths of <ALL> files in folder A
AFiles = fullfile(Folder_A_location, '*.png');
AFiles = dir(AFiles);
% path to the folder B that contains .png files
Folder_B_location = 'C:\Users\Awais\Documents\MATLAB\B';
% Get paths of <ALL> files in folder B
BFiles = fullfile(Folder_B_location, '*.png');
BFiles = dir(BFiles);
% a1, b1, b55, b109, b163, b217, b271, b325, b379, b433, b487 are in row # 1 of Acell
% a2, b2, b56, b110, b164, b218, b272, b326, b380, b434, b488 are in row # 2 of Acell
% and so on
for AfileNum = 1:1:length(AFiles)
AfileName = AFiles(AfileNum).name;
fprintf('Loaded file # %d, name = %s \n',AfileNum, AfileName)
col = 1;
% read image (Must provide full path)
AFileLoaded = imread([Folder_A_location '\' AfileName]);
Acell{AfileNum,col} = AFileLoaded;
for BfileNum = AfileNum:54:length(BFiles)
col = col + 1;
BfileName = BFiles(BfileNum).name;
fprintf('Loaded file # %d, name = %s \n',BfileNum, BfileName)
% read image (Must provide full path)
BFileLoaded = imread([Folder_B_location '\' BfileName]);
Acell{AfileNum,col} = BFileLoaded;
% Now all images are in Acell{} according to your selection criteria.
% Loop through Acell{} and perform your operations
I did not check the code as I do not have access to MATLAB right now. In case of any error, I hope you can find a solution. I used fprintf() to see what is being loaded for easy debugging. You can remove those as well.
  댓글 수: 2
Ammy 2021년 9월 18일
Thank you very much.

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

추가 답변(1개)

Jan 2021년 9월 17일
편집: Jan 2021년 9월 17일
A = 'D:\Your\A';
B = 'D:\Your\B';
AList = dir(fullfile(A, 'a*.jpg'));
BList = dir(fullfile(B, 'b*.jpg'));
nB = numel(BList);
for iA = 1:numel(AList)
imageA = fullfile(A, AList(iA).name);
indexB = iA:54:nB;
imageBList = fullfile(B, {BList(indexB).name});
for iB = 1:imageBList
imageB = imageBList{iB};
If you are sure that all file names a1 to a54 and b1 to b540 are existing, you can create the file names automatically also:
A = 'D:\Your\A';
B = 'D:\Your\B';
for iA = 1:54
imageA = fullfile(A, sprintf('a%d.jpg', iA));
for iB = iA:54:540
imageB = fullfile(B, sprintf('b%d.jpg', iB));
... process imageA and imageB now

Community Treasure Hunt

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

Start Hunting!

Translated by