Resize an Image in Matlab

by admin in How To on November 18, 2019

MATLAB stores most images as two-dimensional matrices, in which each element of the matrix corresponds to a single discrete pixel in the displayed image. Some images, such as truecolor images, represent images using a three-dimensional array. In truecolor images, the first plane in the third dimension represents the red pixel intensities, the second plane represents the green pixel intensities, and the third plane represents the blue pixel intensities.

Image Resize using imresize():

Image resize changes the size of an image. There are two ways of using the imresize column. if the input image has more than two dimensions imresize only resizes the first two dimensions.

  • J = imresize(I, scale) : The method takes the input image I as input and a scaling factor and scales the input image with that factor. For eg. if we choose 0.5 as the scaling factor every two pixels in the original image is mapped to one pixel value in the output image for both the dimensions.
  • J = imresize(I, [numrows numcols]) : The methods takes the number of rows and columns and fits the original input image to an output image having the specified number of rows and columns.

Code #1: Read the image from file

% read image file
I = imread('image.jpg');  
% display image size
size(I)  
% display the image figure,
imshow(I);

Output :

ans = 371   660     3

Code #2: Resize by scaling

% compress the image and save 
% in another variable
I1 = imresize(I, 0.5);  
% display image size
size(I1)  
% display the image figure,
imshow(I1);

Output :

ans = 186   330     3


Code #3: Resize with specified rows and columns

% resize by specifying rows and columns
I2 = imresize(I, [100, 200]);  
% display image size
size(I2)  
% display the image figure,
imshow(I2);

Output :

ans = 100   200     3

Summarizing the Code in one Place:

% This file resize the image to desired size
% Run the code section by section NOT at once.
%********************************
% Created By: CodeToBuy         *
% Date: 09/2019                 *
% website: www. CodeToBuy.org   *
% mail:  [email protected]    *
%********************************

close all; clear all;

%% Section 1
% Read an Image
I = imread('cameraman.tif');
% see size of image
size(I)
% resize the image
ir = imresize(I,[100 100]);
size(ir)

%%   Section 2
% resize by specifying rows and columns
I2 = imresize(I, [100, 200]);  
% display image size 
size(I2)  
% display the image figure, 
imshow(I2); 

%%   Section 3
% compress the image and save  
% in another variable 
I1 = imresize(I, 0.5);  
% display image size 
size(I1)  
% display the image figure, 
imshow(I1);   

Categories

Share Your Valuable Opinions