How to Resize Image in MATLAB (2024)

  1. Resize Images in MATLAB Using the imresize() Function
  2. Conclusion
How to Resize Image in MATLAB (1)

Resizing images is a fundamental task in image processing, whether you’re preparing images for analysis, visualization, or presentation. MATLAB, a widely used platform for numerical computing and data analysis, provides powerful tools for image manipulation, including resizing.

In this article, we’ll explore how to resize images in MATLAB using the built-in function imresize() and discuss various techniques and considerations.

Resize Images in MATLAB Using the imresize() Function

The imresize() function provides a convenient way to adjust image dimensions in MATLAB. This function allows users to adjust the size of images while preserving their aspect ratio and content integrity.

The imresize() function provides flexibility in specifying the desired dimensions of the output image, allowing users to resize images to a specific size or scale them by a certain factor.

The basic syntax of the imresize() function is:

B = imresize(A, scale)

Where:

  • A is the input image.
  • scale is the scaling factor. If scale is less than 1, it will resize the image to a smaller size; if scale is greater than 1, it will resize the image to a larger size.

Additionally, there are several other syntaxes of the imresize() function, allowing you to specify the output size, interpolation method, and other parameters.

Here’s a more detailed breakdown of the syntax options:

B = imresize(A, [M N])B = imresize(A, [M N], method)B = imresize(A, [M N], method, 'OutputSize', 'crop')B = imresize(A, [M N], method, 'OutputSize', 'scale')B = imresize(___, Name, Value)

Where:

  • [M N] specifies the output size as a two-element vector [rows cols].
  • method is an optional argument specifying the interpolation method. Possible values include:
    • 'nearest': Nearest-neighbor interpolation.
    • 'bilinear': Bilinear interpolation.
    • 'bicubic': Bicubic interpolation.
    • 'box': Box interpolation.
    • 'triangle': Triangle interpolation.
    • 'lanczos2': Lanczos-2 interpolation.
    • 'lanczos3': Lanczos-3 interpolation.
  • 'OutputSize', 'crop' specifies that the output image should be cropped to the specified size.
  • 'OutputSize', 'scale' specifies that the output image should be scaled to fit within the specified size while preserving the aspect ratio.
  • Name, Value pairs allow you to specify additional parameters such as 'Antialiasing', 'Colormap', etc.

The imresize() function works by interpolating pixel values from the original image to create a new image with the desired dimensions. When enlarging an image, it generates new pixels by interpolating values from neighboring pixels. Conversely, when reducing image size, it removes pixels based on the specified scale factor.

You can type help imresize in the MATLAB command window for more detailed information on the imresize() function and its syntax.

Now, let’s walk through some examples demonstrating how to resize images using the imresize() function in MATLAB.

Example 1: Uniform Resizing Using the imresize() Function

In this example, we’ll resize an image uniformly by a scale factor of 2.

% Load original imageoriginal_image = imread('cat1.jpg');% Resize the imageresized_image = imresize(original_image, 10);% Display original and resized imagesfigureimshow(original_image)title('Original Image')figureimshow(resized_image)title('Resized Image')

Here, we demonstrate uniform resizing of an image by a scale factor of 2 using the imresize() function.

Initially, we load the original image using imread() and assign it to the variable original_image. Then, we resize the image using imresize(original_image, 2) to increase its dimensions by a factor of 2.

This creates a new image, resized_image, with doubled width and height compared to the original. Finally, we display both the original and resized images side by side using imshow() within a figure with two subplots.

The imshow() function is used to display each image separately. This example showcases how imresize() uniformly scales the dimensions of an image.

Output:

How to Resize Image in MATLAB (2)

Example 2: Resizing to Specific Dimensions Using the imresize() Function

In this example, we’ll resize an image to specific dimensions, such as 300x300 pixels.

% Load original imageoriginal_image = imread('cat1.jpg');% Resize the image to specific dimensionsresized_image = imresize(original_image, [300, 300]);% Display original and resized imagesfigureimshow(original_image)title('Original Image')figureimshow(resized_image)title('Resized Image')

In this example, we demonstrate resizing an image to specific dimensions, in this case, 300x300 pixels.

Similar to Example 1 above, we start by loading the original image using imread() and store it in the variable original_image. Next, we use imresize(original_image, [300, 300]) to resize the image to the specified dimensions.

This results in a new image, resized_image, with dimensions of 300x300 pixels. We then display both the original and resized images side by side in separate figures, similar to Example 1.

This example illustrates how imresize() can be used to resize images to precise dimensions.

Output:

How to Resize Image in MATLAB (3)

Example 3: Resizing an Indexed Image Using the imresize() Function

In this example, we’ll resize an indexed image while preserving its colormap.

% Read indexed image[indexed_image, colormap] = imread("corn.tif");% Resize the indexed image[resized_indexed_image, new_colormap] = imresize(indexed_image, colormap, 2);% Display original and resized indexed imagesfigureimshow(indexed_image, colormap)title('Original Indexed Image and Colormap')figureimshow(resized_indexed_image, new_colormap)title('Resized Indexed Image and Optimized Colormap')

Here, we demonstrate resizing an indexed image while preserving its colormap.

First, we read the indexed image and its corresponding colormap using imread() and assign them to the variables indexed_image and colormap, respectively. Then, we use imresize(indexed_image, colormap, 2) to resize the indexed image by a scale factor of 2 while maintaining its colormap.

This results in a new resized indexed image, resized_indexed_image, and a new colormap, new_colormap. We display both the original indexed image and the resized indexed image side by side in separate figures.

This example highlights how imresize() can handle indexed images with colormaps.

Output:

How to Resize Image in MATLAB (4)

Example 4: Resizing an Image Using the imresize() Function With Interpolation Methods

In this example, we’ll resize an image while specifying different interpolation methods.

% Load original imageoriginal_image = imread('cat1.jpg');% Resize the image using different interpolation methodsresized_nearest = imresize(original_image, 0.2, 'nearest');resized_bilinear = imresize(original_image, 0.3, 'bilinear');resized_bicubic = imresize(original_image, 0.4, 'bicubic');% Display original and resized images with different interpolation methodsfigureimshow(original_image)title('Original Image')figureimshow(resized_nearest)title('Resized (Nearest)')figureimshow(resized_bilinear)title('Resized (Bilinear)')figureimshow(resized_bicubic)title('Resized (Bicubic)')

Here, we show resizing an image using different interpolation methods: nearest neighbor, bilinear, and bicubic.

We start by loading the original image using imread() and store it in the variable original_image. Then, we resize the image three times using imresize() with different interpolation methods: 'nearest', 'bilinear', and 'bicubic'.

This creates three resized images: resized_nearest, resized_bilinear, and resized_bicubic, each using a different interpolation method. Finally, we display the original image and the three resized images side by side in separate figures.

Each plot is labeled with the corresponding interpolation method. This example demonstrates how to specify different interpolation methods with imresize() to achieve varying degrees of image quality and smoothness.

Output:

How to Resize Image in MATLAB (5)

Example 5: Resizing an RGB Image Using the imresize() Function

In this example, we’ll resize an RGB image using the imresize() function in MATLAB.

% Load original RGB imageoriginal_image = imread('peppers.png');% Resize the RGB image to 50% of its original sizeresized_image = imresize(original_image, 0.5);% Display original and resized imagesfigureimshow(original_image)title('Original RGB Image')figureimshow(resized_image)title('Resized RGB Image (50% of Original Size)')

In this code snippet, we begin by loading the original RGB image named peppers.png into the variable original_image using the imread() function.

Next, we proceed to resize the original RGB image to 50% of its original size. We accomplish this by calling the imresize() function with two arguments: the original image variable original_image and the scaling factor 0.5.

This scaling factor reduces the dimensions of the image by half, effectively resizing it to half of its original width and height. The resulting resized image is stored in the variable resized_image.

Finally, we display both the original and resized images for comparison purposes. We create two separate figures using the figure function to accommodate each image.

You can adjust the scaling factor as needed to resize the image to a different size. Additionally, you can experiment with other interpolation methods supported by the imresize() function for resizing RGB images, such as 'nearest', 'bilinear', or 'bicubic'.

Output:

How to Resize Image in MATLAB (6)

Conclusion

In conclusion, resizing an image in MATLAB is a straightforward process facilitated by the imresize() function. This function allows us to adjust the dimensions of an image according to our specific requirements, whether it be scaling the image by a certain factor, resizing it to fit within specified dimensions, or applying custom interpolation methods.

By simply loading the original image and applying the desired resizing operation using imresize(), we can efficiently manipulate image sizes to suit our needs. Additionally, MATLAB provides various options for interpolation methods, aspect ratio preservation, and batch processing, enabling flexibility and customization in resizing images.

Whether working with grayscale, RGB, or indexed images, MATLAB offers a versatile toolkit for image resizing tasks, making it a powerful tool for image processing and analysis. With the ability to resize images seamlessly, MATLAB empowers users to effectively manage and manipulate image data for a wide range of applications, from computer vision and machine learning to scientific research and digital imaging.

How to Resize Image in MATLAB (2024)

FAQs

How do I resize an image in Matlab? ›

B = imresize( A , scale ) returns image 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 .

How to increase resolution of image in Matlab? ›

You can increase the size of figure object as follow calling getframe() as follow,
  1. f = gcf;
  2. f. Position = [100 100 width height]; % here specify the width and height of the figure.
  3. % corresponding to this, the image resolution will also change.
  4. F = getframe(f);
Apr 11, 2018

How to calculate image size in matlab? ›

You must use [height, width, colour_planes] = size(image); because images have 3 dimensions.

How do I keep the aspect ratio of an image in Matlab? ›

Specify Size of 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 as NaN , imresize calculates the value for that dimension to preserve the aspect ratio of the image.

How do I resize an image to a specific size? ›

Open the image in Preview by simply double-‐clicking the image. (If you've changed your image file associations, you also can right-‐click on an image, point to Open With, and select Preview.) 2. Select Tools, then select Adjust Size to bring up the Resize dialog box, which will allow you to resize the image.

How do I resize an image in coding? ›

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do I change the resolution of a figure in Matlab? ›

It is possible to increase the resolution of the plots obtained in MATLAB. On the Plot window ---- Click on <Edit> --> <Figure properties> --> <Export setup> --> <Rendering>. Then change the resolution to 300 or 600dpi. Then Click on Export and save as .

What is size size in Matlab? ›

size (MATLAB Functions) d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements. [m,n] = size(X) returns the size of matrix X in separate variables m and n . m = size(X,dim) returns the size of the dimension of X specified by scalar dim .

What is the formula for image size? ›

Digital Imaging Tutorial - Basic Terminology. FILE SIZE is calculated by multiplying the surface area of a document (height x width) to be scanned by the bit depth and the dpi2. Because image file size is represented in bytes, which are made up of 8 bits, divide this figure by 8.

How do you set the size of a figure in Matlab? ›

Set Figure Size

Set the figure size by clicking File > Export Setup. Specify the desired dimensions in the Width and Height fields, for example 5-by-4 inches. The dimensions include the entire figure window except for the frame, title bar, menu bar, and any tool bars.

How do I fix the size of a figure in MATLAB? ›

All you have to do is to replace the values of startingX startingY Width Height, like the following:
  1. figure('Position',[startingX startingY Width Height]);
  2. % Example with numerical values.
  3. figure('Position',[600 100 1500 1000]);
Jan 30, 2015

How do I resize an image keeping proportions? ›

If the Shift key is held down whilst moving the handle, then the proportions of the object will be preserved. For example, if I hold Shift and drag the bottom edge upwards to reduce the size by half, then the right edge will automatically move to the left to reduce the width of the object by the same amount.

How do you resize image and bounding box in MATLAB? ›

Resize Image and Bounding Box

Use imresize to scale down the image by a factor of 2. scale = 1/2; J = imresize(I,scale); Use bboxresize to apply the same scaling to the associated bounding box. bboxResized = bboxresize(bbox,scale);

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6420

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.