This note describes a project assignment for the courses of GEO4060. Although the project is inspired by needs in mathematical computations, and the text contains some (simple) mathematics, there is no need for a thorough understanding of concepts like matrices and vectors to do the project. The goal of this project is to manipulate and process PGM (Portable Gray Map) pictures.
pgmEX1
Glacier over Antartica

GEO4060 Project Assignment 2015

A simple library for image processing






What is a PGM image?


You can view  PGM pictures using the display program from the Image Magick suite (http://www.imagemagick.org/). Image Magick is available on a wide range of platforms including  Unix/linux, Mac OS X, and Windows.

On a linux platform, you can
visualize a PGM picture with the display program:

display output.pgm

Here is what you should see if you display moon.pgm:
pgmEX1
Moon


A PGM image consists of a sequence of one or more PGM images. There are
no data, delimiters, or padding before, after, or between images.
Each PGM image consists of the following:
  1. A "magic number" for identifying the file type. A pgm image's magic number is the two characters "P5".
  2.  Whitespace (blanks, TABs, CRs, LFs).
  3. A Width, formatted as ASCII characters in decimal.
  4. Whitespace.
  5. A Height, again in ASCII decimal.
  6. Whitespace.
  7. The maximum gray value (Maxval), again in ASCII decimal. Must be less than 65536, and more than zero.
  8. A single whitespace character (usually a newline).
  9. A raster of Height rows, in order from top to bottom. Each row consists of Width gray values, in order from left to right. Each gray value is a number from 0 through Maxval, with 0 being black and Maxval being white. Each gray value is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.
A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous. Each gray value is a number proportional to the intensity of the pixel. Strings starting with "#" may be comments.

Convert to a binary image


It is sometimes convenient to convert a grayscale image into a binary image, based on a threshold \(t\).

To transform a PGM image into a binary image, you would need to loop over the entire image and replace each gray values by \(0\) if \( pic(i,j) <= t \) and \(1\) if \( pic(i,j) > t \).
Here \(pic(i,j)\) is the gray value and \(i=1,...,Width\) and \(j=1,...,Height\).

Can the resulting image can be stored in PGM format?

Edge detection


The goal is to implement a simple graphics-processing method for detecting the edges of features contained in a picture. For simplicity, we define the edges of a picture by comparing the values of each pixel to its four nearest neighbours:

\( edge(i,j) = pic(i-1,j) + pic(i+1,j) + pic(i,j-1) + pic(i,j+1) - 4 pic(i,j) \)