How to select every 54th image in the folder?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Awais Saeed
on 17 Sep 2021
Edited: Awais Saeed
on 17 Sep 2021
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);
struct2table(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);
struct2table(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;
end
end
% 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 Comments
Awais Saeed
on 17 Sep 2021
- I was just not providing the full path of image in imread(). I have updated the code.
- You said there are two folders namely A and B. That is why I used C:\Users\Awais\Documents\MATLAB\A and C:\Users\Awais\Documents\MATLAB\B
- Now to your question how to pick 1 image from folder A and then pick 10 images from folder B one by one? All images are in Acell cell array. Row # 1 has a1, b1, b55, b109, b163, b217, b271, b325, b379, b433, b487 in it. Same goes for remaining rows. Just loop through them as following:
for row = 1:1:size(Acell,1)
A = Acell{row,1}; % to pick a1 in first iteration
for col = 2:1:size(Acell,2)
B = Acell{row,col};
% find npcr value. Call it x
npcr_result(row,col-1) = x
% % find uaci value. Call it y
uaci_result(row,col-1) = y
end
end
% At the end, you will have 10 values for a1 in row # 1
% 10 values for a2 in row # 2 and so on
More Answers (1)
Jan
on 17 Sep 2021
Edited: Jan
on 17 Sep 2021
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};
...
end
end
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
end
end
0 Comments
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!