Professor X has taken two greyscale (PGM) images of a colony of E. coli. These images can be found here: EColiProtR.pgm, EColiProtG.pgm. (Depending on what web browser you are using, these files may give you some trouble. If so, try downloading these, which have the same contents but end with ".txt" so the web browser should treat them as simple text files: EColiProtR.txt, EColiProtG.txt.)
If you view these greyscale images with the xv program, you will see some cells. The two images are of the same colony and were taken at virtually the same time. However, in the first image, the brightness of the cell is proportional to the amount of protein R the cell is expressing. In the second image, the brightness of the cell is proportional to the amount of protein G the cell is expressing. (R and G are not the names of a real proteins; the real names don't matter for this exercise.) Thus, a cell that is rich in protein R but has little protein G will be bright in the first image and dark in the second image. (Technically, these images are obtained by exciting the proteins with lasers, and reading back the intensities at wavelengths that are specific to the two different proteins.)
To help visualize the relationship between these two proteins, Professor X wants the two greyscale images to be combined into one color image (a PPM file). The greyscale values from the protein R image should be filled into the "Red" slots for each pixel in the PPM image, and the greyscale values from the protein G image should be filled into the "Green" slots for each pixel in the PPM image. In this way, if a cell in the color image is red, we know it is expressing only protein R. If a cell is green, it is expressing only G. If it is yellow, it is expressing both R and G. (Because, Red + Green = Yellow, as far as computer screens are concerned.)
For example, if the two greyscale files like like:
P2 3 3 255 10 40 20 40 50 70 10 60 30and
P2 3 3 255 60 20 10 30 60 70 60 70 80Then the PPM image should look something like the following (though exact formatting is not important):
P3 3 3 255 10 60 0 40 20 0 20 10 0 40 30 0 50 60 0 70 70 0 10 60 0 60 70 0 30 80 0(Note that these examples are tiny 3 pixel-wide by 3 pixel-high images, that aren't pictures of anything real. The real grayscale images are much bigger, and should look like cells when view with xv.)
Write a perl program that reads in the two PGM images above, and writes out a PPM file that combines them as Professor X desires. Then, view the resulting file using xv, and briefly describe what you see! (It should look like colored cells.)
2. Exploring compression (10 points)
Download any image from the web, and load it in xv. From xv, you can read off the width and height of the image in pixels.
(A) Calculate the expected file size (ignoring the first few lines) if you saved your image as a PPM-ascii file. Recall, the PPM-ascii files created by xv use 4 ascii characters (bytes) per color, and thus 12 ascii characters per pixel (for red, green, and blue components). Report the width and height of your image, in pixels, and the result of your calculation, so that the TA can check your answer.
(B) Now, use xv to save your image in various formats: PPM-ascii, PPM-raw (which uses 3 bytes per pixel, instead of 12), GIF, and JPEG. Recall that the JPEG format uses lossy compression. When you save as JPEG, there is a "quality" control dial, that lets you control how aggressively xv tries to compress your file. Save several different JPEG versions at different quality levels. Create a table that reports the size (in bytes) of each of the resulting files. Also compute the "compression ratio" or "compression factor", for each image by dividing the size of the PPM-ascii file by the sizes of each of the other files. For example, if the PPM-ascii file was 400,000 bytes, and the GIF file was 10,000 bytes, then the compression ratio for the GIF is 400,000/10,000 = 40. Your table might look something like this:
Image | Size (bytes) | Compression ratio |
Original image in PPM-ascii | 1010479 | |
In PPM-raw | 436244 | 2.32 |
In GIF | 46155 | 21.9 |
In JPEG quality 10% | 4565 | 221 |
In JPEG quality 30% | 7068 | 221 |
In JPEG quality 50% | 8844 | 114 |
In JPEG quality 75% | 12562 | 80.4 |
In JPEG quality 90% | 20315 | 50.2 |
In JPEG quality 95% | 28883 | 35.0 |
In JPEG quality 100% | 62537 | 16.2 |
(These are the results for the PPM image of the cells from Question 1. Your results will differ, first of all, because your image will be different, and secondly, because how much the GIF or JPEG formats manage to compress an image is different for different images.)
How does the size of the PPM-ascii image compare with your calculation from part (A)?
What happens visually to the JPEG images as you compress them to smaller and smaller sizes?
3. Feedback
How long did it take you? Fun / interesting / hard / boring?