6/1/2021 Lab4 (10pt): image processing
Lab4 (10pt): image processing
Due Monday by 11:59pm Points 10 Available after May 13 at 1pm
This time you need to download three files: lab4.asm (https://canvas.eee.uci.edu/courses/37196/files/14428611/download?download_frd=1) Minimize File Preview
ZOOM
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 1/10
6/1/2021 Lab4 (10pt): image processing
lab4_test.asm (https://canvas.eee.uci.edu/courses/37196/files/14514082/download?download_frd=1) lena.pgm (https://canvas.eee.uci.edu/courses/37196/files/14428623/download?download_frd=1)
But only upload lab4.asm to Gradescope, NOTHING ELSE.
In this lab assignment, you will implement two image processing kernels in MIPS assembly. The input for both functions is a square sized grayscale image. Refer to the following “Memory Layout of a 2D
Array” section to learn how a 2D array is stored in memory. In this case, images are stored in a 2D array where each element is a byte, 8-bit unsigned integer, that represents each pixel’s intensity 0-255 (0x00 (0) used for black to 0xFF (255) for white). The output image in both parts will have the same size as the input image. We have included a utility function that loads and stores image data from images stored on disk with pgm file format (a simple raw format). Your task is to write the body of the functions
that load an array of pixels along with its dimensions and process them and store the result back in
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 2/10
6/1/2021 Lab4 (10pt): image processing
that load an array of pixels along with its dimensions and process them and store the result back in memory.
Memory Layout of a 2D Array:
An image is essentially a 2 dimensional (2D) array where every element of that array represents a pixel. The value of that element determines the corresponding pixel appearance such as color or brightness (for grayscale images).
In order to process a 2D array in assembly, it’s important to understand how a 2D array is laid out in memory. First, some notes on the nomenclature of this handout. Computer memory will be represented as a linear array with low addresses on the left and high addresses on the right. Also, we’re going to use programmer notation for matrices: rows and columns start with zero, at the top-left corner of the matrix. Row indices go over rows from top to bottom; column indices go over columns from left to right.
The elements sit in the memory in a manner called row-major layout where the first row of the matrix is placed in contiguous memory, then the second, and so on:
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 3/10
6/1/2021 Lab4 (10pt): image processing
Another way to describe row-major layout is that column indices change the fastest. This should be obvious by looking at the linear layout in the above diagram. If you read the element index pairs from left to right, you’ll notice that the column index changes all the time, and the row index only changes once per row.
For programmers, another important observation is that given a row index (row_idx) and a column index (col_idx), the offset of the element they denote in the linear representation is:
offset = row_idx * num_of_columns + col_idx
Where num_of_columns is the number of columns per row in the matrix. It’s easy to see this equation fits the linear layout in the diagram shown above.
Having the memory address of the base of an array and a pair of (row, col) indices, we are able to find the address of individual elements in a 2D array:
element_addr = base_addr + element_size_in_bytes * offset
Part 1) Image Thresholding:
#a0: input buffer address
#a1: output buffer address
#a2: image dimension (Image will be square sized, i.e., number of pixels = a2*a2) #a3: threshold value
Image thresholding is a simple way of partitioning an image into a foreground and background. This image analysis technique is a type of image segmentation that isolates objects by converting grayscale images into binary images. An example of such conversion is shown below:
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 4/10
6/1/2021
Lab4 (10pt): image processing
Your function should replace the value of each pixel in the grayscale image with the maximum possible value (0xFF, i.e. brightest value), if the intensity (value) of that pixel is greater than or equal to some constant, threshold value (function input), or otherwise with the minimum value (0x00, i.e. darkest value). Remember that these values are unsigned.
Part 2) Affine Transformation:
#a0: input buffer address
#a1: output buffer address
#a2: transform matrix address
#a3: image dimension (Image will be square sized, i.e., number of pixels = a3*a3)
For the second part, you are implementing a function to apply an affine transformation to the input image. Such transformation is described using a 2×3 transform matrix (M) that maps pixels at input image to different coordinates in output image. Affine transformations (https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations) have this property that they map parallel lines in input image to parallel lines in output image. Examples of affine transformations include: translation, rotation, reflection and scale.
NOTE: Although image pixel are bytes, matrix elements are integers (words).
The following algorithm demonstrates applying an affine transformation to an input image. In this algorithm x and y represent current output pixel coordinates — x being the column index and y being the row index. Top-left pixel has coordinates of (0, 0).
Th ditfth23tf ti
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 5/10
)( 20𝑀 ,10𝑀 ,00𝑀
𝑀
6/1/2021 Lab4 (10pt): image processing
The coordinates of the 2×3 transform matrix are For each output image pixel at x, y coordinate:
Example:
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 6/10
esiwrehto ,0 { = )𝑥 ,𝑦(𝑡𝑢𝑝𝑡𝑢𝑜 𝑠𝑛𝑚𝑢𝑙𝑜𝐶_𝑙𝑎𝑡𝑜𝑇 < 0𝑥 ≤ 0 dna 𝑠𝑤𝑜𝑅_𝑙𝑎𝑡𝑜𝑇 < 0𝑦 ≤ 0 fi ,)0𝑥 ,0𝑦(𝑡𝑢𝑝𝑛𝑖
)21𝑀 ,11𝑀 ,01𝑀( = 𝑀 20𝑀 ,10𝑀 ,00𝑀
6/1/2021 Lab4 (10pt): image processing
Here are some examples of affine transformations:
(Since we are applying 2D affine transformation, the 3rd rows of a transformation matrix is unused.)
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 7/10
6/1/2021 Lab4 (10pt): image processing
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 8/10
6/1/2021 Lab4 (10pt): image processing
Testing
lab4_test.asm comes with a tester that tests your function with proper parameters such as input arrays and images.
We have provided three test cases for part 1 and eight test cases for part 2 that you can use to verify your code functionality. Make sure the printed output matches the expected data.
To test your code against images, you need to put the tester code --"lab4_test.asm", your source code -- "lab4.asm"--, input images --“lena.pgm”--, and MARS in the same directory. And “lena_thresh.pgm”, "lena_identity.pgm", “lena_rotation.pgm”, “lena_shear.pgm” and “lena_scale.pgm” are output images that will be generated by parts 1 and 2 of this lab. You need to verify those images manually. (how to open pgm? google online pgm viewer.)
Note: It takes a while (~10s) to test your code against images, depends on your hardware performance.
Submission instructions
Please upload the modified "lab4.asm" to Gradescope under Assignment "Lab 4 (https://www.gradescope.com/courses/258505/assignments/1237037) ". The autograder will test against the given (public), hidden test cases as well as the lena output images. Only upload lab4.asm to Gradescope, NOTHING ELSE.
== Appendix ==
WARNING you might encounter (false alarm in Lab4):
"Warning in ... is out-of-range for a signed value and possibly truncated."
Answer: it comes from the assembler's static analysis to warn the potential bugs if the data is treated as a signed value. However, it's not the case in lab4 since the pixel's value is always positive, 0-255. That is, we treat it as an 8-bit unsigned integer.
Hint: lb? lbu?
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 9/10
6/1/2021 Lab4 (10pt): image processing
Update:
These are expected pgm output you can download. (lena_thresh.pgm (https://canvas.eee.uci.edu/courses/37196/files/14157384/download?download_frd=1) lena_rotation.pgm
(https://canvas.eee.uci.edu/courses/37196/files/14157381/download?download_frd=1) lena_scale.pgm (https://canvas.eee.uci.edu/courses/37196/files/14157382/download?download_frd=1) lena_shear.pgm (https://canvas.eee.uci.edu/courses/37196/files/14157383/download?download_frd=1) )
https://canvas.eee.uci.edu/courses/37196/assignments/752027?module_item_id=1292233 10/10