Image Processing (Lab Report 7)
In this assignment, we use the MyroC library's representation of images and practice passing 2D arrays to functions that alter their parameters. Note that you will be working with a structure that contains a two-dimensional array of structures. You may want to review the readings from earlier in the week as you tackle this lab.
Getting Started
This lab will use the MyroC library, which is a library that was written by Grinnell CS faculty (primarily Henry Walker) for use with the scribbler robots. We haven't used this library explicitly yet, so we should make sure we can use it before getting started.
First of all, in a lab at the beginning of the semester, you were asked to make copies of Makefiles in your 161 directory on mathlan. (See problem 12: Linux Basics). Verify that you copied this file correctly in your labs folder on your machine. (You should see a number of items set including CPPFLAGS, CFLAGS, LIBRARY_PATH, and LDFLAGS).
Second, we need to edit our .bashrc file, which is stored in the home directory. To do this, type emacs ~/.bashrc. Near the bottom of the file, you should see a section for customization for several of our required CSC courses. Look for the CSC-161 customization section and add the following lines in that section:
# Make MyroC libraries known to the execution environment
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/walker/MyroC/lib"
export LD_LIBRARY_PATH
In order for the changes you just made to take effect, you'll likely need to close and restart the terminal. So, go ahead and do that now.
Now, when compiling a program that includes the MyroC library, we will use the Makefile. The Makefile automatically links the MyroC library so we don't have to write a complicated compile line at the terminal. For example, for the program white-picture-with-black-dot.c in problem 1 below, to compile this file simply type make white-picture-with-black-dot . When you type this command, you should see in the terminal what compliation command is actually being executed.
Example Dots Program
The program
white-picture-with-black-dot.c creates a white
picture, places a [2 pixel by 2 pixel] black dot in the middle, and
displays the result using rDisplayPicture from the MyroC
library.
-
Copy
white-picture-with-black-dot.cto your account, compile and run it, and review how the program works.-
The program uses the MyroC library, which includes definitions
of
PixelandPicture. What happens if you definePixelandPicture(as given in the reading) in this program as well? -
Variables
whitePixandblackPixare declared and initialized at the start of the program. Review how astructis defined and initialized, and explain what the expression= {0, 0, 0}does. What values are assigned to which fields within thewhitePixstruct? - Review how the program places a black dot in the middle of the picture. Enhance the program to place a red dot near the top middle of the picture, a green dot near the left middle of the picture, and a blue dot near the middle bottom of the picture. You may use an external site such as rgbcolorpicker.com to determine the RGB values.
-
Review the definition of
rDisplayPicturein the MyroC documentation file.- Where is the text, "display of myArt", displayed?
- What happens if the parameter 10 is changed to 20? to -3? to 0?
-
The program uses the MyroC library, which includes definitions
of
Add an Academic Honesty Statement to the top of this file. The original author was Henry Walker. Add your names as authors of the revised version. Submit your revised program AND a text file that answers the question in 1a, 1b, and 1d.
Initial Experiments with Pictures
Create a new file calledpic-experiments.c, where you will add code for the remaining questions. Don't forget that you'll need an Academic Honesty statement at the top of this file, as well as documentation for all functions other than main.
-
Write a function
Picture pictureArt(int height, int width)that creates a picture with a design of your choosing. Your picture should include at least 4 different colored pixels, and should have some sort of structure (i.e. not random pixels). For example, you could create an image which colors each quadrant a different color. In your main function, call this function, and make it display for 5 seconds using something like this:Picture art = pictureArt(192, 256); rDisplayPicture(&art, 5, "display of my art"); - Add the function
which takes in avoid colorToGray (Picture * pic)Picture*and then converts each pixel to its grayscale value, where the RGB channels at each pixel are all set to the same brightness value, calculated as(0.30 * red) + (0.59 * green) + (0.11 * blue)
In the forumla above, the values `red', `green', and `blue' should be the original values for a given pixel. Then, the RGB values for that pixel should all be updated to the same value based on that formula.
The main function should then display the Picture.
Note:- A grayscale pixel will have identical values for the red, green, and blue components. Thus, in this problem you will need to compute the composite intensity using the formula above and assign that value to each component of the pixel.
-
The
colorToGrayshould change the picture argument. Thus, the calling context will need to provide the address of the picture (e.g.,&pix), and code withincolorToGraywill need to dereference the pointer to get the original picture (e.g.,(*pic)).
- Add the function:
which finds what the highest RGB value for each pixel is and sets that pixel to just that value—e.g., a pixel of RGB (50,135,85) will have a new RGB of (0,135,0). That is,void setPictureMax (Picture * pic)setPictureMaxshould not change the RGB value of the channel with the highest value, but the other two colors should be set to 0.Your function should change the parameter, so the original picture in the calling procedure (e.g., in
main) is changed. Test out your function in main by re-setting your picture (using problem 2) and then callingsetPictureMaxand then displaying the new picture usingrDisplayPicture.Note that, for some pictures, the change may not be very obvious from looking at the picture. You might want to add a print statement or two that confirms that the pixel values were changed, for testing purposes only. Or, you could create a test image that will show a marked change between original and modified images, which might contain strips of colors that are comprised of combinations of red, green, and blue (e.g. purple, brown, pink, or orange).
If you have two channels tied for the max value, you may choose either (or any) of them to be the max. That is, you don't need to have a special case to check if there is a tie.
-
Write a function
void flipPicture(Picture * pic)that takes a picture and flips the picture upside down.Flipping a picture upside down requires swapping pixels in one row (e.g., near the top) with pixels in a corresponding row (e.g., near the bottom). In the swap, the column index of the swapped pixels will be the same, but the corresponding pixels in two rows will be interchanged.
Note: If
aandbare two values of typeT, then interchanging the values in these variables cannot be done easily with simple assignments:a = b; /* BROKEN */ b = a; /* SWAP */In this code, the original value of
ais overwritten in the first assignment, so there is no way to givebthe original value ofa. Instead, in normal processing, interchanging the values of two variables requires an additional storage location:T temp = a; /* declare temporary variable storing data of the relevant type */ a = b; b = temp;
Grading
Make sure you follow the guidelines on the Submitting Work page as well as the course style guide. In particular, you must include an Academic Honesty Statement in each program. Your Academic Honesty Statement must include the names of all people and sources you consulted while completing this assignment.
I will not require you to submit a test file or testing transcript for this lab, but note that using those tools will dramatically increase the chance that your programs work as expected for a variety of inputs.
Do not include a Makefile or compiled files. Submit your project files in Gradescope.
Your program must compile without ANY WARNINGS on MathLAN and run without crashing. If your program does not compile or crashes, the associated problem(s) will not be graded.
This lab will be worth 20 points based on the following criteria:
- [4 points] Example Dots Program
- Describe what happens with structure redefinitions in the program
- Describe what values are saved to the struct fields for
whitePix - Revise
white-picture-with-black-dot.cto include the dots described in question 1d - Describe how the parameters of rDisplayPicture control the image when it is displayed to the user
- [4 points] Function pictureArt
- [4 points] Function colorToGray
- [4 points] Function setPictureMax
- [4 points] Function flipPicture