Вы находитесь на странице: 1из 15

School of Physics, Astronomy and Mathematics

4PAM1008 MATLAB Coursework


2012, Semester B

tr11abf March 2012

4PAM1008 MATLAB Coursework 2012


Instructions
You will need the following files all of which can be accessed through the StudyNet assignment page: 1. 2. 3. 4. 5. Coursework assignment document this file. PixelHighlight.m LogImageDisplay.m IMAGE001.JPG IMAGE002.JPG

Enter your answers in this document as directed It is strongly recommended that you develop your code using m-files. Once the code is finished and de-bugged you can copy that code into your answer script and also re-generate data and figures instantly if required. Not every part of a question will necessarily require MATLAB. You must include the MATLAB code you have written with your answers. Always include any plots you are asked to generate. If you have been using the diary feature, do NOT simply copy and paste your diary file and submit that as an answer. You are required to provide well presented and formatted MATLAB code in your answers where directed.

When you have completed your coursework you will need to submit the following online: 1. This document with completed answers. 2. Your completed function files for Task 2 a. PixelHighlight.m b. LogImageDisplay.m

To submit your work, go to the 4PAM1008 module page on StudyNet, and select Assignments from the menu on the left of the page. Then press Submit assignment next to the MATLAB coursework, and follow the on-screen instructions.

Student Details
Please enter your name (as it appears on your student card) and student number below:

Student Name

THOMAS ROBERTS 11568941

Student Number

4PAM1008 MATLAB Coursework 2012


Task 1: Mean Free Path

The mean free path is the average distance a particle can travel before colliding with another. To determine this, we can use the equation given below, which assumes all the particles have the same diameter and that the particle moving through the volume is moving very fast with respect to the others.

Where d is the diameter of the particles, and nv is the number of particles per unit volume.

Question 1
Using the ideal gas law, Equation (2), calculate nv for the number of particles per unit volume at standard atmospheric pressure (1.01x105Nm-2), and room temperature (293K).

Type your answer in the shaded box below.

2.4979e+025

(1 mark)

Where P is pressure, V is volume, N is the number of molecules, kB is the Boltzman constant (1.38x10-23JK-1) and T is temperature.

4PAM1008 MATLAB Coursework 2012


Question 2
Use your answer to question 1 to calculate the mean free path, l, in metres of a molecule in air assuming an average diameter of a molecule is 2x10-10m3.

3.1858e-007

(1 mark)

Question 3.
Use MATLAB to calculate the mean free path for particles in the range suitable interval to generate at least 100 values. Assume room temperature and pressure Plot a graph showing how the mean free path changes with particle size. In the box below: 1. Copy the graph created in MATLAB (in the figure window select edit -> Copy Figure) and paste. 2. Copy and paste the code used to answer Q3. m using a

1.4

x 10

-6

(4 marks)

1.2

0.8

0.6

0.4

0.2

9 x 10

10
-10

x = (1*10^(-10)):0.05*(10^(-10)):(1*10^(-9)); Lx = 1./(pi*(x.^2)*N); plot(x,Lx)

4PAM1008 MATLAB Coursework 2012

Question 4
Consider now 1000 particles at standard temperature and pressure. Using the ideal gas equation, calculate the volume occupied.

4.0034e-023

(1 mark)

Consider this volume as a tiny cube and calculate the length of the sides

3.4209e-008
Question 5

(1 mark)

In this question you are going to create 1000 random x,y,z coordinates to represent the positions of these particles inside the cube whose size you determined in Q4 and then plot them in a 3-D scatter chart Use the rand function to generate a 1000 x 3 matrix of x,y,z coordinates in the range specified by the size of the cube calculate in Q4. Remember that the rand function by default generates random numbers between 0 and 1 so you will have to scale the results to be between 0 and the length of the side of the cube. We will consider column 1 to be x-values, column 2 to be y-values and column 3 to be z-values. Now plot those locations using the scatter3 function to plot each particle location as a point (look at the MarkerType property in the help documentation for scatter3. Label the axes as X, Y and Z and add a title to the plot: Particle Locations. In the box below: 1. Copy the graph created in MATLAB (in the figure window select edit -> Copy Figure) and paste. 2. Copy and paste the code used to answer Q5.

4PAM1008 MATLAB Coursework 2012


Particle Locations
-8

(5 marks)

x 10 4

0 4 3 x 10
-8

4 2 1 Y 0 0 1 X 3 2 x 10
-8

%% Question 5 a = Side.*rand(1000,3); X = a(:,1); Y = a(:,2); Z = a(:,3); scatter3(X,Y,Z, 'g.') title('Particle Locations') xlabel('X') ylabel('Y') zlabel('Z')

Question 6
Imagine a sphere at the centre of the cube containing the particles whose locations you generated in Q5. In this question you are going to plot the particle locations again as you did in Q5 and then highlight all the particles occupying a sphere of radius 0.3 width of the cube centred on the specified particle. To do this you need to carry out the following steps. 1) Determine the radius of the sphere and put your answer below.

1.0263e-008

(1 mark)

2) You will need to find all the particles in your 1000 x 3 matrix of position values whose location in the box falls within the sphere. For each particle with position x,y,z you will need to do the following logical

4PAM1008 MATLAB Coursework 2012


comparison. r is the radius and the centre of the cube (ie the location of the reference particle) is given by . To do this, implement the above expression as MATLAB code to return indices to each row in your 1000 x 3 matrix of particle positions which satisfy the above comparison. Hint Section 3.2.2 in your notes shows you how to access whole rows or columns of a matrix. Section 3.6 shows you how to do logical operations Use the indices to extract all particle locations from your matrix satisfying the inequality above to create a new n x 3 matrix containing the n particles inside the sphere. Now plot the original 1000 particles exactly as you did in Q5. To highlight the particles within the sphere you simply plot your subset of particles in the same scatter plot but use a different colour. Hint Look at section 4.1 of your notes to see how to plot multiple data sets in the same axes. Copy and paste your figure in the space below.

4PAM1008 MATLAB Coursework 2012


Particle Locations with Central Sphere
-8

(6 marks)

x 10 4

0 4 3 x 10
-8

4 2 1 Y 0 0 1 X 3 2 x 10
-8

4PAM1008 MATLAB Coursework 2012


Task 2: Image Enhancement
Background For many years researchers at the university have been developing techniques to study microscopic atmospheric particles. They use a technique known as laser light scattering. Particles which are only a few microns across are difficult to image conventionally (ie take a photo) because one is approaching the resolution limit. However, if a small particle passes through a laser beam, light is scattered by that particle and if you capture an image (with a CCD camera) of the scattering pattern, its structure will depend on the shape and other characteristics of the particle. Below is an example scattering pattern of a tiny spherical drop.

Figure 1. Typical scattering pattern from a spherical droplet

As you can see the light is scattered in such a way as to produce concentric rings. A fibre would produce a completely different pattern. For this question you will be required to complete two tasks. In each case you will need to write a MATLAB function which changes the way the image is displayed in a manner which assists in the analysis of the images. In order to be able do this you must understand how images are handled in MATLAB. Images in MATLAB You will have been supplied with two images to work with called IMAGE001.jpg (figure 1) and IMAGE002.jpg. These are grayscale, ie no colour, and are represented in MATLAB as a matrix of integer values between 0 and 255. You can use the MATLAB function imread to load an image into the MATLAB workspace. When you do, if you double-click on the matrix containing the image in the Workspace window it will open the matrix in the Variable editor (a spread sheet view of the data). Figure 2 shows a few of the pixel values of one of the images. Each element of the matrix is one image pixel and the value can be between 0 and 255.

4PAM1008 MATLAB Coursework 2012

Figure 2 Variable Editor showing part of an image matrix

Those values are indices which specify a row in a 256 x 3 matrix called a colormap (ie a lookup table) an example of which is shown in figure 3. In MATLAB the brightness of a pixel is represented by a value between 0 and 1 (0 is black and 1 is brightest). The colormap contains 3 columns, one each for specifying the colours red, green and blue.

As an example, consider the pixel value at element 18, 218 in figure 2. The value is 10. This means that the pixel will be displayed with the colour specified in row 11 (figure 3) red = 0.0392, blue = 0.0392, green = 0.0392. Note two important points. 1. You must add 1 to the index value to get the correct row. Thats because in MATLAB the first row (or column) is number 1 but the lowest index value in the image can be 0.

2. You will notice that each row in the table contains identical values That is because this is a grayscale colormap, (which is what you are going to create). To display an image as grayscale, simply have equal values for read, green and blue channels in the colormap.

Figure 3 A grey-scale lookup table (colormap)

The key point to remember is this. The colour / brightness of a displayed image is determined by the colormap. You can change the way an image looks by using different colormaps, you do not have to change the image data.

10

4PAM1008 MATLAB Coursework 2012


Standard colormap function 1

Image display Intensity Value (0 - 1)

0.8

0.6

Thus the color map is a function (figure 4) which maps the input pixel value (x-axis) to the output display value (y-axis). Figure 4 specifies a graylevel colormap completely since all color channels are the same.

0.4

0.2

50 100 150 200 250 Image Pixel Intensity Value (0 - 255)

300

Figure 4: Standard colormap function

Consider figure 5 which is generated from the same image matrix as figure1 but using a different colormap.

Figure 5 Scattering image with false colour

This question requires you to create two MATLAB functions which will change the way images are displayed. In both cases it involves creating and modifying a colormap. You will need to make use of several built in MATLAB functions image imread colormap Displays an image in a figure window Load an image and store in a MATLAB matrix. Manage colormaps.

11

4PAM1008 MATLAB Coursework 2012


Use the MATLAB documentation to learn how to use these and any other functions you require.

Question 1
If you look at the image in figure 1 you can see that near the centre some of the pixels appear completely white. That is, it appears that some of the pixels have a value of 255. Such pixels are said to be saturated and this means the image is overexposed. This can be a problem when trying to analyse the scattering patterns because some of the pattern structure is lost. A quick way to see if an image does contain saturated pixels is to use a colormap which colours all saturated pixels red. In this task you must write the code for the function PixelHighlight.m to colour all saturated pixels red. Figure 6 shows the results you should expect for IMAGE001.

Figure 6: IMAGE001 with saturated pixels highlighted

Use the function file PixelHighlight.m as your template and do the following

1. Create and apply the colormap to highlight saturated pixels in the image. You can use the colormap function to create the standard grayscale colormap and then you only need to do a very slight alteration to highlight saturated pixels. 2. Display the image with the new colormap in a new figure window (use the figure command).

12

4PAM1008 MATLAB Coursework 2012


3. Return the colormap in the output variable myMap. When you create the colormap, assign it to the variable myMap then it will be returned automatically by the function. 4. Return your student number in the output variable studentNumber. 5. Write comments to explain how to use the function. Have a look at how comments are written for other functions. So, when the function is called, the user should see a new figure window with the image displayed and the saturated pixels highlighted in red as in figure 6. Try it out on the other image, see what happens. Copy and paste the text from your function PixelHighlight.m in the box below (Remember, you must also submit the function file along with this document).

function [myMap, studentNumber] = PixelHighlight(im) %PixelHighlight Displays the specified image and highlights saturated pixels. %In the first line, change 'IMAGE001.jpg' to your own image final %The second line loads the colormap gray(256) - this is a 256 bit greyscale %colormap - and renames is ukcolourmap (with 'colour' spelt the English way) %The third line change the fully saturated pixels to display RED only. %The fourth line renames the finished colormap to 'myMap' %The fifth line tells MATLAB to display the image using the modified %colormap.

(8 marks)

% Thomas Roberts % 25 / 04 / 2012 % Replace the number below with your student number. studentNumber = 11568947;

%% Add your code below. im = imread ('IMAGE001.jpg'); ukcolourmap = (gray(256)); ukcolourmap (256,:) = [1 0 0]; myMap = colormap(ukcolourmap); image(im)

13

4PAM1008 MATLAB Coursework 2012


Question 2
If you look at IMAGE002 you will see that there doesnt appear to be much detail. However, there may be image structure there which is not visible to the eye. A common method of seeing if there is information in dark parts of the image is to amplify the lower intensity pixels using a colormap which follows a logarithmic function as shown in figure 7
Log colormap function 1
Image display Intensity Value (0 - 1)

0.8

0.6

0.4

0.2

50 100 150 200 250 Image Pixel Intensity Value (0 - 255)

300

Figure 7: Log colormap function

Use the function file LogImageDisplay.m as your template and do the following

1. Create a logarithmic colormap and apply it to the image passed to the function in the variable im. You will need to generate a 256 x 1 array of values from 0 to 1 which follow a logarithmic curve as shown in figure 7. That array will act as the lookup table for each colour in the 256 x 3 colormap. 2. Display the image with the new colormap in a new figure window (use the figure command). 3. Return the colormap in the output variable myMap. When you create the colormap, assign it to the variable myMap then it will be returned automatically by the function. 4. Return your student number in the output variable studentNumber.

Once you have completed your function, try it on IMAGE002. You should get a result like that shown in figure 9.

14

4PAM1008 MATLAB Coursework 2012

Figure 8: IMAGE002, standard colormap

Figure 9: IMAGE002, Logarithmic colormap

Copy and paste the text from LogImageDisplay.m in the box below (Remember, you must also submit the function file along with this document).

(10 marks)

15

Вам также может понравиться