The objective in this assignment is to write a MATLAB program (“face.m”) which creates a PGM file containing the following image of a face:
The basic characteristics of the image are:
- Image width: 320 px
- Image height: 240 px
- Background colour: light grey (pixel value 200)
- Face stroke colour: black (pixel value 0)
- Face fill colour: white (pixel value 255)
- Outer radius of face: 100 px
- Stroke width: 20 px
By “stroke”, I just mean a line or curve that forms part of the face.
The deadline for this assignment is the end of this week’s class (Thursday 23rd April 2015). You will be able to work on it during the class, but remember that we will also be doing the functions quiz, which will take up about 30 minutes of the 2-hour class. You should therefore be arriving to class with at least a rough solution to the Face Painter problem. I can try to help you iron out any remaining issues at that point, but please bear in mind that I will be assessing your understanding during the class, so it’s in your own interest to show me that you can work independently and that you’re not relying on me too much.
Submission
You will submit your final M-file “face.m” to me by email not later than Thursday 23rd April 2015, together with any additional M-files that it relies on, such as “drawrectangle.m” or whatever else you may have used in your solution.
A related example: Stripes
To solve this problem, you will probably use some of the approaches we discussed in class before the Easter break. To remind you what we were doing before Easter and to get you started on the face problem, here’s a related (but simpler) example, which draws the following image of three stripes:
To create that image, I wrote an M-file called “stripes.m” which contains the code shown below:
%
% stripes.m - Draws a 320x240 px image with light
% grey background and three vertical black stripes
%
% Written by Ted Burke - last modified 18-4-2015
%
% Create a blank light grey image (all pixels = 200)
img = 200 * ones(240,320);
% Draw three vertical black stripes
img = drawrectangle(img, 0, 50, 50, 20, 140);
img = drawrectangle(img, 0, 150, 50, 20, 140);
img = drawrectangle(img, 0, 250, 50, 20, 140);
% Write the image to a PGM file
pgmwrite(img);
You will notice that the program uses a function called “drawrectangle” which I also wrote. This function adds a rectangle to an image. The position, size and colour of the rectangle are all passed as arguments (input values) to the function. The function returns the modified image (which is stored in a MATLAB matrix with 240 rows and 320 columns). The drawrectangle function is saved in a second M-file, called “drawrectangle.m”, which contains the code shown below:
%
% drawrectangle.m - Draws a rectangle of the specified colour at the
% specified coordinates in the array "image".
%
% Arguments are:
%
% image : the array (matrix) that stores the image
% grey : the shade of grey of the rectangle
% left : the x coordinate of the left side of the rectangle
% top : the y coordinate of the top of the rectangle
% width : the width of the rectangle in pixels
% height : the height of the rectangle in pixels
%
% Written by Ted Burke - last modified 18-4-2015
%
function image = drawrectangle(image, grey, left, top, width, height)
% outer loop iterated over the rows in the image
y = top;
while y < top + height
% inner loop iterates over the pixels in the current row
x = left;
while x < left + width
% this sets the colour of the pixel at the current x,y point
image(y,x) = grey;
x = x + 1;
end
y = y + 1;
end
end
Finally, the program also uses the same “pgmwrite” function that we used in class before Easter. In case you don’t still have that M-file, here’s the complete code, which should be saved as “pgmwrite.m”:
%
% pgmwrite.m - MATLAB function to write a matrix
% of pixel values to a greyscale PGM image file.
%
% Written by Ted Burke - last updated 12-3-2015
%
function pgmwrite(pixels)
% Check matrix dimensions which will determine
% the width and height of the image
s = size(pixels);
height = s(1);
width = s(2);
% Open a file for writing
fid = fopen('image.pgm','w');
% Write the PGM image header to the file
fprintf(fid,'P2\n');
fprintf(fid,'%d %d\n', width, height);
fprintf(fid,'255\n');
% Write the pixel values from the matrix into
% the file
y = 1;
while y <= height
x = 1;
while x <= width
fprintf(fid, '%03d ', pixels(y,x));
x = x + 1;
end
fprintf(fid, '\n');
y = y + 1;
end
% Close the file
fclose(fid);
end
To run the “stripes” example, you should place the three files “stripes.m”, “drawrectangle.m” and “pgmwrite.m” into the same folder. Then, in MATLAB, navigate to the same folder and type “stripes” to run the program.
Hints for the Face Painter problem
There are lots of ways you could solve the face drawing problem, but the approach I took was to create another new function called “drawcircle” which was saved in an M-file called “drawcircle.m”. If you decide to take this approach, you’ll need to write the function yourself, but to give you a bit of a hint, this was the comment from the start of my “drawcircle.m”:
%
% drawcircle.m - Draws a circle of the specified colour at the
% specified coordinates in the array "image".
%
% Arguments are:
%
% image : the array (matrix) that stores the image
% grey : the shade of grey of the rectangle
% cx : the x coordinate of the circle centre
% cy : the y coordinate of the circle centre
% r : the circle radius
%
% Written by Ted Burke - last modified 18-4-2015
%
I then wrote the file “face.m” which created a blank grey image and added a number of circles and rectangles using the “drawcircle” and “drawrectangle” functions.
If your require clarification on any aspect of the assignment, you can either leave a comment on this post or email me and I’ll do my best to address your query. By all means contact me if you’re stuck on some aspect of the problem too, but keep in mind that I’ll be hoping to see evidence that you’re getting making a serious effort to get stuck into the problem yourself.