how to loop through a folder
古いコメントを表示
Hello
I have a folder containing 1000 images. I want to produce 10 random cropped images from each image in the folder and store them as original_file_name_crop1.png to original_file_name_crop10.png.I'm using the code below. How can I make it loop through all the images?
for j = 1:length(file_list)
for i = 1:10
originalImage = imread(fullfile(dir_in, file_list(j).name));
inputSize = size(originalImage(:,:,1:3));
targetSize = [128 128];
rect = randomWindow2d(inputSize,targetSize);
K = imcrop(originalImage(:,:,1:3), rect);
filename = sprintf('cropped_image_%d.png', i);
imwrite(K, fullfile(dir_out, filename));
end
end
回答 (2 件)
Walter Roberson
2023 年 4 月 8 日
for j = 1:length(file_list)
originalImage = imread(fullfile(dir_in, file_list(j).name));
[this_folder, this_basename, this_ext] = fileparts(originalImage);
for i = 1:10
inputSize = size(originalImage(:,:,1:3));
targetSize = [128 128];
rect = randomWindow2d(inputSize,targetSize);
K = imcrop(originalImage(:,:,1:3), rect);
new_filename = fullfile(this_folder, this_basename + "_crop" + i + this_ext));
imwrite(K, new_filename);
end
end
Image Analyst
2023 年 4 月 8 日
Try this:
% Optional initialization steps
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
inputFolder = pwd; % Whatever.
outputFolder = fullfile(inputFolder, 'Cropped Output Images')
% outputFolder = inputFolder;
if ~isfolder(outputFolder)
% Make output folder if it does not exist yet.
mkdir(outputFolder);
end
filePattern = fullfile(inputFolder, '*.png');
fileList = dir(filePattern)
targetSize = [128, 128];
for k = 1:length(fileList)
inputFullFileName = fullfile(inputFolder, fileList(k).name)
originalImage = imread(inputFullFileName);
subplot(1, 2, 1);
imshow(originalImage);
caption = sprintf('Image #%d of %d : %s', k, length(fileList), fileList(k).name)
title(caption, 'Interpreter','none')
inputSize = size(originalImage);
% Skip images that are too small.
if inputSize(1) < targetSize(1) || inputSize(2) < targetSize(2)
fprintf('Skipping %s because it is too small.\n', fileList(k).name)
continue;
end
% Make an output folder for the 10 image
for k2 = 1 : 10
% Create randomly located rectangle.
rect = randomWindow2d(inputSize,targetSize);
% Crop out that rectangle to its own image.
croppedOutputImage = imcrop(originalImage, rect);
subplot(1, 2, 2);
imshow(croppedOutputImage);
drawnow;
% Get the output filename.
[f, baseFileNameNoExt, etc] = fileparts(fileList(k).name);
baseOutputFileName = sprintf('%s_crop_%2.2d.png', baseFileNameNoExt, k2);
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
% Save out the image to a folder for the 10 images.
fprintf(' Saving "%s".\n', fullOutputFileName);
imwrite(croppedOutputImage, fullOutputFileName);
end
end
%=========================================================================
function rect = randomWindow2d(inputSize, targetSize)
rows = inputSize(1);
columns = inputSize(2);
topRow = randi(rows - targetSize(1));
leftCol = randi(columns - targetSize(2));
width = targetSize(2);
height = targetSize(1);
rect = [leftCol, topRow, width, height];
end
カテゴリ
ヘルプ センター および File Exchange で Neighborhood and Block Processing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!