Thursday, January 20, 2011

Matlab Basics 1 : How to read and display image

Reading an image

The simplest way to read an image is to store the image in the current directory (workspace) of Matlab and then use the function imread as follows:

im = imread('aa.bmp'); %% where aa.bmp is the name of the image

%% the image data has been stored in the variable 'im'

Most of the commonly used formats like jpg, bmp, png are accepted by the imread function.

Alternatively if the image is in a different directory one can use the imread function by specifying the total path of the image

for example:

im= imread('C:/Users/Admin/Desktop/aa.jpg'); %% reads from the folder admin

Displaying an image

The command to display images is as follows:

imshow(im); %% will show the image

The problem here would be that the image would replace the current open image (if any).

For this it is better to use :

figure, imshow(im); %% will open the image in a new window

Once can even name the window using int values to use them for various purposes of display and processing post the display. For example :

figure(5), imshow(im);

To operate several graphic commands (or plot commands) one can use the option 'hold'.

Default value of hold is set to the stage off. Enabling it to 'on' state enables it to hold the current plot and all axis properties so that subsequent graphing commands add to the existing graph.

An interesting graphing command :

Suppose you want to find the co ordinates of a particular point in an image using matlab.

you can use the command:

[x, y]=ginput(i); plot(x, y, '+r'); %% where i is to replaced with the figure number in this case, for the above example we used 5

and then click on the point on the respective image.

the co ordinates will be stored in the matrix [x y].


  1. wao thanks for sharing this awesome stuff here ...........

    Khurram Shahzad
    Alaziz Online

  2. Is there any relation between histogram process and contrast stretching function

  3. This comment has been removed by the author.