If you are working with images in Matlab for image processing you need to learn matlab commands and functions to import and export the images. The following is a quick illustration of how to read images, display images and write images in matlab software.

Reading Images

To read images into matlab you can use the imread function. The syntax is


In the above syntax, filename is the name of your image file. For example, xray.png or xray.jpg etc.

>> imread('xray.jpg');

This will read the xray.jpg from the current working directory. But suppose your image file is stored in some other directory, for example in D drive: D:\images\ray.jpg. Then you can specify the direct image directory path into the filename. For example.

>> imread('D:\images\ray.jpg');

 The above two commands will import it but the images are not assigned to any variable. To assign to a variable like f we write:

>> f = imread('xray.jpg');
>> f = imread('D:\images\ray.jpg');

 The supported image formats or extensions are as follows:

Now f which is the image is a matrix which was explained in How images can be represented as Matrix. Thus f has M rows and N columns. To prove this we can use the size function which gives us the row and column values.

>> size(f)
ans =
1024 1024

where the first value is the number of rows(M=1024) and the second is number of columns(N=1024).

We can store these values in M and N variables as follows.

>> [M,N] = size(f)

This is useful for programming. Now have the image and size of the image for purpose of further image processing.

If you want detailed information about the image then you should use the whos function like so-

>> whos f

This gives you size of the image f, the total bytes of the image and the data type(or class) of the image. This information is required for example if you are compressing the image and you want to know the original image size and the data type used to represent it.

Displaying Images

Until now, the image was imported into matlab. It is not displayed yet on the output display. So to view the image that was imported we use the imshow command. The imshow has the following syntax.

>> imshow(f,G)

where now G is the number of intensity level or the quantization level. The default quantization level is 256 so if ommitted you will be using 256 quantization level for display.

We can display black and white colored image in matlab using the following syntax:

>> imshow(f,[low,high]

If used, all pixel values lower than low value will be displayed black. All pixel value higher than high will be displayed white.

If you want to get the pixel intensity at a point then you can use the pixval command. It is an interactive command, meaning that once this command is issued you are shown the image and when you place the cursor onto the image, the coordinate and the intensity is shown.

If you want to open multiple images then you can use the figure command after the first image has been displayed followed by the second image display command. This is as follows.

>> imshow(picture1.jpg)
>> figure
>> imshow(picture2.jpg)

Now you will see both the images but on different windows.

Saving Images

Once you are done with importing, viewing and most probably editing the image you want to save the image somewhere in your computer. The matlab command to save your image is the imwrite. The syntax is

imwrite(f, 'filename')

For example,

>> imwrite(f,'xray.jpg')

If you want to save the image in some other supported image format like png then you can use the following.

>> imwrite(f,'xray.png')

In the above cases we did not provide any file path so the image will be saved in the current working directory. If you wanted to save the image in some folder of your choice like D:\images then you will write the following.

>> imwrite(f,'D:\images\xray.png')

The book digital image processing using matlab by Rafael C. Gonzalez and Richard Eugene Woods explains this in more details. See also download Mathworks Matlab R2016a ISO free.


Post a Comment