change size of images

1 次查看(过去 30 天)
Jyoti Nautiyal
Jyoti Nautiyal 2021-3-26
I have a database of thousands of images of different sizes like 45x78, 67x89, 83x99 etc. how to make all the images of same size?

采纳的回答

KSSV
KSSV 2021-3-26
Read about imresize. Run a loop for each image and change them and save them if you want using imwrite.

更多回答(1 个)

Image Analyst
Image Analyst 2021-3-27
@Jyoti Nautiyal, try this full demo. If it works, could you Vote for the Answer:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% FAQ reference:
% https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
% Specify the folder where the files live.
inputFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(inputFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', inputFolder);
uiwait(warndlg(errorMessage));
inputFolder = uigetdir(); % Ask for a new one.
if inputFolder == 0
% User clicked Cancel
return;
end
end
outputFolder = fullfile(inputFolder, 'Resized');
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Specify how many rows and columns you want the output image to be.
desiredRows = 60;
desiredColumns = 90;
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(inputFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
subplot(2, 1, 1);
imshow(imageArray); % Display image.
axis('on', 'image');
caption = sprintf('Original "%s"', baseFileName);
title(caption, 'Interpreter', 'none');
% Resize the image
resizedImageArray = imresize(imageArray, [desiredRows, desiredColumns]);
subplot(2, 1, 2);
imshow(resizedImageArray); % Display image.
axis('on', 'image');
caption = sprintf('Resized "%s"', baseFileName);
title(caption, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it to the output folder
outputFullFileName = fullfile(outputFolder, baseFileName);
imwrite(resizedImageArray, outputFullFileName);
end
fprintf('Done running %s.m\n', mfilename);

Community Treasure Hunt

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

Start Hunting!

Translated by