Topics: Multidimensional Arrays & Image Files

Today's lab uses image manipulation to demonstrate multidimensional array commands.

### Image files

There are many different formats to store images. Some store the information about the color of every pixel ("picture element" or dot on the screen). Others store end points of lines or boundaries of regions and the colors of each. We focus on the former, and in particular, use the png (portable network graphics) file format, which is popular for storing images captured from cameras and is `lossless' (every pixel captured is stored, so, can also be quite large). Each pixel is represented by its the percentage of red, green, and blue ("RGB") values. Images are stored as a grid of red values, a grid of green values, and a grid of blue values.

For example, if we stored our image in the variable, img, we could access the red value by:

```	print "Upper left has red:", img[0,0,0]
```
and the amount of green:
```	print "Upper left has green:", img[0,0,1]
```
and the amount of blue:
```	print "Upper left has blue:", img[0,0,2]
```
Any point can be accessed via its coordinates (i,j) and the color channel (0 for red, 1 for green, and 2 for blue).

What does this code do? Work through it first, and then try on your computer. This program assumes that you have a file called bf1.png in the same directory. You can use that file, or substitute one of your own.

```
import matplotlib.pyplot as plt
import numpy as np
plt.show()
height = img.shape[0]
width = img.shape[1]
newImage = np.zeros((height,width,3))
for i in range(height):
print "processing row", i
for j in range(width):
newImage[i,j,0] = img[i,j,0]
newImage[i,j,1] = 0
newImage[i,j,2] = 0
plt.imshow(newImage)		#Open window to show image (close to continue)
plt.show()
plt.imsave('face2.png',newImage)
```

#### Challenges:

• How can you display only the green channel? How can you display the blue channel?
• Write a program that displays the image in black and white. Each color value of the pixels in the new image are an average of the red, green, and blue in the original. That is,
```newR = (r+g+b)/3
newG = (r+g+b)/3
newB = (r+g+b)/3
```
• Write a program that shows only the pixels that are white (or very close-- that is, red, blue, and green values above 90%).
• Write a program that shows only pixels that differ between these two butterfly images, bf1.png and bf2.png.

#### Landsat Satellite Data

Landsat Satellite Program is a joint program of USGS and NASA that has provided continuous images of the earth since 1972. The data is publicly available through the USGS-EROS site and has been invaluable in mapping changes in the earth. Today, we will use data images from the USGS remote sensing gallery:

http://remotesensing.usgs.gov/gallery/

The gallery consists usually of pairs of images demonstrating change over time. Our challenges for today are:

• California drought: how much change has their been in the Sierra Nevada snowpack during the current drought? there are three images of the Sierra Nevada mountains. Count the amount of snow cover (pixels that are nearly white) in the image from February 2011. Then count the snow cover in February 2013 and in February 2014. What is the percentage coverage (using 2011 as a baseline)?
• Korean Coastline: how much has the coastline of Incheon, South Korean grown over the last 30 years? In the last 32 years, the land region around Incheon, South Korea has grown significantly. Using the images in the gallery from 1981 and 2013, highlight in red the additional land mass.
(Hint: subtract the two images to find the regions that are different. Next, add in an if-statement inside your loop to color only those regions red.)
• Choose an image set from the gallery, develop a hypothesis, and design a program to analyse your image.
Note: Some images are stored in JPG format-- use Preview to convert to PNG.

### Lab Report

For each lab, you should submit a lab report by the target date to: kstjohn AT amnh DOT org. The reports should be about a page for the first labs and contain the following:

Target Date: 23 March 2016
Title: Lab 7: Multidimensional Arrays and Image Files
Name & Email:

Purpose: Give summary of what was done in this lab.
Procedure: Describe step-by-step what you did (include programs or program outlines).
Results: If applicable, show all data collected. Including screen shots is fine (can capture via the Grab program).
Discussion: Give a short explanation and interpretation of your results here.

### Using Rosalind

This course will use the on-line Rosalind system for submitting programs electronically. The password for the course has been sent to your email. Before leaving lab today, complete the first two challenges.