Input/Output and Display
You can use the imread function to read an image into the MATLAB environment. The basic syntax of imread is:
imread('filename')
Here, filename is a string containing the full name of the image file (including any available extensions). For example, the statement:
>> f = imread('chestxray.jpg');
reads the JPEG image chestxray into the image array f. Note that the single quotes (‘) are used to delimit the filename string, and the semicolon at the end of the command line is used in MATLAB to suppress output.
If the semicolon is not included in the command line, MATLAB will display the result of the operation specified by this command line.
When the prompt (>>) appears in the MATLAB command window, it indicates the start of the command line.
To display an image on the MATLAB desktop, use the imshow function. The basic syntax of imshow is:
imshow(f)
where f is the image array. The following statements can also display an image named rose_512.tif from disk using the imshow function:
>> f = imread('rose_512.tif');
>> imshow(f)
If another image g is subsequently displayed using imshow, MATLAB will replace the image in the figure window with the new image.
To keep the first image and output the second image, you can use the following figure function:
>> figure, imshow(g)
To write an image to the current directory, use the imwrite function with the following basic syntax:
imwrite(f, 'filename')
The imwrite function can also have additional parameters depending on the file format to be written. Most work in subsequent chapters will either deal with JPEG images or TIFF images, so we will focus on these two formats.
A more commonly used imwrite syntax that is applicable only to JPEG images is:
imwrite(f, 'filename.jpg', 'quality', q)
where q is an integer between 0 and 100 (due to JPEG compression, the smaller the q, the more severe the degradation of the image). A more commonly used imwrite syntax that is applicable only to TIFF images is as follows:
imwritex(g, 'filename.tif', 'compression', 'parameter', ...)
'resolution', [colres rowres]
where parameter can take the following values: 'none' (indicating no compression), 'packbits' (default for non-binary images), 'lwz', 'deflate', 'jpeg', 'ccitt' (only for binary images, default value), 'fax3' (only for binary images) and 'fax4'.
The 1×2 array [colres rowres] contains two integers, giving the column resolution and row resolution in dots per unit (dots-per-unit) (default value is [7272]). For example, if the dimensions of the image are in inches, then colres is the number of dots per inch in the vertical direction, and similarly, rowres is the number of dots per inch in the horizontal direction.
Specifying the resolution with a single scalar res is equivalent to writing [res res].
End
Copyright Statement:
-
This article is sourced from “MATLAB Implementation of Digital Image Processing (Second Edition),Image: Xiaowei, please delete if infringing
-
Copyright belongs to the relevant rights holders. If there is any improper use, please contact us.
-
This public account shares articles for learning and communication purposes only, please do not experiment casually.

Click “Read the Original” for us to progress together.