Lecture 29                                                                                                                 March 17, 2008
COMP 364

- HW3 late today
- Sample HW3 solutions will be posted later today
- Quiz 2 is on Wednesday (arrays, hashes, pattern-matching) and possibly choose 3 of 4
- Thursday HW4 on images will be released
- Still seeking note-takers
- Today: 	- grey scale, colour images (PGM, PPM)
		- problem, compression (we might not get that far today)

Greyscale images
- Each pixel is black or white or some shade of grey in between.
- xv program allows you to save images in different formats.  We can open ("Load") an image and save it in PGM (the greyscale format) ascii format (must also select "greyscale" in the colour menu).
- PGM (ascii) format:
	P2
	Optional comment line
	<width> <height> in pixels
	<max intensity> which is the value of white, while 0 is black
	A long list of numbers representing the pixels with pixel-by-pixel intensities
	
You need to have some sort of white space between the numbers.
For max intensity, you can choose anything greater than 0 and less than 65536

Colour images
- Each pixel has three intensities: red, green and blue.
- In an old-school TV, the screen is made up of individual red, green or blue pixels and it just turns on certain pixels to show whichever colour it wants to.
- In an LCD screen, all three colours are in each pixel.
- Red + green + blue = white
- All off = black
- Red + green = yellow
- Green + blue = cyan
- Red + blue = magenta
- 1/2 red + 1/2 green + 1/2 blue = grey
- Red + 1/2 green + 1/2 blue = pink (halfway between full red and full white)

- In xv, you can save a colour image using PPM format and full colour.
- PPM format (portable pixel map):
	P3
	<width> <height>
	<max intensity>
	<pixel1red> <pixel1green> <pixel1blue> <pixel2red> <pixel2green> ...

Problem:
Given a greyscale image in a PGM file, compute the total intensity of the image (the sum of the greyscale values of every pixel).
This can be used in biology to show how much a colony has grown in a petri dish compared to others (a lighter E. coli colony on a dark background, etc.).
If we had the pixel intensities in an array @P, we could do a foreach loop and add each pixel to a running count:

	$Total = 0;
	foreach $Pix (@P) {
		$Total += $Pix;
	}

To put the pixel intensities in such an array, we could do the following:

	open INFILE, "Image.pgm";
	@Lines = <INFILE>;
	#Need to get rid of the first four lines in the file
	shift @Lines;
	shift @Lines;
	shift @Lines;
	shift @Lines;
	$Str = join(" ", @Lines);
	#Strings are separated by some sort of white space and newlines
	#Split it apart again to get numbers out
	@P = split(/\s+/ , $Str);
	
Now we do the above foreach loop to complete the problem.

An alternative to @P = split /\s+/, $Str); :
	@P = $Str =~ /\d+/g;
	This searches for a pattern of digits globally.

We tested this problem on the computer and it worked.

Problem:
Count the number of pixels that are above some intensity threshold (say 100).  It begins the same as before up to @P.

	$Count = 0;
	foreach $Pix (@P) {
		if ($Pix > 100) {
			$Count++;
		}
	}
	print "# above threshold: $Count\n";