Batch processing images for downsampling

Hi,
I would like to perform downsampling for like over 700 images. It is pretty time-consuming to do it one by one. How can I achieve it at one time?
Thanks a lot.

 Accepted Answer

hello
see example below - adapt to your own needs
%% Initalize the data
dataDir= fullfile(pwd); % select appropriate directory
exts = {'.jpg','.png','.tif'}; % choose valid file extensions like {'.jpg', '.png'}
resize_size = 100; % pixels size for output img
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions',exts,'LabelSource','foldernames');
countEachLabel(imds);
numImages = numel(imds.Files);
for i = 1:numImages
img = readimage(imds, i);
[m,n,p] = size(img);
% compute scale factor (same on both dimensions)
scale_factor = min(resize_size/m,resize_size/n);
img3= imresize(img, scale_factor);
figure(i),
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %output image stored in cell
end

8 Comments

another example
% select appropriate options below and adapt code to your specific needs ...
% files = dir('*.tif'); % select all tiff files in current directory
files = dir('handheld*.tif'); % select only tiff files with "handheld" in the filename
% resize options (sacle factor or pixels)
scale_factor = 0.5;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 120;
y_dim = 80;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, scale_factor);
% select (crop)
H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.tiff'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Many thanks. I am choosing the second one. Is it ok if I use .png format for the second script?
Cemre
Cemre on 13 Jun 2023
Edited: Cemre on 13 Jun 2023
Hello again, I changed .tiff to .png format. Also, my scale factor is 0.2. I didn't want to select a portion from the images so I just wrote the original size which is 140x140. I commented out crop section down there. After all these adjustments, I could run the code but only one image was downsampled. Why has that happened? Thanks a lot.
could you please share your final code ?
% select appropriate options below and adapt code to your specific needs ...
files = dir('*.png'); % select all tiff files in current directory
%files = dir('handheld*.tif'); % select only tiff files with "handheld" in the filename
% resize options (sacle factor or pixels)
scale_factor = 0.2;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 140;
y_dim = 140;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, scale_factor);
% select (crop)
%H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.tiff'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Also, size of the output image is 560x420. I would like it to be 140x140 and with bicubic interpolation as like in this example but it also gives me a different size.
% Load the original image
origImage = imread('Planet_20221104_1667.png');
% Set the desired scale factor to achieve 10m resolution
scaleFactor = 0.20;
% Resize the image using bicubic interpolation
downsampledImage = imresize(origImage, scaleFactor, 'bicubic');
% Save the downsampled image to a new file
imwrite(downsampledImage, 'Planet_20221104_0364_1.png');
many thanks for your helps.
here you are my friend :
% select appropriate options below and adapt code to your specific needs ...
files = dir('*.png'); % select all png files in current directory
% resize options (sacle factor or pixels)
x_dim = 140;
y_dim = 140;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, [x_dim y_dim], 'bicubic');
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.png'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Many thanks. Much appreciated.
as always, my pleasure !

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 13 Jun 2023

Commented:

on 14 Jun 2023

Community Treasure Hunt

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

Start Hunting!