Lab - Image Filter

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

EE 285 Fall 2020

100 pts.

Image Filter

Background​:

Images on computer screens are composed of many tiny dots, called pixels, which can take on
a range of colors. The simplest way to store an image is through bitmapping, where each pixel
is represented by some number of bits, and these collections of bits are basically stored in a 2D
array that maps directly to the image. For example, the below 2x3 array of color values would
represent something like the image to the right (but the pixels would be much, much smaller
when displayed on a modern screen).

This idea of an image being an array of pixels is what we mean when, for example, we say an
image is 256x256 pixels. Similarly, when we call a display "4K," we're saying it is approximately
4000 pixels wide.

The next question is, how do we represent the colors? What value do we store to indicate "red"
or "orange?" The most popular method of color representation is RGB. This represents a color
as a combination of a red component (R), a green component (G), and a blue component (B). In
a typical system, we use one byte for each component. Therefore, the values for the
components come from the range [0:255]. The higher the value, the more that color shows up in
the final pixel color. Think of mixing paint: the proportion of each color of paint that you put in the
mixture determines the final color. Interestingly (and perhaps not coincidentally), this digital
representation of separating the RGB components is very similar to how these colors are
actually, physically, created in many types of displays.

Here are some examples: if we write a color in the form R.G.B., a plain blue would be 0.0.255
(zero red, zero green, and 255 blue). Black is 0.0.0, white is 255.255.255, and 127.127.127 is a
shade of gray halfway between white and black. Play around with a ​colorpicker​ to better
understand how the R, G, and B components combine to create the final color.
Assignment:

1. For this lab, you'll modify an image represented as a set of two-dimensional arrays.
Open repl project Lab: Image Filter. This is a "bash" repl instead of a C repl, but don't
worry, you'll still be writing C code. If you open main.sh, you'll see a bash shell script with
three commands: one to delete the file cy.bmp, one to compile the C code, and one to
run the C program.

2. Open main.c. This is the only file you'll need to modify. Read through the main() function.
It opens an input file (cy.bmp) and an output file (cy2.bmp). The file cy.bmp is an image
stored in an uncompressed, bitmapped format.

The main() function then calls load_image(), which is implemented in img.c. This function
extracts the pixel information of the image and stores it in three arrays: ​r_in​, ​g_in​, and
b_in ​(you can ignore ​a_in​ for the purposes of this lab). Each of these arrays is
two-dimensional. The first dimension is the row in the image, and the second dimension
is the column in the image. Array ​r_in​ stores the red information for each pixel in the
image, and green and blue info are stored in ​g_in​ and ​b_in​. The two dimensions of the
arrays represent the row and column of the pixel, respectively. For example, the color of
the pixel in the fourth column of the second row of the image is represented by a
combination of ​r_in[1][3]​, ​g_in[1][3]​, and ​b_in[1][3]​.

The main() function also defines a second set of arrays: ​r_out​, ​g_out​, and ​b_out​.
These arrays are what will be written to file to create the modified image. Therefore,
these are the arrays that you need to populate with your image filter code.

The next section of main() is labeled as "filter code." You will write code here later.

Finally, main() calls write_image(). This function copies the input file to the output file and
then overwrites the pixel array in the output file with the image defined by ​r_out​, ​g_out​,
and ​b_out​.

3. Run the project. It should create a new file called cy2.bmp, which will initially be a black
square (most likely). You may find that repl is not entirely reliable at creating the new file.
If you have a problem, delete cy2.bmp and/or reload the page.

4. Now you are ready to write an image filter! The basic idea is that, in the area marked for
"filter code," you will modify the pixel information in ​r_out​, ​g_out​, and ​b_out​, and your
changes will be written as a viewable image in cy2.bmp. Leave ​r_in​, ​g_in​, and b ​ _in
unmodified, so that you can reference the pixel values of the original image.

For this lab, you will be graded on the following filters. You only need to demo the final
filter (but can demo the others for points if you aren't able to finish everything).

a. (20 pts.) Write filter code to simply create a copy of the original image. This
should result in cy2.bmp looking just like cy.bmp.

b. (40 pts.) Write filter code to create a grayscale (black-and-white) copy of cy.bmp.
Pixels with identical R, G, and B values will be a shade of gray. You can thus
convert all pixels to grayscale by doing the following for each pixel:
i. Find the average of the input RGB values for that pixel
ii. Set each of the output RGB values for that pixel to that average

c. (40 pts.) Extend your grayscale filter to convert all pixels to grayscale EXCEPT
for red pixels, which should remain red. For an example of this type of image, see
Banksy's Girl with Balloon​.

5. Demo this project to a TA, then download the project as a zip and upload it to
Canvas. That's all for the lab, but what other filters can you write? Have some fun!

You might also like