Resize image
returns image J
= imresize(I
,[numrows
numcols]
)J
that has the number of rows and columns
specified by the vector [numrows numcols]
.
___ = imresize(___,
uses name-value pair arguments to control various aspects of the resizing
operation.Name,Value
)
Read image into the workspace.
I = imread('rice.png');
Resize the image, specifying scale factor and using default interpolation method and antialiasing.
J = imresize(I, 0.5);
Display the original and the resized image.
figure
imshow(I)
title('Original Image')
figure
imshow(J)
title('Resized Image')
Read image into the workspace.
I = imread('rice.png');
Resize the image, specifying scale factor and the interpolation method.
J = imresize(I, 0.5, 'nearest');
Display the original and the resized image.
figure
imshow(I)
title('Original Image')
figure
imshow(J)
title('Resized Image Using Nearest-Neighbor')
Read image into the workspace.
[X, map] = imread('trees.tif');
Resize the image, specifying a scale factor. By default, imresize
returns an optimized color map with the resized indexed image.
[Y, newmap] = imresize(X, map, 0.5);
Display the original image and the resized image.
figure
imshow(X,map)
title('Original Image')
figure
imshow(Y,newmap)
title('Resized Image')
Read image into the workspace.
RGB = imread('peppers.png');
Resize the image, specifying that the output image have 64 rows. Let imresize
calculate the number of columns necessary to preserve the aspect ratio.
RGB2 = imresize(RGB, [64 NaN]);
Display the original image and the resized image.
figure
imshow(RGB)
title('Original Image')
figure
imshow(RGB2)
title('Resized Image')
I
— Image to be resizedImage to be resized, specified as a numeric, logical, or categorical array
of any dimension. If I
has more than two dimensions,
then imresize
only resizes the first two
dimensions.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| logical
| categorical
scale
— Resize factorResize factor, specified as a positive number.
If scale
is less than 1, then the output
image is smaller than the input image.
If scale
is greater than 1, then the
output image is larger than the input image.
imresize
applies the scale factor to each dimension in
the image. To apply a different resize factor to each dimension, use the
Scale
name-value pair argument.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
[numrows numcols]
— Row and column dimensions of output imageRow and column dimensions of the output image, specified as a 2-element
vector of positive integers. You can specify the value
NaN
for either numrows
or
numcols
. In this case, imresize
computes 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 resizedIndexed image to be resized, specified as a numeric array.
Data Types: double
| uint8
| uint16
map
— ColormapColormap associated with indexed image X
, specified
as a c-by-3 numeric matrix with values in the range [0, 1]. Each row is a
three-element RGB triplet that specifies the red, green, and blue components of a single color
of the color map.
Data Types: double
method
— Interpolation method or kernelInterpolation method or kernel, specified as a string scalar, character vector, or 2-element cell array.
When method
is a string scalar or character vector, it
identifies a particular method or named interpolation kernel, listed in the
following table.
Method | Description |
---|---|
| Nearest-neighbor interpolation; the output pixel is assigned the value of the pixel that the point falls within. No other pixels are considered. Nearest-neighbor interpolation is the only interpolation method supported for categorical images and it is the default method for images of this type. |
| Bilinear interpolation; the output pixel value is a weighted average of pixels in the nearest 2-by-2 neighborhood. |
| Bicubic interpolation; the output pixel value is a weighted average of pixels in the nearest 4-by-4 neighborhood. Bicubic interpolation is the default method for numeric and logical images. Note Bicubic interpolation can produce pixel values outside the original range. |
Interpolation Kernel | Description |
'box' | Box-shaped kernel The box-shaped kernel is the only interpolation kernel supported for categorical images. |
'triangle' | Triangular kernel (equivalent to
'bilinear' ) |
'cubic' | Cubic kernel (equivalent to
'bicubic' ) |
'lanczos2' | Lanczos-2 kernel |
'lanczos3' | Lanczos-3 kernel |
When method
is a 2-element 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 user-specified interpolation kernels,
the output image can have some values slightly outside the range of pixel
values in the input image.
Data Types: char
| string
| cell
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
I2 = imresize(I,0.5,'Antialiasing',false);
'Antialiasing'
— Perform antialiasing when shrinking an imagetrue
| false
Perform antialiasing when shrinking an image, specified as the comma-separated pair consisting
of 'Antialiasing'
and true
or
false
.
If method
is
'nearest'
, then the default value of
'Antialiasing'
is
false
.
If the interpolation method
is the
'box'
interpolation kernel and the
input image is categorical, then the default value of
'Antialiasing'
is
false
.
For all other interpolation methods, the default is
true
.
Data Types: logical
'Colormap'
— Return optimized or original color map'optimized'
(default) | 'original'
Return optimized or original color map for indexed image, specified as
the comma-separated pair consisting of 'Colormap'
and
one of the following.
Value | Description |
---|---|
'original' | The output color map newmap is
the same as the input color map
map . |
'optimized' | imresize returns a new optimized
color map. |
The 'Colormap'
argument is valid only when resizing
indexed images.
Data Types: char
| string
'Dither'
— Perform color ditheringtrue
(default) | false
Perform color dithering, specified as the comma-separated pair consisting of
'Dither'
and true
or
false
. In dithering, you apply a form of noise to
the image to randomize quantization error and prevent large-scale
patterns.
The 'Dither'
argument is valid only when resizing
indexed images.
Data Types: logical
'Method'
— Interpolation methodInterpolation method, specified as the comma-separated pair consisting of
'Method'
and a scalar string, character vector,
or 2-element cell array. For details, see
method
.
Data Types: char
| string
| cell
'OutputSize'
— Size of output imageSize of the output image, specified as the comma-separated pair consisting of
'OutputSize'
and a 2-element vector of positive
integers of the form [numrows numcols]
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
'Scale'
— Resize scale factorResize scale factor, specified as the comma-separated pair consisting of
'Scale'
and a positive number or 2-element vector
of positive numbers. If you specify a scalar, then
imresize
applies the same scale factor to each
dimension in the image. If you specify a 2-element vector, then
imresize
applies a different scale value to each
dimension.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
J
— Resized imageResized image, returned as a numeric, logical, or categorical array of the
same class as the input image, I
.
Y
— Resized indexed imageResized indexed image, returned as a numeric array of the same class as the input indexed
image, X
.
newmap
— Optimized colormapOptimized colormap, returned as a c-by-3 numeric matrix with values in the range [0, 1]. Each row is a three-element RGB triplet that specifies the red, green, and blue components of a single color of the color map.
The function imresize
changed in
version 5.4 (R2007a). Previous versions of the Image Processing Toolbox™ used
a different algorithm by default. If you need the same results produced
by the previous implementation, use the function imresize_old
.
If the size of the output image is not an integer, then
imresize
does not use the scale specified.
imresize
uses ceil
when calculating
the output image size.
Usage notes and limitations:
imresize
supports the generation of C
code (requires MATLAB®
Coder™). For more information, see Code Generation for Image Processing.
Syntaxes that support indexed images are not supported,
including the named parameters 'Colormap'
and 'Dither'
.
Custom interpolation kernels are not supported.
All name-value pair arguments must be compile-time constants.
Usage notes and limitations:
'Colormap'
and 'Dither'
name-value pair arguments are not supported.
Indexed images are not supported.
Custom interpolation kernels are not supported.
All name-value pairs must be compile-time constants.
For certain interpolation kernels, there can be a small numerical mismatch between the results in MATLAB and the generated code.
Usage notes and limitations:
gpuArray
input must be of type
single
or double
.
imresize
does not support indexed images on a
GPU.
imresize
does not support name-value pair arguments
on a GPU.
On a GPU, imresize
supports only bicubic
interpolation and always performs antialiasing.
There is a slight numerical difference between the results of
imresize
on the CPU and the 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 Image Processing on a GPU.
You have a modified version of this example. Do you want to open this example with your edits?