You are on page 1of 4

Introduction to Image Processing with Matlab

How do I write Matlab programs? Matlab programs can be written straight to command line (1), or in m-files. M-files are created by (2), or writing edit functionName on command line (1). Created files are stored as fileName.m to current directory, which you can change in (3). You can, for example in case

your files organized.

(2)

(3)

(1)

Functions and scripts what are they? Script is simple m-file that executes all lines as they were written in the command line. It hasheaders or anything, just plain program lines. Scripts see all variables created in no command line (i.e. workspace) and vice versa. Functions are advanced scripts, that take can parameters and can produce outputs. Examples of both a script and a function are take given below.

outpu2,] = functionName(param1, param2) on command line. In case of single output, drop the []s. Matlab has numerous functions to help your work. You can find out how to use a function by typing help functionName on command line. Figures and graphics You can display images and vectors on figures. The most common function to do these things stem, imshow and imagesc. Learn more of them by typing help functionName on are plot, command line. When a graphic-function is called, it produces graphics on current figure, which is a separate window for graphics. If there are no existing figures, function creates one. Current figure is the figure which is viewed last, or taken action upon. You can create a figure surprise, surprise with the command figure. Each figure is identified with a number, and you can set an individual figure to current figure with the command figure(number). You can attach multiple graphics to one figure, for example, plot some markers on an image with the command hold and hold on/off. Hold changes current figures state between hold on and holdIn a hold off state, graphics replace one another, and in hold on state, graphics are off. cumulated. Parallel display of graphic objects can be achieved with subplot function. Subplot divides figure to an array. For example, the command figure,subplot(4,2,5),imshow(image), creates a new figure, divides it to 4x2 array and the image in the 5th sub window, counted row-wise. Another example: the lines displays
figure(1),imshow(image),title(Kuva 1.) hold on plot( 50, 50, r*) hold off

displays an image to the figure no.1 (if doesnt exist, creates it) and draws a red *-mark on pixel (50,50).

How to get images to workspace? This one is the easy part just use the function imread. This returns a matrix carrying the pixel values. If the image is color image, function returns an MxNx3-array (M = no. of rows, N = no. of columns). In case of a grayscale image, function returns an MxN-image. Note that images are created as unsigned integer, and if you want to do some fancy mathematical operations on them, I advice to convert to double-format with the function double. Color images can be transformed to grayscale with rgb2gray. Following example reads a color image, transforms it to grayscale and creates a copy of it in doubleformat. im1 = rgb2gray(imread(image1.bmp)); im1d = double(im1); In the example, the image must exist in the current directory, but to import an image outside the current directory use the whole path (for example c:\temp\images\image1.jpg ). How to process images? Now you have the images in the right form in the workspace as matrices. The basics of matrix manipulation you have to learn elsewhere, for example in the Matlabs begin here tutorial, illustrated in the beginning of this document. In the same illustration, you can find the part Image Processing Toolbox. There you can find lots of existing functions for image processing. You can study them there. To get more functions, you can try google or browse through the Mathworks File Exchange archives at: http://www.mathworks.com/matlabcentral/fileexchange.

You might also like