Mini-Project: Image Compression: Tsrt04 - I M
Mini-Project: Image Compression: Tsrt04 - I M
How can you compress an image into a set of bytes? This and other questions are answered in this project. You will work with some advanced algorithms during this project.
Assignment
To compress data is to store as little data as possible without losing any of the original information. One application is to store images as effective as possible. Your task is to implement a function for RunLengthCoding in MATLAB. Compression algorithms similar to the one presented here are for instance used in the .pdf and .gif le formats.
1.1 Specication
Start with a data matrix (e.g., an image). The rst four entries in the code vector (the compressed data) represent the size of the matrix: h 1 h 0 w1 w0 , where the number of rows (the height) of the matrix is h = 28 h1 + h0 and the number of the columns (the width) is w = 28 w1 + w0 . (This allows for the size of large matrices to be stored using bytes.) The rest of the code vector contains the matrix in compressed form one row at the time. The rows are compressed according to the following rules: A sequence of n identical values is represented with the code block [ (257 n) x ], where 257 n 129. A sequence of n different values render the code block [ (n 1) x1 x2 . . . xn ], where n 1 127. The code 128 indicate the end of a row. As new codes are obtained they are just appended to the code vector. Please, observe that a sequence that is to be coded as one block cannot contain more than 128 values in order for the code to be unambiguous. Divide longer sequences into shorter ones, e.g., interpret 200 ones as 128 + 72 ones.
Example:
is
1.2 Examination
Produce a MATLAB function that takes a matrix and compresses it. Let the matrix be the argument to the function and return the compressed code vector. Assume that all values in the matrix are integers between 0 and 255. Create a matrix with integers between 0 and 255 and display it as a gray scale image. Use your function to compress the image. An alternative is to use the supplied test data described in Section 2.8. Compute the compression ratio for an image, i.e., compare the size of the code vector to the size of the matrix. You shall, without problem, be able to run the four lines of code in the end of this compendium. If you have the time, investigate how the compression ratio varies with the color depth. The teaching assistant can help you generate test data. An alternative is to generate a random image with two colors, e.g., let 10% of the image be black and the rest white.
First read though the problem specication and the suggested solution outline and try to understand how it divides the problem into smaller subproblems, each of which is easier to solve than the original problem. The steps below provide one way to split the main programming problem into logical subproblems. The subproblems are deliberately chosen so that it is possible to test the code before continuing with the next step. Do take these opportunities! Following the suggestions closely can result in many small functions solving small parts of the problem, however, it may not be the best, most efcient, nor smartest way to call these small functions to solve the whole problem. Some of the smaller functions (all?) are better suited to be pasted into the code where needed. Note that this solution outline is intended for students with no or little previous programming experience. A skilled programmer would probably, based on 2
previous experience, divide the problem into different and/or fewer parts. If you consider yourself a skilled programmer and think that you have a better solution to the problem you are allowed to solve it your way. However, if you need help, the teaching assistant has more experience of the outlined solution.
another number (disregard it for now), or the end of the matrix, and that the sequence is shorter than 129 numbers so that splitting the sequence is not needed. Write a function to solve the problem. First, preallocate as much memory as you will need according to Section 2.1. Use a variable, c, to keep track of which column in the matrix should be studied next. If you compare the value in column c to the value in column c 1 this allows you to use an easier test for the end of the row, than with a look ahead approach. Two examples to use for testing: [ 1 1 1 1 1 5 ] should yield [ 252 1 ]. [ 1 1 1 1 1 ] should yield [ 252 1 ].
[ 1 1 1 1 1 ] should yield [ 0 1 0 5 252 1 128 ]. The vector repmat( 135, 1, 530 ) should give a result starting with 0, 1, 2, 18 and ending with 128.
in MATLAB to add them to the MATLAB path. The function snowboard provide the images. (Read the online help for information on how to use it.) Display the image in gray scale and investigate the effect changing the color depth has.
>> colormap(gray);imagesc(snowboard(256))
Use the function tsrt04decompress to decompress the code vectors your function produce. (See the online help for usage.) Choose an image, and compress and decompress it. The decompressed image should be identical to the original image. The code below can be used to test this (assuming you function is called compress):
>> >> >> >> snowboardImage = snowboard(12); comp = compress(snowboardImage); decomp = tsrt04decompress(comp); all(all(decomp == snowboardImage))
ans = 1 >>