MATLAB Answers

Resize the images without deforming them

3 views (last 30 days)
Hi
I want to know how to resize images 200 x 200 to 100 x 100 without deforming the images in this code
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
% img2= imshow(img, 'InitialMagnification', 800);
img3= imresize(img, [100 100]);
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %#ok<SAGROW>
end

  3 Comments

Mathieu NOE
Mathieu NOE on 26 Oct 2020
hello
if your input images are square, then your code is OK. the output image will not be distorted.
if you have non square input image, you should use a scale factor instead of [100 100] output size, because this will create distorsion
Abdussalam Elhanashi
Abdussalam Elhanashi on 26 Oct 2020
Hi Mathieu NOE
When I resize the images, i got distortion with this code
looking for your advice
Mathieu NOE
Mathieu NOE on 26 Oct 2020
hello
could you send me one picture where the problem arise ?

Sign in to comment.

Accepted Answer

drummer
drummer on 26 Oct 2020
you're trying to resize 200x200 to 100x100 images.
scale = 0.5;
J = imresize(I,scale); % using scale rather than your final output image didn't work as well?

  3 Comments

Walter Roberson
Walter Roberson on 29 Oct 2020
Reminder: this code will distort the image in most cases (for example a constant image would not be distorted.)
This is a consequence of "the pigeon-hole principle": if you have more than N/4 pieces of "information" in an image, than when you shrink the image from NxN to (N/2) x (N/2), one or more of the pieces of information will have to be lost.
Abdussalam Elhanashi
Abdussalam Elhanashi on 29 Oct 2020
Hi Walter That is true . I tried this but the image is distorted
I found this example, Does it work or make benefits for image rescaling
Walter Roberson
Walter Roberson on 30 Oct 2020
Every resizing to smaller will distort some image; if your requirement is for there to not be distortion then you cannot resize the image (unless you know for some reason that the image has lower information content so no information would be lost.)
For example, consider the simple grayscale image
0 13
238 67
that is to be scaled 50%. The output can only be a single byte, and a single byte cannot represent all four values and a single byte cannot in any way express whatever brightness gradient that is.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 26 Oct 2020
Resizing images always introduces distortions.
In some cases the distortions are acceptable. In other cases, the distortions are not acceptable, and you need to reconsider what you are doing.
For example,
10102020
01010202
30304040
03030404
If you resize this by 50% starting with the upper left:
1122
3344
Notice no 0's: the checkerboard pattern has been lost.
If you start from the upper right:
0000
0000
Notice no 1's, 2's, 3's, 4's.
Suppose you mean() each 2 x 2 discrete block and round(), starting from the upper left:
1111
2222
The first two 1's are round(2/4) -> 1, and the second two 1's are round(4/4) -> 1... clearly this is distorted relative to the original.
If you need to keep the "character" of being checker-board then since the output is only 2 x 4:
1020
0304
maybe?? Still obviously distorted compared to the original.
Resize to smaller must lose detail.

  0 Comments

Sign in to comment.


Mathieu NOE
Mathieu NOE on 26 Oct 2020
so I modified your code so that the shortest dimension of your input image will be resized to 100 pixels
If your input image is rectangular (non square) the ratio will remain the same (otherwise if you force it to 100 x 100 pixels it will of course create a distorsion - longer side will be compressed.
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
resize_size = 100; % pixels size for output img
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
% img2= imshow(img, 'InitialMagnification', 800);
[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);
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %#ok<SAGROW>
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by