Mathematica Lab 3

MATH 220E Spring 2025

Work through the worksheet below.  Complete the exercises in a separate notebook and follow submission guidelines at the end.

Exploring linear combinations with images

First we will import some pictures. You could theoretically use your own pictures, but the images I’m giving you have been scaled and centered so that they’re all roughly the same, which will make things easier. You should also download the three images from the course website before proceeding. Note where the images are saved on your computer!

The first thing you need to do is change the path to the file image1.png that you see below. On a Mac computer, the path looks something like this:

/Users/wright/courses/spring2025/math220/image1.png

On a Windows computer, the path looks something like this:

C:\Users\wright\courses\spring2025\math220\image1.png

Now use the Import function to load the image into Mathematica. A sample Import function appears below, but you will need to use file path for where the file is located on your computer.

Note: Backslashes in text strings have special functions. Windows uses backslashes to separate folders in a file path, so if you’re using a Windows computer, you have to “escape” each backslash by typing an extra backslash, as shown below.

pic1=Import["C:\\Users\\wright\\courses\\spring2025\\math220\\image1.png","Data"]

Now you can find the dimensions of the image like this:

Dimensions[pic1]

This is a grayscale image with 240 rows and 160 columns of pixels. Each entry ranges from 0 (interpreted as black) to 255 (white). For example, you can find out the entry in row 75 and column 82 like this:

pic1[[75,82]]

We’ll convert the entries to real numbers between 0 and 1 (by dividing by 255), and then after that show the image. Mathematica can display matrices as images, but it requires that the entries be between 0 and 1.

pic1rescaled=pic1/255.0

Image[pic1rescaled]

If you don’t see a grayscale image of a random person above, something has gone wrong.

Note that pic1 refers to the imported data and that pic1rescaled is the rescaled version of the data.

A scalar multiple of the 240 × 160 matrix will scale each of the 38,400 entries. For example, if we multiply the matrix 1/2, the whole picture will get darker because all the entries will be that much closer to 0 (and 0 represents black). Let’s try this for fun.

Image[(1/2)*pic1rescaled]

The code below outputs a comparison of the first 10 entries in row 1 of the original picture array and of the scaled picture.

Take[pic1rescaled[[1]],10]

Take[(1/2)*pic1rescaled[[1]],10]

Problem 1

Find a way to show a lightened version of the image pic1rescaled. Then, explain how you did it (with a sentence or two).

Import another image

Now we’ll import a second picture, image2.png. We will rescale the value to be between 0 and 1, and then we will display the image.

pic2=Import["your filepath here","Data"]

pic2rescaled=pic2/255.0

Image[pic2rescaled]

Let’s look at a linear combination of the two pictures. To make sure the entries of the linear combination don’t exceed 1 by too much, the two scalars will add up to 1 (so 1/2 and 1/2, or 1/3 and 2/3).

Image[(1/2)*pic1rescaled+(1/2)*pic2rescaled]

Next is a function that combines different scaled values of the two images. The higher the value of k, less of the first picture is used and more of the second picture is used.

image[k_]:=Image[(1/k)*pic1rescaled+(1-(1/k))*pic2rescaled]

Try it out for some different values of k:

image[3]

image[8]

image[1.05]

Problem 2

Experiment with the image function for some values of k between 0 and 1.  Explain what is happening (and why).

Problem 3

What happens if k < 0?  Experiment and explain.

An Animation

Below is an animation that starts with the mostly first picture and ends with mostly the second picture.

Animate[image[k],{k,1,8,.25,Appearance->"Labeled"},AnimationRate->.5]

Problem 3

Stop the animation when you think the features of the second person dominate the image. What is the k value when this occurs (you can read the number to the right of the control buttons)?

Random Matrices

The following code generates is a random 240 × 160 matrix with values between 0 and 1. We’ll combine it with a picture and ask at what point the “humanness” loses out to the random pixel values.

randomPic=RandomReal[1,{240,160}]

Image[randomPic]

newImage[k_]:=Image[(1/k)*pic1rescaled+((k-1)/k)*randomPic]

Animate[newImage[k],{k,1,8,.25,Appearance->"Labeled"},AnimationRate->.5]

Problem 4

Stop the animation when you can no longer tell there’s a face in it. What is the value of k? (You’ll be biased since you know the face is there, so show it to a roommate or friend and ask if they can identify a face in the noise).

Problem 5

Now import a third picture, either image3.png or image4.png Do the necessary steps so that you can (a) show a linear combination of the three pictures, each scaled by 1/3; (b) show any other non-trivial linear combination of your choosing; and (c) create an animation that involves all three pictures.

You’ll get some extra credit if you can create an animation that starts with being almost all dominated by picture 1, changes almost completely into picture 2, and ends by being very dominated by picture 3.  This is not as simple as you might think.

What do I hand in?

Once you’ve completed Problems 1-5 above in a new Mathematica notebook, make sure your animations are stopped. Upload your notebook to the Mathematica Lab 3 assignment on Moodle.