Some basic commands for image processing in MATLAB


1)Reading an Image
To import an image from any supported graphics image file format, in any of the supported bit
depths, use the imread function.

Syntax A = imread(filename,fmt)

Description

A = imread(filename,fmt) reads a greyscale or color image from the file specified by the string
filename, where the string fmt specifies the format of the file. If the file is not in the current
directory or in a directory in the MATLAB path, specify the full pathname of the location on
your
system.
>> b= imread('C:\Documents and Settings\bond\My Documents\ld1 (8 x 8).jpg')\



2)Display An Image
To display iamge, use the imshow function.

Syntax imshow(A)

Description
imshow(A) displays the image stored in array A.
>> a = [0 0 255 0 0 0 0 255;0 0 255 0 0 0 255 0;0 0 255 0 0 255 0 0;0 0 255 0 255 0 0 0;0 0 255
255 0 0 0 0;0 0 255 0 255 0 0 0;0 0 255 0 0 255 0 0;0 0 255 0 0 0 255 0;0 0 255 0 0 0 0 255];
>>imshow(a)


3)Writing Image Data/ Writing Image to Disk
Imwrite Write image to graphics file

Syntax
imwrite(A,filename,fmt)

Description

It writes the image to filename, inferring the format to use from the filename's extension. The
extension must be one of the values.
>> a = [0 0 255 0 0 0 0 255;0 0 255 0 0 0 255 0;0 0 255 0 0 255 0 0;0 0 255 0 255 0 0 0;0 0 255 255 0 0
0 0;0 0 255 0 255 0 0 0;0 0 255 0 0 255 0 0;0 0 255 0 0 0 255 0;0 0 255 0 0 0 0 255];

>>imwrite(a,'b1.jpg','jpeg')
>>imshow(‘b1.jpg’)


4)How to get no. of rows and columns of image
Syntax [r,c]=size(a)

Description Function size gives the rows and columns dimension of image

Accessing the Pixel data
There is a one-to-one correspondence between pixel coordinates and the coordinates MATLAB uses
for matrix subscripting. This correspondence makes the relationship between an image's data matrix
and the way the image is displayed easy to understand. For example, the data for the pixel in the fifth
row, second column is stored in the matrix element (5,2). You use normal MATLAB matrix
subscripting to access values of individual pixels.
For example, MATLAB Code A(2,15) returns the value of the pixel at row 2, column 15 of the image A.

5)How to get Image Information
Syntax info = imfinfo(filename)

Description
It returns a structure, info, whose fields contain information about an image in a graphics file.
filename is a string that specifies the name of the graphics file.The file must be in the current
directory or in a directory on the MATLAB path.

>> imfinfo('C:\Documents and Settings\bond\My Documents\ld1 (8 x 8).jpg')


6)Convertion of Image

Syntax Im2unit8
Description Convert image to 8-bit unsigned integers
I1 = im2uint8(I)

Syntax Im2bw
Description Convert image to binary image, based on threshold.
BW = im2bw(I, level)


Syntax Im2double
Description Convert image to double type image.

I2=im2double(I)


7)Displaying gray scale image
Syntax imtool(I,[low high])

Desription
It displays the grayscale image I, specifying the display range for I in the vector [low high]. The
value low (and any value less than low) is displayed as black, the value high (and any value greater
than high) is displayed as white.


8)Changing the size of Image
Syntax B = imresize(A, scale)

Description
It returns image B that is scale times the size of A. The input image A can be a grayscale, RGB,
or binary image. If scale is between 0 and 1.0, B is smaller than A. If scale is greater than 1.0, B
is larger than A.

Example B=imresize(a,0.5) making image B half of the image size of A.
B=imresize(a,[512 512]) making image b with size of 512X512.


9)Rotating the image
Syntax B = imrotate(A,angle)

Description
It rotates image A by angle degrees in a counter clockwise direction around its center point. To
rotate the image clockwise, specify a negative value for angle. imrotate makes the output image B
large enough to contain the entire rotated image. imrotate uses nearest neighbour interpolation,
setting the values of pixels in B that are outside the rotated image to 0 (zero).


10)Reading the color value of an Image
Syntax P = impixel(I)

Description
impixel returns the red, green, and blue color values of specified image pixels. In the syntax,
impixel displays the input image and waits for you to specify the pixels with the mouse.




11)Converting color Image to Gray scale Image
Syntax I = rgb2gray(RGB)

Description
It converts the truecolor image RGB to the grayscale intensity image I. rgb2gray converts RGB
images to grayscale by eliminating the hue and saturation information while retaining the
luminance.


12)Detecting edges of an Image
Syntax BW = edge(I)

Description It takes a grayscale or a binary image I as its input, and returns a binary image BW of the
same size as I, with 1's where the function finds edges in I and 0's elsewhere.