In this part of the assignment, you will write functions that do various image manipulations or compuations.
numpy
, imageio
, and matplotlib.pyplot
packages, but you may not use any other imports to solve this problem.numpy
arrays) while others will work with color images (3-d numpy
arrays). Each problem will specify which type of image your solution should handle and you do not need to handle the other for that problem.hw6_main.py
.invert_colors
Input: A color image represented as a 3-d numpy
array.
Write a function called invert_colors
that takes a color image and returns a new color image with all the colors "inverted". An inverted image changes each color channel so that if there was a high value for a color at a pixel, the result will have a low value instead for that channel. The specifics of the inversion is, for each color channel, to subtract the pixel value from the number 255; if there was a high value (near 255), then it will become small after this computation and vice versa. The result of this function should be returned as a numpy
array.
You may use no loops to solve this problem. Everything should be done using numpy
computations. This method should NOT modify the input parameter, but rather should return a new numpy
array.
For example, if we were to call this function passing in the array for images/puppy.png
and plotting the result, we would see:
blur
Input: A gray-scale image represented as a 2-d numpy
array and a number specifying the size of the "patch" (described below).
Write a function called blur
that takes a gray-scale image and the patch size and returns a new image that has been blurred using the given patch size. This is essentially a convolution where we are using a kernel with width and height of the patch size, where we take the average of all the values that overlap with the kernel at each iteration. This is equivalent to doing a convolution with a kernel of this size, where all the values are \(\frac{1}{patch\_size^2}\). The return should be a new numpy
array that stores the averages values from the convolution casted to ints. Below are two example calls, one that shows a very small example that can be done by hand and the second an actual image with different patch sizes.
You will need to convert the values in the resulting matrix to integers so it can be plotted properly. To do this, you can convert numpy
array of floats to a numpy
array of integers. Assuming you have a numpy
array of floats stored in a variable called result
, you can create a new array of ints with the line result.astype(np.uint8)
.
You may use two loops to solve this problem (to move the sliding window). Everything else should be done using numpy
computations. This method should NOT modify the input parameters, but rather should return a new numpy
array.
Development Strategy:
template_match
Input: A gray-scale image represented by a 2-d numpy
array and a smaller gray-scale image represented by a 2-d numpy
array.
For this problem, we will be implementing a common task of finding the instance of a small image in a larger image. For example, we might have an image of a specific coin (the template) and an image of many coins and we want to find where the coin appears in this larger image. At a high level, this algorithm works like a convolution over the image using the template as the kernel, where it computes a "similarity measure" between the template and the current portion of the image. The result returned should be the similarities at each possible point, which our provided code will use to find the most likely spot by finding the position with maximum similarity.
The similarity measure we will use is based on the idea of a "cross correlation". For each position of the template in the algorithm, we will compute the sum of element-wise product between the current section of the image and the template (much like a step of a convolution). However, we don't want to use the raw pixel values since high brightness can cause artificially high similarities. Instead, we have to "de-mean" both the template and the section of the image we are currently looking at by subtracting their average pixel values from all their respective pixels. At a pseudo-code level, this involves for each valid position of the template over the image:
This function should return a 2-d numpy
array of floats that stores the similarity between the template at each valid point in the image. You should NOT round or cast the array to integers. You should see the last problem for an appropriate development strategy.
You may use two loops to solve this problem (to move the sliding window). Everything else should be done using numpy
computations. This method should NOT modify the input parameters, but rather should return a new numpy
array.