Main Content

Resize an Image with imresize Function

This example shows how to resize an image using theimresizefunction.

Specify the Magnification Value

Read and display an image.

I = imread("circuit.tif"); imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

Resize the image, using theimresizefunction. In this example, you specify a magnification factor. To enlarge an image, specify a magnification factor greater than 1.

magnificationFactor = 1.25; J = imresize(I,magnificationFactor);

Display the enlarged image.

imshow(J)

Figure contains an axes object. The axes object contains an object of type image.

Specify the Size of the Output Image

再次调整图像,time specifying the desired size of the output image, rather than a magnification value. Passimresizea vector that contains the number of rows and columns in the output image. If the specified size does not produce the same aspect ratio as the input image, the output image will be distorted. If you specify one of the elements in the vector asNaN,imresizecalculates the value for that dimension to preserve the aspect ratio of the image. To perform the resizing required for multi-resolution processing, useimpyramid.

K = imresize(I,[100 150]); imshow(K)

Figure contains an axes object. The axes object contains an object of type image.

Specify the Interpolation Method

再次调整图像,time specifying the interpolation method. When you enlarge an image, the output image contains more pixels than the original image.imresizeuses interpolation to determine the values of these pixels, computing a weighted average of some set of pixels in the vicinity of the pixel location.imresizebases the weightings on the distance each pixel is from the point. By default,imresizeuses bicubic interpolation, but you can specify other interpolation methods or interpolation kernels. See theimresizereference page for a complete list. You can also specify your own custom interpolation kernel. This example use bilinear interpolation.

L = imresize(I,1.5,"bilinear"); imshow(L)

Figure contains an axes object. The axes object contains an object of type image.

Prevent Aliasing When Shrinking an Image

再次调整图像,time shrinking the image. When you reduce the size of an image, you lose some of the original pixels because there are fewer pixels in the output image. This can introduce artifacts, such as aliasing. The aliasing that occurs as a result of size reduction normally appears as stair-step patterns (especially in high-contrast images), or as moire (ripple-effect) patterns in the output image. By default,imresizeuses antialiasing to limit the impact of aliasing on the output image for all interpolation types except nearest neighbor. To turn off antialiasing, specify the 'Antialiasing' parameter and set the value to false. Even with antialiasing turned on, resizing can introduce artifacts because information is always lost when you reduce the size of an image.

M = imresize(I,.75,"Antialiasing",false); imshow(M)

Figure contains an axes object. The axes object contains an object of type image.