Computer Graphics Assignment Needs to be done in C++ Programming

techguy
CAssignment.pdf

CPSC 4040/6040 Computer Graphics Images

Assigned: Sept 21, 2022 Due: 11:59pm, Sept 30, 2022

Programming Assignment 3 — Green Screening (Grading: 0–10 points)

Problem Description

In this assignment you will develop two programs for doing green screening. Green or blue screening is a process where a range of colors in an image (typically highly saturated greens) is used to determine an alpha channel for the image, for purposes of compositing. The most familiar example of blue screening is seen every night on the local news during the weather report. What you see on the screen is the weather person standing in front of an animated weather map. Actually, he or she is standing in front of a blue curtain or screen, and the map is simply on a TV monitor. The image of the weather person is superimposed on the image of the weather map by an analog compositing operation, that (in essence) treats the weather map as a background image, and assigns every pixel of the foreground weather person image an alpha value of 0 wherever the image is blue and 1 wherever the image is not blue. Green screening works the same way, only with a green screen as background.

Basic Requirements

First, you are to write code to generate an alpha channel mask for an image based upon image color information. Second, you will complete an image compositing program that uses the alpha channel information of a foreground image and computes the over operator. All final tests of your program and your homework submission should use the green screened images available on the website. For compositing, you can composite these pictures onto any background image that you choose. You are also welcomed to test your code on your own green screened images.

Masking. Your first program should be called alphamask and it should take as input an image and use its color information to produce a four channel version of the image. The RGB channels of the image should remain unaltered, the only new data this program creates is the fourth α channel. I expect that you will call the program by using:

alphamask input.img output.png

Where input.img is any image file that can be read by OIIO and output.png is the output image stored in a png format that supports four channels.

As we discussed in class, there are many techniques for green screening. For this assignment, you should focus on chroma-keying:

1. You should first allow for setting the alpha channel based on a range of hue values. To do so, convert the color data to HSV and next select the pixels to mask based on those which have a hue near the value 120 (pure green on a 360 degree scale). I have included in the appendix some sample code to convert RGB to HSV values. Any pixel that falls within your hue range should be converted to have a transparent mask (α = 0), while any pixel that falls outside of the range should be opaque (α = 255).

2. Next, mask pixels based on not just hue, but also saturation and value thresholds. I suggest that your program reads an input parameter file containing these values and hue ranges, so that you can simply edit this file and rerun your program while you tune and debug (this

may be slightly less painful than providing all parameters manually or as command line arguments). No doubt, it will require some experimentation to get the range of hues and the thresholds set right to get results that you like. This arrangement will also allow you to easily modify the colors used to determine the alpha channel so that you can do “red screening”, “blue screening” or even “black screening”. Be sure to include in the README exactly how to run your program and your preferred settings for the different test images.

When you get all this working, you might want to experiment with using a full greyscale mask for alpha, not just a binary mask as described above. Your function for masking pixels should be extended to allow for setting α to values other than 0 or 255 by examining color information and interpolating between 0 (inside the original cutoff range) and 255 (outside the range). This will give a gradient region around the edges of the alpha mask that smoothly fades from foreground to back- ground image, avoiding jagged looking edges between foreground and background in the final image.

Important Note. If you want your program to visualize your alphamasked image for debugging pur- poses, you can display pixel values multiplied by their corresponding alpha values. In this case, make sure that you are directly exporting the 4-channel image from your pixmap data structure rather than by reading it from the OpenGL viewport using glReadPixels. The reason is that the viewport will contain premultiplied color information. Alternatively, you can display your al- phamasked image by including the following code in your handleDisplay function that you use to handle display calls: glBlendFunc(GL SRC ALPHA, GL ONE MINUS SRC ALPHA); glEnable(GL BLEND); ...your display code... glDisable(GL BLEND).

Please keep in mind that you don’t have to display the alphamasked image. As soon as you export it to an output file, you can simply open the file and verify the resulting mask.

Compositing. The second program you are required to write should be called compose. It should read in two color images and their alpha channels, and display their composite (A over B). If the alpha channel is missing for the second image, your program should assume that the image is a background image (i.e. an image that is completely opaque). Your program should display the composite of A and B, and allow the user to write to an optional third file containing the newly composited image.

This means you must support:

compose A.png B.img

As well as

compose A.png B.img out.img

Where A.png and B.img are two image files, and in particular A.png is one of the images exported from your alphamask program. In your program, you must write your own code to compute A over B. Do not use glBlend() or any other OpenGL operation here. As images A and B can be of different sizes, extra care should be taken to perform the over operation, with the simplest being to only allow composition between equally sized images. Another solution, is to always use a background B image that is taller and wider than A.

Testing. You should extract alpha channel masks for the three greened screen images available on the website. Please include these files in your submission. For compositing, you can com- posite these pictures onto any background images of your liking and get as creative as possible! Along with your code and README file, please include your pulled alpha masks, selected backgr- ground images, and resulting composite images in your submission. Feel free to experiment with other green screened images that you can find online, and submit your related results.

Note: To perform over operations, you may find it simpler to store each channel of an image in memory as a floating point number between 0 and 1 instead of an unsigned char.

Advanced Extension

Extend your alphamask program to provide interactive masking using two different functions:

1. The first will automatically assign α values using the Petro Vlahos equation instead of chroma- keying. You can enable this functionality by adding a flag, such as alphamask -pv, or via keyboard interaction. Please note that here you are only going to set the transparency values of the pix- els, rather than extracting both transparency information and foreground color values which will require solving a linear system of equations as explained in class. To do so, initialize k to be 1, where G = kB is the equation we discussed in class. Hence, if G>kB, then α = 0, otherwise α = 1. As the constant k plays an important role in the resulting pulled alphamasks, you are going to control it via the keyboard. When the user hits UP on the keyboard, it should increase the value of k. Likewise, pressing DOWN should decrease the value. Only a binary mask is required, but just like the basic requirements you may want to consider how a greyscale alpha mask could be used.

2. The second functionality will enable spill suppression. You should be able to execute the code with or without this feature, for example by adding a flag alphamask -ss, or via keyboard interac- tion. Note that the basic requirement for the assignment only creates alpha values—you should use this flag as a way to enable the modification of color values in the output image. You may choose any scheme you like to adjust colors, either using some of the basic color manipulation techniques we discussed in class that adjust the green channel (e.g. G = min(R,G,B) or G = min(G,B)) or a scheme of your own design.

You should test your code on the images provided in the Advanced Extension image folder. Both of these function can either be used separately, or together. In theory, with both, you should be able to achieve some reasonably high quality green screen effects!

Code Documentation Please provide documentation on how to operate the programs, related parameters for each tests case, etc., in a README text file. You should also comment your code.

Submission

Turn in your C++ source files, Makefiles, any parameter data files, your extracted alphamasks, background images, and resulting composite images using the standard turn-in procedure. Please also include documentation on how to operate the programs and parameters used in each test case in a README text file. Make sure that you are including all files needed to compile your code. Since we are using multiple files, please make a directory named with your Clemson username, and add all files to it. You should zip your directory and upload it to the Canvas submission system.

Help

If you get stuck, please do not hesitate to contact us for help, and stop by during office hours. We also encourage you to post questions and initiate discussions on Canvas. Your colleagues are also there to help you.

Appendix: Code to convert RGB to HSV

Adapted from Foley, Van Dam, Feiner, and Hughes, pg. 592.

/∗ Input RGB color primary values : r , g , and b on scale 0 − 255 Output HSV colors : h on scale 0−360, s and v on scale 0−1

∗/

#define maximum(x, y, z) ((x) > (y)? ((x) > (z)? (x) : (z)) : ((y) > (z)? (y) : (z)))

#define minimum(x, y, z) ((x) < (y)? ((x) < (z)? (x) : (z)) : ((y) < (z)? (y) : (z)))

void RGBtoHSV(int r, int g, int b, double &h, double &s, double &v){

double red, green, blue;

double max, min, delta;

red = r / 255.0; green = g / 255.0; blue = b / 255.0; /∗ r , g , b to 0 − 1 scale ∗/

max = maximum(red, green, blue);

min = minimum(red, green, blue);

v = max; /∗ value i s maximum of r , g , b ∗/

i f (max == 0) { /∗ saturation and hue 0 i f value i s 0 ∗/ s = 0;

h = 0;

} else {

s = (max - min) / max; /∗ saturation i s color purity on scale 0 − 1 ∗/

delta = max - min;

i f (delta == 0) { /∗ hue doesn ’ t matter i f saturation i s 0 ∗/ h = 0;

} else {

i f (red == max) { /∗ otherwise , determine hue on scale 0 − 360 ∗/ h = (green - blue) / delta;

} else i f (green == max) {

h = 2.0 + (blue - red) / delta;

} else {/∗ (blue == max) ∗/ h = 4.0 + (red - green) / delta;

}

h = h * 60.0;

i f(h < 0) {

h = h + 360.0;

}

}

}

}