How to Read an Image File in MATLAB?


Reading an image file is the first step in any digital image processing task. In MATLAB, reading an image file means the process of inputting or loading the digital image into the MATLAB workspace and store it in a variable.

Let us now understand the process of reading an image file in MATLAB with the help of examples.

Read an Image File in MATLAB

In MATLAB, there is a built-in function named "imread" in the function library that is used to read an image file.

Syntax

img = imread(imageFilePath);

Here, "img" is a variable to store the input image.

The most important feature of the "imread" function is that it can automatically determine the format of the image file depending on the file extension.

It reads and loads the image data in the form of a matrix, where elements of the matrix denote the pixel values of the image.

In MATLAB, if the input image is a gray scale image, then the "imread" function stores the pixel values in a 2-dimensional matrix. If the input image is a colored image, then the matrix would be a 3-dimensional matrix to store the RGB pixel values of the image.

Example

Let us now take an example to read an image file in MATLAB.

% MATLAB code to read an image file
% Call the imread function to read the image file
I = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Display the image file
imshow(I);

Output

When you run this code, it will produce the following output −

In this example, the "imread" function is used to read the image file and the "imshow" function is used to display the loaded image file.

Conclusion

This is all about reading an image file in MATLAB. In MATLAB, there is a built-in function "imread" that is used to read any kind of image file. In this brief tutorial, I explained the process of reading an image file with the help of an example.

Updated on: 06-Oct-2023

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements