Resize the images without deforming them

4 vues (au cours des 30 derniers jours)
Abdussalam Elhanashi
Abdussalam Elhanashi le 26 Oct 2020
Commenté : Walter Roberson le 30 Oct 2020
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 commentaires
Abdussalam Elhanashi
Abdussalam Elhanashi le 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 le 26 Oct 2020
hello
could you send me one picture where the problem arise ?

Connectez-vous pour commenter.

Réponse acceptée

drummer
drummer le 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 commentaires
Abdussalam Elhanashi
Abdussalam Elhanashi le 29 Oct 2020
Modifié(e) : Walter Roberson le 30 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 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.

Mathieu NOE
Mathieu NOE le 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

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by