Image Processing With MATLAB
Image Processing With MATLAB
Image Processing With MATLAB
Students will be required to keep a laboratory notebook which will be submitted at the end of the semester. In each of the sessions described in these notes there are a series of exercises that students are asked to work through. Students will record all comments and observations on these exercises in their laboratory notebooks in sufficient detail that someone reading the notebook could reproduce their results. The day-book is a diary, a journal, a day by day, hour by hour, record of the work which you do in the pursuit of the objectives of the practical work. In it, you should make an accurate and comprehensive record of your activities, the resources used, the results
obtained and such notes on interpretation, planning and other organisation of the work, that another person, conversant with the area of your work, could reconstruct your activities, in full. Thus, the laboratory notebook records failure as well as success. * It records good ideas and bad ideas. * It records leads which produce the big breakthrough, as well as those which do not. * It records decisions to do things and decisions to not do things. * It records things which are undecided. * It records how tests were set up. * It records raw results, as tables, technical sketches, graphs, &c. * It records references to sources of ideas and information. It is a chronological record; one made at the time and place of the activity. It is not a formal report, neatly written up after the event, edited to produce a consistent and logical flow of ideas and actions. It is life in the raw. It contains working notes, made as thoughts and plans are conceived, modified in real time as the ideas develop. It is a book of rough working. Nonetheless, the records should be legible. The records in a laboratory notebook must be dated and the pages are numbered. By following your laboratory notebook, another person should be able to reconstruct (literally, if necessary) all the work which you have performed, in the order in which you performed it. You should expect your day-book to be inspected and commented on through the course of the semester by the lab supervisor or laboratory demonstrator. When this happens, they will sign and date the book at the latest record. Assessment will be based on the following breakdown of marks. 10% for attendance. 30% for the quality of record keeping in the laboratory notebook based on the criteria discussed above. 4 x 15% that is 15% for each one of four coursework submissions spread throughout the semester. Note: All work must be the students own work and plagiarisms and copying will be subject to penalty. The timetable for submitted assessments will be as follows: Week 4: Assessed Coursework exercise on Fourier Analysis of images Week 8: Assessed Coursework on Histogram Techniques Week 6: Assessed Coursework on Image Filtering Week 10: Assessed Coursework on Hough Transform
For each of these assessments the students will submit a 1 page description of the problem and their approach to the solution. Flow charts where appropriate illustrating how they have structured their solution. Programmes used to solve the problem. All solutions must be submitted via WebCT by the 5pm on the Friday of the weeks in which an assignment is due.
Session 1
2. What is MATLAB ? MATLAB is a high performance language and software for technical computing. It allows Maths and computation Algorithm development Modelling, simulation and prototyping Data analysis and visualisation Scientific and engineering graphics It is an interactive system (all data always in memory) with the following key facts: The basic data element is an array No declaration (or prototyping of functions) required All data represented as doubles In script mode or command line mode, data is always accessible and in memory. 3, Basic Commands and Getting Started 3.1 Knowing where you are: Use the pwd command to know where you are Use the dir command to list the files in your current directory Use the cd command to change directory Now go to your home directory to be able to save todays work 3.2 The help command: You can get help on all commands in matlab by typing (beware, matlab is case sensitive!): >> help command For instance, try typing >> help help And read what pops up in the screenThat should give you plenty of information on how to find information on topics. For instance, if you are looking for the cosine function, type >> help cos Note that if you press the "up" and "down" arrows on your keyboard, this enables you to recall previous commands. 3.3 The lookfor command
If you do not know what is the name of the matlab command you want to use the lookfor command is what you are looking for. For instance, if you are looking for the cosine or related functions, type >> lookfor cosine matlab comes back with all matlab functions related to cosine with a short description as:
ACOS Inverse cosine. ACOSH Inverse hyperbolic cosine. COS Cosine. COSH Hyperbolic cosine. TFFUNC time and frequency domain versions of a cosine modulated Gaussian pulse. DCT2 Compute 2-D discrete cosine transform. DCTMTX Compute discrete cosine transform matrix. DCTMTX2 Discrete cosine transform matrix. IDCT2 Compute 2-D inverse discrete cosine transform. CHIRP Swept-frequency cosine generator. DCT Discrete cosine transform. FIRRCOS Raised Cosine FIR Filter design. IDCT Inverse discrete cosine transform. DCT Discrete cosine transform. IDCT Inverse discrete cosine transform. dctold.m: %DCT Discrete cosine transform. idctold.m: %IDCT Inverse discrete cosine transform. BLKACOS This block defines an output angle that is the arccosine of the input. BLKCOS This block defines the output as the cosine of the input. BLKCOSASIN This block defines the cosine of an angle whose sine is u. BLKCOSATAN This block defines the cosine of an angle whose tangent is u1/u2.
Now type help cos to get details on the cosine function. >> help cos Note that if you press the "up" and "down" arrows on your keyboard, this enables you to recall previous commands.
>> x This will display the contents of x on the screen >> who This will tell you about all the matrices/vectors you have in your current workspace. >> whos This will provide more details about these matrices/vectors. >> y = [6; 7; 8; 9; 0; 9; 8; 7; 6] Create a new matrix called y of size 9 by 1. (note that is there is a semicolon at the end of the instruction, matlab doesn't expand the result of the instruction) >> y' Take the transpose of y >> z = [1 2 3; 4 5 6; 7 8 9; 0 1 2] Create a new matrix called z
5. Operators:
NOTE: MOST OF THEM WORK ON MATRICES DIRECTLY. 5.1 The colon operator: A way to define quickly some vectors is to use the ":" operator. Type for instance (and observe the results): >> >> >> >> >> >> a b c d e f = = = = = = 0:10 -5:10 0:2:10 10:-2:-5 0:0.01:4.2 -pi:0.01:pi
You can see that these instructions are of the form lower limit: increment: upper limit. If no increment is given, it is assumed the increment is 1. If you wish to define elementary vectors that only contain ones and zeros, then the commands zeros and ones are useful. Also randn to create a matrix of random numbers. Look at the help on zeros, ones, randn. To illustrate ones and zeros try:
Try: >> g = zeros(2,4) >> h = ones(5,3) 5.2 Arithmetic operators: These are the standard operators. (+ plus, - minus, / divide, * multiply, ^ exponent) Try >> >> >> >> >> 3^2 y' x+y' x*y 3*x
The operators ( / , * and ^) have also got a matrix counterpart (respectively ./ , .* and .^), which apply the operation element by element. For instance, compare: x*y and x.*y. Type: >> >> >> >> x^2 x.^2 x.*x x*x
if you type "whos" now, you will see that you have a lot of variables in the workspace. Type clear, and whos again and you will see that your workspace has been cleared. 5.3 Matrices and operators: Type >> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] >> sum(A) % displays the sum of the columns of A. This will display on the screen >> 34 34 34 34 To investigate further >> sum(sum(A)) % displays the sum of A >> A %Transpose conjugate (if complex number) else transpose. >>sum(A) % Sum of the lines of A (columns of A). Note that functions can be combined very easily.
Type as given below to find out what is in element (4,2) >> A(4,2) However if you try >> A(4,5) You will get ERROR: index exceeds matrix dimension! 7. The functions available and the toolboxes Type: >> help elfun % elementary functions >> help specfun % special functions >> help elmat % elementary math functions to see some of what's there. Try out some of the functions. For instance, type: >> t =-pi:0.01:pi; >> c = cos(t); >> s = sin(t); Important toolboxes are available: Image processing toolbox - for more information >> help images Signal processing toolbox - for more information >> help signal Statistics toolbox - for more information >> help stats 6. Plotting functions: Type >> >> >> >> >> >> figure plot(s) figure plot(t,s) figure plot(t,s,'g',t,c,'r')
Type help plot if you haven't already done it to see more possibilities. Try and modify the axis, put a title and labels on the x- and y- axes Add a title and a label on the x-axis. 7. Image processing:
Try
im1 = imread(peppers.png); % Reads an image and puts it in im1 im2 = randn(384,512); % Creates a 384x512 normally
% distributed image (gaussian noise) % of the size of im1. whos % Note that images are stored in uint8 to % save memory space Try
imshow(im1); % Display matrices as images imshow(im2);
Try
figure(1) imshow(im1); % Display matrices as images figure(2) imshow(im2); % Same thing with 2 windows.
Play with the figure menus and especially the export button that allows saving images and figures. Type help figure for more information. Try
subplot(2,1,1) imshow(im1); % Display matrices as images subplot(2,1,2) imshow(im2); % Same thing with 2 images in one
% window. Try
clf; % Clear figure imshow(im1(:,:,1)) % Displays Red component of the image
Try
clf; subplot(3,1,1) imshow(im1(:,:,1)) % Displays Red component of the image title(Red Component); subplot(3,1,2) imshow(im1(:,:,2)) % Displays Green component of image title(Green Component); subplot(3,1,3) imshow(im1(:,:,3)) % Displays Blue component of the image title(Blue Component);
% double type. 7.1 Operators and images: Addition: Try ima = im1 + im2; imshow(ima); Substraction: Try ima = im1 - im2; imshow(ima); Multiplication: Try ima = im1*im2; This is a matrix multiplication which cannot be applied to multiply images. The correct way to do this is: ima = im1 .* im2; imshow(ima); Other operators / Matrix division ^ Power operator Complex transpose The combination of . and any other operator makes it an element to element operator. For instance .* or ./ multiplies and divides each element of the first image by the element of the second.
To load it load name Do help load to learn more about load. YOU CANNOT SAVE ANYWHERE. YOU NEED TO HAVE WRITE PERMISSION.
Session 2
1. Matlab resources: During this session, you will learn: 123456Basic flow control and programming language How to write scripts (main functions) with matlab How to write functions with matlab How to use the debugger How to use the graphical interface Examples of useful scripts and functions for image processing
After this you will know most of what you need to know about Matlab and should definitely know how to go on learning about it on your own. So the "programming" aspect won't be an issue any more, and you will be able to use matlab as a tool to help you with you image processing. Programming languages don't come any easier! 2. Matlab resources: