Change Image Size - MATLAB & Simulink (2024)

This example uses:

  • HDL CoderHDL Coder
  • SimulinkSimulink
  • Computer Vision ToolboxComputer Vision Toolbox
  • Vision HDL ToolboxVision HDL Toolbox

Open Model

This example shows how to downsample a multicomponent image by using the Image Resizer block. The example also shows how to implement custom bicubic and Lanczos-2 interpolation algorithms for FPGAs by using basic Simulink blocks.

The most basic of interpolation algorithms, nearest neighbor, assumes the value of its closest neighbor and is computationally lightweight. In theory, you can achieve a more exact reconstruction of an image by using a sinc kernel. However, sinc kernels have infinite spatial extent. To limit the extent, interpolation implementations use simpler kernels to approximate a sinc. The bilinear interpolation algorithm uses the weighted sum of the nearest four pixels to determine the values of the output pixels. Bicubic and Lanczos-2 interpolations are approximations of a sinc kernel. Bicubic interpolation is a more computationally efficient version of the Lanczos-2 method. This example implements and compares these interpolation algorithms.

Behavioral Reference

By default, the imresize function uses the bicubic interpolation algorithm. You can choose the nearest neighbor, bilinear, or Lanczos-2 interpolation algorithms by setting the 'Method' name-value argument to 'nearest', 'bilinear', or 'lanczos2', respectively.

v = VideoReader('rhinos.avi');I = readFrame(v);Y = imresize(I,[160,256],Method='bilinear');figure;imshow(Y)

Change Image Size- MATLAB & Simulink (1)

Interpolation Algorithms

Nearest Neighbor

Nearest neighbor interpolation determines the inserted pixel values by assuming the value of the closest of its four neighbors. The interpolator calculates the horizontal, Change Image Size- MATLAB & Simulink (2), and vertical, Change Image Size- MATLAB & Simulink (3), scale factors independently.

Change Image Size- MATLAB & Simulink (4)

No additional calculations are required once the scale has been determined, which removes the requirement for any multipliers.

Bilinear and Bicubic

Bilinear interpolation, a first-order sinc approximation algorithm, determines the inserted pixel value from the weighted average of the four input pixels nearest to the inserted location.

Change Image Size- MATLAB & Simulink (5)

The value for each output pixel is given by Change Image Size- MATLAB & Simulink (6).

The bicubic algorithm [1] calculates the average of the 16 input pixels nearest to the inserted location.

Change Image Size- MATLAB & Simulink (7)

The bicubic coefficients are given by Change Image Size- MATLAB & Simulink (8).

These equations show that the bilinear and bicubic algorithms calculate coefficients for each output pixel.

Lanczos-2

The Lanczos-2 algorithm precalculates the coefficients based on the resize factor. The model calls the lanczos2_coeffi.m script to calculate and store these coefficients. The script calculates the Lanczos-2 coefficients using 6 taps and 32 phases.

Implementation of Basic-Blocks Interpolation Algorithms for HDL

This figure shows the principle used to implement the image resize algorithm for hardware. For example, consider resizing an image by a scale factor of 3/4. One possible implementation is to upsample by a factor of 3 and then downsample by a factor of 4. The figure shows the pixel indexes after these operations. Blue dots represent the original pixels, and green crosses represent the interpolated pixels after upsampling.

Change Image Size- MATLAB & Simulink (9)

The indexes after downsampling show that not all the interpolated pixels are used in the output image. This example implements a more efficient version of the downsample step by generating interpolated pixels only when they are needed in the output image.

The phase, shown in the bottom line of the figure, is an index that selects which pixels are needed for the output image. When the phase is 0, the algorithm returns the original input pixel value. When the phase is 1, the algorithm calculates coefficients to generate the interpolated pixel in the first position. When the phase is 2, the algorithm calculates coefficients to generate the interpolated pixel in the second position.

Example Model

The model contains two datapaths. The top datapath uses the Image Resizer block to perform resize using bilinear interpolation. The bottom datapath implements resize using bicubic and Lanczos-2 interpolation.

Change Image Size- MATLAB & Simulink (10)

Image Resizer

The Image Resizer block provides support for nearest neighbor and bilinear interpolation with an integrated antialiasing prefilter.

Change Image Size- MATLAB & Simulink (11)

The integrated lowpass Gaussian antialiasing filter provides convenient means to avoid aliasing introduced through the reduction in sampling rate when downsampling.

The Image Resizer block provides the ability to insert horizontal blanking between output active video lines. You can use this capability to meet downstream horizontal blanking constraints. However, do not use this capability to perform the role of the Pixel Stream FIFO, which buffers input video lines to output contiguous valid active video lines. Inserting too much horizontal blanking can have an impact on the overall frame rate and may necessitate pacing of the input.

The model includes an optional Pixel Stream FIFO after the Image Resizer block that you can use to consolidate the output pixels, while the Measure Timing block displays the size of the output frames.

When processing multicomponent data, use the Image Resizer block with a ForEach subsystem. The ForEach subsystem replicates the Image Resizer block across each input component while maintaining a single multicomponent frame input and output.

Change Image Size- MATLAB & Simulink (12)

The ForEach subsystem is configured to partition and concatenate the pixel input and output on the second dimension, horizontally. The output pixelcontrol buses are all duplicates and so only one needs to be retained at the top level. The Selector block selects a single pixelcontrol bus to use in the rest of the data path.

Basic-Blocks Implementation

Similar to the imresize function, the imresize(downsample) subsystem in this model supports two ways to define the output image size. You can specify a scale factor ranging from 1.000 to 127.999, or you can define the output frame width and height in pixels down to a minimum of 8 by 8. Double-click the imresize(downsample) subsystem to set its parameters. The imresize(downsample) subsystem requires contiguous video lines.

To avoid aliasing with the basic-blocks implementation, the model includes a lowpass filter before the imresize(downsample) subsystem that you can enable by setting its Constant block input to a value of 1. After the imresize(downsample) subsystem, there is also an optional Pixel Stream FIFO and a Measure Timing block to display its output frame sizes.

In the imresize(downsample) subsystem, the input_conversion and output_conversion subsystems convert the color space [2] of the pixel stream based on the parameter on the mask. The valid_gen_horizontal and valid_gen_vertical subsystems return control signals that are used for generating coefficients and rebuilding the output control bus. If the last line of the image contains no valid pixels after downsampling, the ctrlBusRebuild subsystem rebuilds the control bus for the new size.

Change Image Size- MATLAB & Simulink (13)

This diagram shows the expected output from the valid_gen_horizontal and valid_gen_vertical subsystems. The valid signal indicates the validity of the current address and the corresponding phase. To simplify rebuilding the control bus, the first line and row of each output frame are always valid.

Change Image Size- MATLAB & Simulink (14)

The coefficient generation subsystem, coeffi_gen, is a variant subsystem, where bilinear, bicubic, and Lanczos-2 coefficient generators are implemented separately. You can select the algorithm from the mask.

Change Image Size- MATLAB & Simulink (15)

The resize_process_element subsystems multiply the coefficients with each pixel component by using a separable filter in vertical order and then in horizontal order. The trim_0_1 subsystem ensures the result is between 0 and 1.

Change Image Size- MATLAB & Simulink (16)

Resource Usage

These tables show the resource usage for the Image Resizer block and imresize(downsample) subsystem with 240p video input, and do not include the antialiasing filters or the Pixel Stream FIFOs. The design was synthesized to achieve a clock frequency of 150 MHz.

This table shows the resources for each of the algorithms when downsampled in the RGB color space.

 LUT LUTRAM FF BRAM DSP ____ ______ _____ ____ ___ Image Resizer - Nearest Neighbor 1282 36 1337 3 0 Image Resizer - Bilinear 1317 24 1808 3 12 imresize(downsample) - Bilinear 2414 386 4333 1.5 20 imresize(downsample) - Bicubic 4405 597 8827 4.5 30 imresize(downsample) - Lanczos-2 6490 802 13708 7.5 38 

This table shows the resources for each of the three imresize(downsample) algorithms when downsampled in the HSV color space.

 LUT LUTRAM FF BRAM DSP ____ ______ _____ ____ ___ imresize(downsample) - Bilinear 2987 444 5688 11 26 imresize(downsample) - Bicubic 5117 658 10248 14 36 imresize(downsample) - Lanczos-2 7139 875 15351 17 44 

References

[1] Keys, R. "Cubic Convolution Interpolation for Digital Image Processing." IEEE Transactions on Acoustics, Speech, and Signal Processing 29, no. 6 (December 1981): 1153-60. https://doi.org/10.1109/TASSP.1981.1163711.

[2] Smith, Alvy Ray. "Color Gamut Transform Pairs." In Proceedings of the 5th Annual Conference on Computer Graphics and Interactive Techniques - SIGGRAPH '78, 12-19. Not Known: ACM Press, 1978. https://doi.org/10.1145/800248.807361.

See Also

Image Resizer

Related Topics

  • Image Pyramid
  • Runtime Image Resizing
Change Image Size
- MATLAB & Simulink (2024)

FAQs

How to adjust image size in MATLAB? ›

Resize the image, using the imresize function. 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);

How to resize all images 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

What is resizing images and what are the parameters to change the size of the picture? ›

Resizing an image is done using software to add or subtract pixels, and is called resampling. When an image is resampled it increases or decreases the width and height of the image in pixels. There are other ways to resize an image, such as by cropping it to a smaller size.

How can I adjust the size of an image? ›

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 does resize work in MATLAB? ›

resize uses the default fill value for the data type of each variable. The default fill value for each data type is the same as the value of elements that MATLAB® creates when assigning a value beyond the last row of the table. Specify a custom fill value for the elements to add to each timetable variable.

What is true size image in MATLAB? ›

truesize( fig , [mrows ncols] ) adjusts the display size of an image in a figure, fig , to the dimensions [mrows ncols] , in pixels. truesize( fig ) adjusts the display size such that each image pixel covers one screen pixel. If you do not specify a figure, truesize adjusts the display size of the current figure.

How to check image dimensions in MATLAB? ›

You must use [height, width, colour_planes] = size(image); because images have 3 dimensions. The third dimension is the number of colour planes. If you don't need this value, you can substitute ~ to ignore it.

How do I increase my image resolution? ›

Change resolution by adjusting pixel dimensions.

If you have specific needs for the dimensions or size of your image — like a poster at a certain print size — tick the box for Resample. This allows you to adjust the print size and resolution independently, which changes the number of pixels in the image.

How do I maximize image quality? ›

Try enhancing the overall look of an image in Photoshop using the Camera Raw filter or sharpening the details in a photo using the sliders in the editing panel in Adobe Photoshop Lightroom. Try these methods to get the highest quality you can out of the digital information you have in your image.

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 can be used to change the size of an image? ›

Answer. Photo Resizer Tool is used to change the size of an image.

Which tool is used to change the size of an image? ›

The photo resizer tool( from Microsoft) is used to adjust the size of an image.

Which option is used to change the size of the image? ›

Explanation: The Resample Image option lets you change the size of an image without changing the resolution.

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 to change aspect ratio of image in MATLAB? ›

The plot box aspect ratio is the relative lengths of the x-axis, y-axis, and z-axis. By default, the plot box aspect ratio is based on the size of the figure. You can change the aspect ratio using the pbaspect function.

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.

Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6430

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.