Updated March 8, 2023
Introduction to Matlab Imread
Imread function is used in MATLAB to read images or color scales from graphic files, which are in the formats such as ’bmp,’ ‘cur,’ ‘gif,’ ‘jpg,’ ‘hdf,’ ‘ico’ etc. If no format is mentioned in the syntax of the Imread function, it infers the format from the contents of the file.Imread function only “reads” an image into a variable initiated by us; in order to display the image on our work, we need to use the ‘imshow’ function in conjunction with the imread function.
The graphic file from which we read the image can be in a MATLAB directory or a file saved by us.
Below is the table showing some of the formats supported by the Imread function:
Format | Description | Variants |
1. ‘bmp’ | Bitmap | 1, 4, 8, 16, 32-bit uncompressed image |
2.‘cur’ | Cursor resources | 1, 4, 8-bit uncompressed image |
3.‘gif’ | Graphics Interchange Format | 1 to an 8-bit image |
4.‘hdf’ | Hierarchal Data Format | 8-bit raster, with or without colormap |
5.‘ico’ | Icon resources | 1, 4, 8-bit uncompressed image |
Let us now understand the syntax of the Imread function in MATLAB:
Syntax
- I = imread (file)
- I = imread (file, fmt)
Description:
- I = imread (file) will read the image specified by the argument ‘file.’ Since we have not passed any format argument in this syntax, it will infer the format from the file’s contents. In case the file has multiple images, imread will read only the 1st image. If we want imread to read an image other than the first image, we will have to pass the number of the image as an argument.
- I = imread (file, fmt) will read a color image or a greyscale present in the file passed as an argument. In this syntax, we have also specified the format of our file.
Examples of Matlab Imread
Let us now understand the code to read an image from a file in MATLAB using the ‘imread (file) function’ with the help of various examples.
Example #1
In this example, we will read an image from the ‘moon.tif’ file, which is present in MATLAB’s directory. Below are the steps to be followed:
- Call the imread function to read the image.
- Call the imshow function to display the image read in our workspace
Code:
I = imread (‘moon.tif’)
imshow (I)
Input:
I = imread ('moon.tif')
imshow (I)
Output:
As we can see in the output, we have obtained an image of the moon, which we read from MATLAB’s file.
Example #2
In this example, we will read an image from the ‘kids.tif’ file present in MATLAB’s directory. Below are the steps to be followed:
- Call the imread function to read the image.
- Call the imshow function to display the image read in our workspace
Code:
I = imread (‘kids.tif’)
imshow (I)
Input:
I = imread ('kids.tif')
imshow (I)
Output:
As we can see in the output, we have obtained an image of the kids, which we read from MATLAB’s file.
In the above 2 examples, we read the images present in the first position in our input files. Next, we will learn how to read a particular image if the file has multiple images.
Example #3
In this example, we will read the 3rd image from the ‘corn.tif’ file, which is present in MATLAB’s directory. Below are the steps to be followed:
- Call the imread function and pass ‘3’ as 2nd argument to read the third image.
- Call the imshow function to display the image read in our workspace
Code:
I = imread (‘corn.tif’, 3)
imshow (I)
Input:
I = imread ('corn.tif', 3)
imshow (I)
Output:
As we can see in the output, we have obtained an image present at position 3 in the file ‘corns.tif,’ which was read by us from MATLAB’s directory.
In all the above examples, we have read the image from the ‘tif’ file. Next, we will learn how to read an image from the ‘png’ format.
Example #4
In this example, we will read an image from the ‘peppers.png’ file, which is present in the MATLAB directory. Below are the steps to be followed:
- Call the imread function to read the image.
- Call the imshow function to display the image read in our workspace
Code:
I = imread (‘peppers.png’)
imshow (I)
Input:
I = imread ('peppers.png')
imshow (I)
Output:
As we can see in the output, we have obtained an image of the peppers, which we read from a png file.
Conclusion
- Imread is used in MATLAB to read an image from a graphic file. MATLAB supports various formats from which we can read images
- Imread function can also be used to read images from multi-image files
Recommended Article
This is a guide to Matlab Imread. Here we discuss the various Examples of Matlab Imread along with the codes and outputs. You may also have a look at the following articles to learn more –