SUNY Geneseo Department of Mathematics

Lab 8—Image Processing

Math 230 02
Spring 2016
Prof. Doug Baldwin

Complete by Thursday, April 21
Grade by Tuesday, April 26

Purpose

This lesson reinforces your understanding of vectorization and matrices in Matlab, and introduces you to some basic ideas behind digital images and image processing. When you finish this lab I expect that you will be able to (1) write vectorized code that operates on matrices, and (2) implement the chroma key image compositing algorithm in Matlab.

Background

This lesson is built around implementing a “green screen” (more technically known as “chroma key”) effect, in which a uniformly colored background in one image is replaced with another image to create a picture showing someone or something in a place they never really were. In the following, I will refer to the image whose background is being replaced as the “subject” image, and the replacement background as the “background” image.

A general introduction to digital images is available at http://cs.geneseo.edu/~baldwin/reference/images.html. Sections 11.2 and 11.5 of our textbook describe how to work with some of these ideas in Matlab. We will talk about this material in class on April 13.

This exercise also emphasizes using vectorized computations in place of loops in Matlab. “Vectorization” refers to using Matlab operations that map over arrays in place of loops that act on array elements one at a time. Vectorized calculations are usually much faster than loops in Matlab, and so are preferred to loops. Section 13.6 of our textbook talks briefly about this fact.

You will almost certainly need to do some amount of ancillary image processing outside of Matlab in this lesson. At the very least, you will have to scale your subject and background images to be the same size, and you might find it helpful to have a source of image statistics to gauge the range of colors in your background image. I can do some of this for you, but there are also many free image processing programs that you can download to your own computer. For example, ImageJ, available at http://imagej.nih.gov/ij/, is well respected and very capable.

Activity

Write a Matlab function that takes two images, a subject and a background, and a description of the key color(s) in the subject to replace with background as its parameters. The two image parameters should simply be n-by-m-by-3 arrays. The description of the key color(s) should consist of one or more arrays or other values that specify a range of colors. This function should return a chroma-keyed composite of the subject and background images.

Do not use loops in your compositing function. All the computations you need can and should be vectorized.

Note that this function returns, but does not display, the composite image. The function’s caller can do whatever it likes with that composite—quite likely display it, but maybe do further processing on it, or write it to a file, etc.

Extra Credit

For up to 3 points extra credit, see what other sorts of image processing you can implement as Matlab functions. Vectorize those functions where possible. Examples of things you might consider include…

Many if not all of these will require you to do some reading on your own to understand the problems and get ideas for algorithms for solving them.

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring your solution on your computer, as that will speed the process along.

Sign up for a meeting via Google calendar. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.