From my understanding, you have subfolders within a folder, and those subfolders contain images for feature extraction. But the number of images in subfolders is not the same, and feature extraction requires the same number of images.
To make the number of images equal in each subfolder, we can use truncation and padding techniques. We can initially set “fixedNumberOfImages”, as the number of images required in each subfolder. Then, we can truncate the extra images in a subfolder, and we can also pad (replicate) the images if a subfolder contains a smaller number of images than required.
To truncate the number of images, we can use array indexing and set the “pathi” array size to “fixedNumberOfImages”. Also, to pad the images, we can use “repmat” function in MATLAB to repeat the array elements until the required array size is reached.
Here is the complete MATLAB code that addresses this task:
imw = 81;
imh = 71;
fixedNumImages = 10; % Set the desired number of images per subfolder
Mainfolderlist = dir('E:\cropped\*');
totvideofeat = [];
for k = 1:length(Mainfolderlist)
if strcmp(Mainfolderlist(k).name, '.') || strcmp(Mainfolderlist(k).name, '..')
continue;
end
subfolderlist = dir(fullfile(Mainfolderlist(k).folder, Mainfolderlist(k).name, '*'));
for l = 1:length(subfolderlist)
if strcmp(subfolderlist(l).name, '.') || strcmp(subfolderlist(l).name, '..')
continue;
end
patht = fullfile(subfolderlist(l).folder, subfolderlist(l).name, '*.jpg');
pathi = dir(patht);
% Determine the number of images to process
numImages = length(pathi);
if numImages >= fixedNumImages
% Truncate the list to the desired number of images
pathi = pathi(1:fixedNumImages);
else
% Pad the list by repeating images
repeatFactor = ceil(fixedNumImages / numImages);
pathi = repmat(pathi, repeatFactor, 1);
pathi = pathi(1:fixedNumImages); % Ensure we have exactly fixedNumImages
end
videofeat = CalcVidFeat(pathi, imw, imh, param, opticFlow);
totvideofeat = [totvideofeat, videofeat];
end
end
Attaching the documentation of “array indexing” and “repmat” in MATLAB for reference:
I trust this will address the problem.