Image Resize in MATLAB

This tutorial will discuss resizing an image using the imresize() function in Matlab.

Resize an Image Using the imresize() Function in MATLAB

We can use the imresize() function to resize images in Matlab. This function resizes images by increasing or decreasing their pixels. For example, if we resize an image two times of size 100-by-100, then its final size will be 200-by-200. This function creates more pixels using the pixel values already present in the neighborhood. If we want to lower the size of the image, we can use a resize scale value of less than 1. In this case, the imresize() function will remove pixels from the image depending on the resize value. If the resize scale is 0.5, the pixel will become half. The first argument is the image to be resized. It can be of type logical, numeric, and categorical, but the numeric values should be real. The second argument is the scale, or the resize factor, and it should be a positive number. For example, let’s read an image using the imread() function and resize it using the imresize() function, and then plot both using the imshow() function. See the code below.

clc
RGB = imread('cat.jpg');
RI = imresize(RGB,10);
figure
imshow(RGB)
figure
imshow(RI)

Output:

image resize using imresize function

The left image is the original image in the output, and the right image is the resized image. You can also check the size of the variable in which the two output image is stored, and it will be ten times greater than the original size. If the image contains more than two dimensions, the imresize() function will only change the size of the first two dimensions, and other dimensions will remain the same. For example, in the case of the above code, if you look at the workspace in Matlab, you will know that the third dimension is the same in both the original and resized image. Instead of using an integer as the resize scale value, we can use a vector to define the size of the output image like [100 100] to get an output image of size 100-by-100. For example, let’s resize the above image to get an image of size 100-by-100. See the code below.

clc
RGB = imread('cat.jpg');
RI = imresize(RGB,[100 100]);
figure
imshow(RGB)
figure
imshow(RI)

Output:

resizing image to the desired size

The left image is the original image in the output, and the right image is the resized image. You can also see the size of the output image on top of the right image, which is 100-by-100. The third dimension is not changed because it contains the color values present in the image. We can also resize an indexed image using the imresize() function. An indexed image is different as compared to an RGB image, it is composed of a 2D array with byte data stored in it, and we can also put a different type of data in it like double, int16, etc. An RGB image contains the color as a third dimension, but the indexed image contains color as a separate colormap. To resize an indexed image, we have to use a second variable inside the imresize() function, which is the colormap of the input image, which we can get using a second output of the imread() function. The resize function will give us a new 2D array and a new colormap. To plot an indexed image, we have to use the 2D array and the map. For example, let’s read an indexed image and resize it. See the code below.

clc
[OI,map] = imread("corn.tif");
[RI,Nmap] = imresize(OI,map,2);
figure
imshow(OI,map)
figure
imshow(RI,Nmap)

Output:

resizing indexed image

The right image is the original image in the output, and the left image is the resized image. We can also change the method of interpolation, which is used to resize the image. The default method is bicubic, but we can change it to nearest or bilinear by entering a string containing the new method’s name in the imresize() function. In the nearest interpolation method, the output pixel is assigned the value of the pixel that the point falls within. In the bilinear interpolation method, the output pixel is assigned a value which is a weighted average of pixels in the nearest 2-by-2 neighborhood. In the bicubic interpolation method, the output pixel is assigned a value which is a weighted average of pixels in the nearest 4-by-4 neighborhood.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - MATLAB Image

  • Flip Image in MATLAB
  • Draw Line on Image in Matlab