Starting from:

$34.99

DSC395T Image ManipulationProgramming Assignment #1 Solution

In this assignment you will write code that manipulates digital images. We will provide routines that read and write images from the disk, and you will write code that alters the image in various ways. This assignment does not require you to write a lot of code, but it introduces you to NumPy arrays, and it illustrates the power of computing, so we encourage you to be creative in showing off the capabilities of your resulting tool.
1 Your Assignment
Digital images are stored on disks in a variety of formats, including JPEG, PNG, and GIF. We have provided functions for reading these images into your computer’s main memory, so that you can view each image as a 2D array of pixels, where the color of each pixel is determined by its Red, Green, and Blue components. In particular, each Red, Green, and Blue value is a number between 0 and 255.
The first part of your assignment is to implement the following effects.
• NoRed: Remove all shades of red from the image.
• NoGreen: Remove all shades of green from the image.
• NoBlue: Remove all shades of blue from the image.
• RedOnly: Preserve any red shades in the image, but remove all green and blue.
• GreenOnly: Preserve any green shades in the image, but remove all red and blue.
• BlueOnly: Preserve any blue shades in the image, but remove all red and green.
• Grayscale: Turn a color image into a grayscale image. This can be done by taking the mean of the red, green, and blue values at each pixel.
• AddNoise: Add random noise to an image to produce a grainy, aged-film effect. We encourage you to learn about different types of noise and see how they behave, but for the purposes of automated grading, your submitted code should add a random number between −5 and 5, inclusive, to every Red, Green, and Blue value. Make sure that the pixel values do not fall outside of the range 0 − 255.
• Smooth: This effect is trickier to implement than the previous ones but can be implemented by detecting pixels that are outliers compared to their neighbors. For example, look through each of the image’s 3×3 window of pixels and replace outlier pixels with the mean or median value of the others in the 3×3 window. For this assignment, set the resulting pixel value to be the mean value of a pixel and its eight neighbors. For pixels on an edge, use the mean value of the pixel and its five neighbors. For pixels on corners, use the mean value of the pixel and its three neighbors. Be careful! If you choose to loop over every pixel, then you must store your input and output arrays separately (i.e. you cannot modify the given array and return it), because otherwise you will corrupt your input data with your output.
• Normalize: Image Normalization (click here for demo) is a technique used to touch up images that are too dark or too light for the human viewing experience. Implement a version that outputs images with full dynamic range (0–255 pixel intensity). In other words, for each color channel (r, g, b), rescale the range of pixel values for that channel to include both 0 and 255. After normalizing, there should be at least one pixel with a red value of 0, at least one pixel with a red value of 255, at least one pixel with a green value of 0, etc. To normalize a list of numbers nums to the range 0–n, set nums[i] = (nums[i]−min(nums))/(max(nums)−min(nums))∗n for all i. Be careful! When max(nums) == min(nums), you should not touch that list (why?).
• Threshold: Reduce the number of colors used in the image from millions to eight. In particular, set each of the R, G, and B values in the output image to either fully on (255) or fully off (0), depending on whether its value in the input image is above some threshold. This threshold value should be a parameter to this function, and a typical value is of the maximum value, or 127.
2 Details
2.1 Representing Images
Your code should represent the images as NumPy arrays (click for docs). Unlike regular arrays, which are onedimensional, NumPy arrays are more like tensors and can have an arbitrary shape such as (3, 3)—a 3×3 matrix— or (1080, 1920, 3)—an image with 1080 rows, 1920 columns, and 3 color channels per pixel. You can rely on the shape of your input arrays to be (h, w, 3) where h and w are the image’s height and width, respectively, and are each generally under 500. The three color channels (along the last axis of the array) are in the order [red, green, blue]. For example, to change the green value of the pixel at row 30, column 10, you could write array[29, 9, 1] = greenvalue; remember, of course, that these indices are zero-based.
To manipulate these image arrays, we expect you to learn about the capabilities of the NumPy library to determine a suitable method of implementing the desired effect. You can do whatever you like, but we will warn you that iterating through each pixel of a large NumPy array can be substantially (thousands of times) slower than using one of NumPy’s built in ’vectorized’ operations. A fast program is not necessary for correctness, but it will be extremely useful if you wish to run your program multiple times as you debug your code.
2.2 Your Code and Our Supplied Code
You should follow the environment setup instructions available on the EdX course webpage. These instructions will help you install Poetry, the Python package manager that will in turn help you install the GUI dependences for this project. Please make a post on Piazza for environment setup help.
We will provide code in the files effect.py and gui.py. The first file establishes a framework for writing image effects—specifically, by writing subclasses of the ImageEffect class and overriding the abstract method apply, which applies your filter to an input image and returns the output. We have also provided transformations.py, which contains an example filter for you to base your filters on. All of the code that you write should be added to the transformations.py file.
The code in gui.py gives you a mechanism for visualizing the effect of applying a filter to images. If you run it as follows you should see a GUI pop up with the option to choose an input image and a filter from among the filters that you have implemented.
poetry run python3 gui.py
2.3 Naming Conventions
The names of your subclasses should match the names of the effects as given above. Thus, the VerticalReflect subclass should implement the VerticalReflect effect.
2.4 Interfaces
Your code should not alter the interfaces that we provide. For this project, you only need to modify transformations.py.
2.5 Programming Environments
Feel free to use any Python programming environment (VSCode, Pycharm, etc.) to complete your assignment.
2.6 Testing Your Software
To further stimulate your thought process, here are just some of the questions that you might consider.
• You might also consider asking the question the other way: Given an image, which effects does it usefully test? What effects does it not usefully test?
• If you were to apply your test methodology to someone else’s code, how confident would you be that their code is correct? Can you think of other testing approaches that would increase your confidence?
3 Good Karma
If you’d like an extra challenge, there are many other effects that you could implement. The Smooth effect is an example of a neighborhood filter, which operates on a small window of pixels, but the shape and size of the neighborhood can vary, and the function utilizing the neighborhood pixels is limited only by your imagination. One special class of neighborhood filter is a convolution, in which the neighborhood function is a linear combination of the pixel values in the neighborhood. For example, each final pixel could be the average pixel value of the corresponding original pixel and its eight adjacent pixels. By adjusting the coefficients of the linear function, many different effects can be created, including the following.
• Sharpen: Light pixels near dark pixels should be made lighter, and dark pixels near light pixels should be made darker. This effect will highlight neighborhoods of high contrast.
• Highlight edges: Each pixel should be replaced with a light pixel if it came from a neighborhood of high contrast (an edge), or replaced with a dark pixel if it came from a neighborhood of low contrast.
• De-noise: Replace each pixel with the median pixel value of a small neighborhood.
• Erode: Replace each pixel with the minimum pixel value of a small neighborhood. This effect expands the darker regions of an image, “eroding” the edges of lighter regions.
• Dilate: Replace each pixel with the maximum pixel value of a small neighborhood. This effect expands the lighter regions of an image.
Of course, there are many other image effects that you could implement. For example, you might implement transparency, in which a parameter specifies the degree of transparency, with 0% being completely opaque and 100% being completely transparent. Feel free to be creative as you think of new effects to implement, and feel free to do your own research to figure out how to implement such effects.
If you choose to implement Karma, please write a description of the effects you create inside pyimagemanipulation/karma.txt
4 What To Turn In
Use the Canvas website to submit a single ZIP file that contains your modified pyimagemanipulation folder. Specific instructions can be found at this course’s EdX site under the ”Submission Instructions” page. Canvas is available at http://canvas.utexas.edu/.
Important Details.
• Canvas has rather small quotas, so your submissions should not include image files.
Acknowledgments. We thank Ashlie Martinez, Arthur Peters, Josh Eversmann, Leo Orshansky, and Matthew Giordano for their revisions of this assignment.

More products