How can you resize the image without deforming the image?

22 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);
imshow(grayImage);
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);
imshow(newImage);
axis on;
% axis equal
  5 Comments
Image Analyst
Image Analyst on 21 Nov 2020
You made several errors:
  1. No comments, which made it a little difficult to figure out what you were doing.
  2. Using input as the name of your image. Don't do that because it's already a built-in function.
  3. Not using a third argument of 3 to make your sidePanel an RGB image.
  4. Using comman instead of semicolon to stitch the side panels on vertically.
Try this corrected code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
echo off;
% Read in input image.
% input=imread(['C:Users\user\Documents\Folder\File.png']);
rgbImage = imread('peppers.png');
subplot(1, 2, 1);
imshow(rgbImage)
axis('on', 'image');
title('Original Image', 'FontSize', 15);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Resize the image to have be exactly 224 columns wide.
rgbImage = imresize(rgbImage, 224/columns);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Find width of band above and below the middle 224 rows of the image.
sidePanelCols = round((224 - rows)/2)
% Make a gray scale band of 200 that is that size.
% Make sure you use a z height of 3 to make the image RGB instead of gray scale.
sidePanel = 200 * ones(sidePanelCols, columns, 3, 'uint8');
% Construct new image using semicolon to stitch vertically, not comma which would stitch horizontally.
newImage = [sidePanel; rgbImage; sidePanel];
subplot(1, 2, 2);
imshow(newImage);
axis('on', 'image');
title('Resized and Padded', 'FontSize', 15);
impixelinfo
If this is for AlexNet, keep in mind ALexNet is 227x227, not 224x224. From the help documentation for AlexNet:
Description
AlexNet is a convolutional neural network that is 8 layers deep. You can load a pretrained version of the network trained on more than a million images from the ImageNet database [1]. The pretrained network can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals. As a result, the network has learned rich feature representations for a wide range of images. The network has an image input size of 227-by-227.
Pratiwi
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!