Resize the images without deforming them

조회 수: 2 (최근 30일)
Abdussalam Elhanashi
Abdussalam Elhanashi 2020년 10월 26일
댓글: Walter Roberson 2020년 10월 30일
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
Abdussalam Elhanashi
Abdussalam Elhanashi 2020년 10월 26일
Hi Mathieu NOE
When I resize the images, i got distortion with this code
looking for your advice
Mathieu NOE
Mathieu NOE 2020년 10월 26일
hello
could you send me one picture where the problem arise ?

댓글을 달려면 로그인하십시오.

채택된 답변

drummer
drummer 2020년 10월 26일
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
Abdussalam Elhanashi
Abdussalam Elhanashi 2020년 10월 29일
편집: Walter Roberson 2020년 10월 30일
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 2020년 10월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2020년 10월 26일
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 2020년 10월 26일
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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by