making the number of images the same

1 次查看(过去 30 天)
azam.e
azam.e 2020-12-5
回答: Deepak 2024-10-8
Hello
I have a folder with subfolders, in subfolders, there are images, but the number of images aren't the same,
by the following code, I read them, but for extracting feature, I have a problem, I have to make the number of images the same.
Please help me.
imw=81;
imh=71;
Mainfolderlist=dir('E:\cropped\*');
totvideofeat=[];
for k=1:length(Mainfolderlist)
if(strcmp(Mainfolderlist(k).name,'.')||strcmp(Mainfolderlist(k).name,'..')),continue; end
subfolderlist=dir(strcat(Mainfolderlist(k).folder,'\',Mainfolderlist(k).name,'\*'));
for l=1:length(subfolderlist)
if(strcmp(subfolderlist(l).name,'.')||strcmp(subfolderlist(l).name,'..')),continue; end
patht=strcat(subfolderlist(l).folder,'\',subfolderlist(l).name,'\*.jpg');
pathi=dir(patht);
videofeat=CalcVidFeat(pathi,imw, imh,param,opticFlow);
totvideofeat=[totvideofeat,videofeat];
end
end

回答(1 个)

Deepak
Deepak 2024-10-8
Hi @azam.e,
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 setfixedNumberOfImages, 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.

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by