imresize
Resize image
Syntax
Description
returns image B
= imresize(A
,scale
)B
that is scale
times
the size of image A
. The input image A
can be a grayscale, RGB, binary, or categorical image.
If A
has more than two dimensions, then
imresize
only resizes the first two dimensions. If
scale
is between 0 and 1, then B
is smaller than A
. If scale
is greater
than 1, then B
is larger than A
. By
default, imresize
uses bicubic interpolation.
returns image B
= imresize(A
,[numrows numcols]
)B
that has the number of rows and columns
specified by the twoelement vector [numrows numcols]
.
___ = imresize(___,
returns the resized image where namevalue arguments control various aspects of
the resizing operation. Specify namevalue arguments after all other input
arguments.Name,Value
)
Examples
Shrink Image By Factor of Two Using Default Interpolation Method
Load image into the workspace.
I = imread('ngc6543a.jpg');
Shrink the image by a factor of two.
J = imresize(I, 0.5);
Display the original image and the resized image.
figure, imshow(I), figure, imshow(J)
Shrink Image Using NearestNeighbor Interpolation
Load an image into the workspace.
I = imread('ngc6543a.jpg');
Shrink the image to 40% of the original size using nearestneighbor interpolation. This is the fastest method, but it has the lowest quality.
J = imresize(I,0.4,'nearest');
Display the original image and the resized image.
imshow(I)
title('Original Image')
imshow(J)
title('Resized Image Using Nearest Neighbor Interpolation')
Resize RGB Image Specifying Size of Output Image
Read an RGB image into the workspace.
RGB = imread('peppers.png');
Resize the RGB image to have 64 rows. imresize
calculates the number of columns automatically.
RGB2 = imresize(RGB,[64 NaN]);
Get the size of the resized image.
sz = size(RGB2)
sz = 1×3
64 86 3
Display the original image and the resized image.
imshow(RGB)
title("Original Image")
imshow(RGB2)
title("Resized Image with 64 Rows")
Resize Indexed Image
Read an indexed image into the workspace.
[X,map] = imread("corn.tif");
Increase the size of the indexed image by 50%.
[Y,newmap] = imresize(X,map,1.5);
Display the original image and the resized image.
imshow(X,map)
title("Original Indexed Image and Colormap")
imshow(Y,newmap)
title("Resized Image and Optimized Colormap")
Input Arguments
A
— Image to be resized
numeric array  logical array  categorical array
Image to be resized, specified as a numeric array, logical array, or categorical array of any dimension. Input must be nonsparse, and numeric input must be real.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 logical
 categorical
scale
— Resize factor
positive number
Resize factor, specified as a positive number.
imresize
applies the same scale factor to the row
and column dimensions. To apply a different resize factor to each dimension,
use the 'Scale'
namevalue argument.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
[numrows numcols]
— Row and column dimensions of output image
twoelement vector of positive numbers
Row and column dimensions of output image, specified as a twoelement
vector of positive numbers. You can specify the value NaN
for either numrows
or numcols
. In this
case, imresize
calculates the number of rows or columns
for that dimension automatically, preserving the aspect ratio of the
image.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
X
— Indexed image to be resized
real, nonsparse numeric array
Indexed image to be resized, specified as a real, nonsparse numeric array of positive integers.
Data Types: double
 uint8
 uint16
map
— Colormap associated with indexed image
cby3 numeric matrix
Colormap associated with indexed image X
, specified
as a cby3 numeric matrix with values in the range [0,
1]. Each row is a threeelement RGB triplet that specifies the red, green,
and blue components of a single color of the colormap.
Data Types: double
method
— Interpolation method
"bicubic"
(default)  character vector  string scalar  twoelement cell array
Interpolation method, specified as a character vector, string scalar, or twoelement cell array.
When method
is a character vector or string scalar,
it identifies a particular method or named interpolation kernel, listed in
the following table.
Method  Description 


Nearestneighbor interpolation; the output pixel is assigned the value of the pixel that the point falls within. No other pixels are considered. 

Bilinear interpolation; the output pixel value is a weighted average of pixels in the nearest 2by2 neighborhood 

Bicubic interpolation; the output pixel value is a weighted average of pixels in the nearest 4by4 neighborhood Note Bicubic interpolation can produce pixel values outside the original range. 
Interpolation Kernel  Description 
"box"  Boxshaped kernel 
"triangle"  Triangular kernel (equivalent to
"bilinear" ) 
"cubic"  Cubic kernel (equivalent to
"bicubic" ) 
"lanczos2"  Lanczos2 kernel 
"lanczos3"  Lanczos3 kernel 
When method
is a twoelement cell array, it defines a
custom interpolation kernel. The cell array has the form
{f,w}, where f
is a function handle for a custom interpolation kernel and
w is the width of the custom kernel.
f(x) must be zero outside the
interval w/2 <= x <
w/2. The function handle f can be called
with a scalar or a vector input. For userspecified interpolation kernels,
the output image can have some values slightly outside the range of pixel
values in the input image.
For more information about the builtin and custom interpolation kernels, see Create and Compare Resizing Interpolation Kernels.
Data Types: char
 string
 cell
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: B = imresize(A,0.5,Antialiasing=false)
does not perform
antialiasing when shrinking an image.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: B = imresize(A,0.5,"Antialiasing",false)
does not
perform antialiasing when shrinking an image.
Antialiasing
— Perform antialiasing when shrinking an image
true
 false
Perform antialiasing when shrinking an image, specified as
true
or false
.
If the interpolation
method
is"nearest"
, then the default value of'Antialiasing'
isfalse
.If the interpolation
method
is"box"
and the input image is categorical, then the default value of'Antialiasing'
isfalse
.For all other interpolation methods, the default value is
true
.
Data Types: logical
Colormap
— Return optimized colormap
"optimized"
(default)  "original"
Return optimized or original colormap for indexed image, specified as one of the following values.
Value  Description 

"original"  The output colormap newmap is
the same as the input colormap
map . 
"optimized"  imresize returns a new optimized
colormap. 
The 'Colormap'
argument is valid only when
resizing indexed images.
Data Types: char
 string
Dither
— Perform color dithering
true
(default)  false
Perform color dithering, specified as true
or
false
. In dithering, you apply a form of noise to
the image to randomize quantization error and prevent largescale
patterns.
The 'Dither'
argument is valid only when resizing
indexed images.
Data Types: logical
Method
— Interpolation method
"bicubic"
(default)  character vector  cell array
Interpolation method, specified as a character vector or twoelement
cell array. For details, see method
.
Data Types: char
 string
 cell
OutputSize
— Size of output image
twoelement vector of positive numbers
Size of the output image, specified as a twoelement vector of
positive numbers. For details, see [numrows
numcols]
.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
Scale
— Resize scale factor
positive number  twoelement vector of positive numbers
Resize scale factor, specified as a positive number or twoelement
vector of positive numbers. If you specify a scalar, then
imresize
applies the same scale factor to the
row and column dimensions. If you specify a twoelement vector, then
imresize
applies a different scale value to
each dimension.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
Output Arguments
B
— Resized image
numeric array  logical array  categorical array
Resized image, returned as a numeric, logical, or categorical array of the
same data type as the input image, A
.
Y
— Resized indexed image
numeric array
Resized indexed image, returned as a numeric array of the same data type
as the input indexed image, X
.
newmap
— Colormap of resized indexed image
mby3 numeric matrix
Colormap of the resized indexed image Y
, returned as
an mby3 numeric matrix. By default,
imresize
returns a new, optimized colormap with the
resized image. To return a colormap that is the same as the original
colormap, use the 'Colormap'
namevalue
argument.
Tips
If the size of the output image is not an integer, then
imresize
does not use the scale specified.imresize
uses theceil
function when calculating the output image size.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Syntaxes that support indexed images are not supported, including the namevalue arguments
'Colormap'
and'Dither'
.Custom interpolation kernels are not supported.
All namevalue arguments must be compiletime constants.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
'Colormap'
and'Dither'
namevalue arguments are not supported.Indexed images are not supported.
Custom interpolation kernels are not supported.
All namevalue pairs must be compiletime constants.
For certain interpolation kernels, there can be a small numerical mismatch between the results in MATLAB^{®} and the generated code.
ThreadBased Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports threadbased environments. For more information, see Run MATLAB Functions in ThreadBased Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
gpuArray
input with more than 2^{27} elements is not supported.gpuArray
input must be nonsparse.gpuArray
input with underlying typecategorical
is not supported.Indexed images are not supported on a GPU.
If you use bicubic interpolation, the output image can have some values slightly outside the range of pixel values in the input image.
There is a slight numerical difference between the results of
imresize
on a CPU and a GPU. These differences occur on the right and bottom borders of the image and are barely noticeable to the naked eye.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)