SUNY Geneseo Department of Computer Science
CSci 120, Spring 2014
Prof. Doug Baldwin
Complete by Monday, February 24
or Wednesday, February 26
The basic knowledge of Matlab and digital images that you will need for this lesson comes from the following sources:
In addition, you will need to know a little bit about how to represent images in Matlab. The simplest way to represent an image is as a matrix of pixel intensities. Each row of the matrix represents one row in the image, with row 1 being the top. Similarly each column represents one column of the image, with column 1 on the left.
You can read an image from a file using the imread
function. The syntax is
imread( filename )
where filename
is a string containing the name of the file. This function returns a matrix containing the image. For example
A = imread( 'myphoto.jpg' );
The file you want to read should be in Matlab’s current folder. Matlab can read most common image file formats (e.g., JPEG, PNG, etc.), and can usually figure out which format a file is in from its contents. If Matlab can’t figure out the format, you can specifiy it explicitly via an optional second parameter to imread
; use help imread
for details.
To display an image contained in an array, use the imshow
function. This function takes an array as its parameter, interprets that array as an image, and displays the image in a figure window. For example
imshow( A );
Solve the following problems. You can find images to work with in the “Images” folder on the “Course Materials” tab of our myCourses space. This folder contains four images, two grayscale for the main part of the exercise, and two in color for the bonus part. All of these images come from WikiMedia Commons, a Web site devoted to distributing digital media without restrictive licenses. The mandrill image is by Paolo Camera, and is distributed under a Creative Commons Attribution 2.0 Generic license. The color parrot image is by Marshall Flickman and is also distributed under a Creative Commons Attribution 2.0 Generic license.
You can also use images of your own for this exercise, as long as you don’t mind showing them to me when we talk about this lesson, and as long as they aren’t likely to offend others working in the lab with you.
Download at least the two grayscale images (“Lhotse_Mountain.jpg” and “Parrot.png”). Read both into matrices in Matlab, and have Matlab display the resulting images. Also use Matlab to find out the number of rows and columns in both images.
A simple way to make an image darker is to multiply every pixel intensity in it by a number between 0 and 1. Use this idea to create a version of one of the images that is 25% darker than the original.
Use Matlab to find the maximum intensity value in any pixel of one of the images. Similarly use Matlab to find the minimum pixel intensity in that same image.
If A and B are two images of the same size, you can create a new image, C, that blends the two together, much as if you were seeing B through a semi-transparent A (or vice versa), via the formula
C = α A + (1-α) B
where α is a constant such that 0 ≤ α ≤ 1. Properly speaking this formula works on pixels of A, B, and C, i.e., it tells you that each pixel intensity in C is calculated by multiplying the corresponding pixel intensity in A by α, the corresponding intensity in B by 1-α, and adding the products. This is called “alpha blending,” and is the main way transparency or translucency effects are created in computer graphics.
Create two new images that are the same sizes as each other, and that contain the central feature(s) from the parrot and mountain, respectively. Then use alpha blending as described above to create a blend of these images. (I envision the result being sort of a ghost parrot superimposed on a mountain peak, or maybe a ghost mountain on a parrot. Experiment with α values until you get a blend that you like and in which you can detect both of the original images. The experimentation may be easier to do if you write a function to calculate the blended image.)
You can represent color images in Matlab as 3-dimensional matrices: an image with n rows and m columns is represented by an n-by-m-by-3 matrix M, where M(i,j,1) is the red intensity for the pixel in row i and column j, M(i,j,2) is the green intensity, and M(i,j,3) the blue intensity. The imread
and imshow
functions handle color images exactly as they do grayscale ones.
Re-solve each of the above problems using color images instead of grayscale.
A standard way of calculating a grayscale pixel intensity that is equivalent to (in the sense that most people will see it as equally bright as) a color pixel with red, green, and blue intensities r, g, and b, respectively, is to set the grayscale intensity to
0.2126 r + 0.7152 g + 0.0722 b
Use this idea to write a Matlab function that takes a color image as its parameter, and that returns a grayscale version of that image.
In your study group’s first face-to-face meeting with me following the “Complete By” date above, I will look over your solutions to the problems, ask you any further questions I have, and answer any questions from you. I may also ask you to demonstrate some of your answers in Matlab. Please bring short written solutions to each problem to the meeting (diary
output from Matlab showing you solving the problems would be one way to do this). This will speed the meeting along, but because we will also be talking face-to-face, the written answers don’t have to be long or elaborate.
If you aren’t already signed up for a study group meeting during the week following this recitation, please sign up. Make the meeting 15 minutes long, and try to make it at a time all members of your study group can attend. If no such time exists, make it at a time the greatest number of members can attend.