MATLAB Answers

How can you resize the image without deforming the image?

134 views (last 30 days)
Can you please provide a code to resize an image without making it look different. Example I have a 2000 x 1500 pixel image size. I want the image to fit in an 1024 x 1024 size without reshaping it.
  1 Comment
John D'Errico
John D'Errico on 26 Dec 2017
Sorry. You cannot "resize" an image to a different shape without changing the shape. Why does that seem a truism to me? Code cannot do magic. No matter what you do, you will lose some portion of the image, or you will need to fill the image out to the new boundary with your choice of fill pixels.
Nothing stops you from arbitrarily resizing the image while also doing a crop. So resize it first to be too large, then crop away what you don't want. WTP? Or, just interpolate the image on a restricted set of points, so the result has the shape that you want, sampled from the desired area. Again, WTP? Or interpolate the image to the smaller size, but using 0 or 1, or NaN in the regions that would be extrapolated. Again, WTP? Why not make an effort, then show what you tried if it does not work, and ask for help?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Dec 2017
Try this:
% Prepare an input image for demo.
grayImage = imread('moon.tif');
grayImage = imresize(grayImage, [2000, 1500]);
subplot(1, 2, 1);
axis on;
% Now start....
% Resize gray image.
[rows, columns, numberOfColorChannels] = size(grayImage)
grayImage = imresize(grayImage, 1024/rows);
[rows, columns, numberOfColorChannels] = size(grayImage)
% Prepare side panels
sidePanelCols = (1024 - columns)/2
sidePanel = 200 * ones(rows, sidePanelCols, 'uint8');
% Construct new image
newImage = [sidePanel, grayImage, sidePanel];
subplot(1, 2, 2);
axis on;
% axis equal
Pratiwi on 23 Nov 2020
wow, it works! Thanks for your clear answer.
this is for VGG16 input size. If I don't make a mistake, the require input size is 224x224. CMIIW

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!