Exercise 1 - Warm up
This exercise is meant to get you started with some simple image operations.
Task 1
Load the image ‘football.jpg’, convert it to a greyscale image and apply a 5x5 mean filter.
- When applying convolution in ‘full’ mode, the resulting image will have a 2 pixel wide black border. Remove the border by slicing the image before displaying it.
- Use convolution with mode ‘valid’ to remove this border.
- Use the boundary options to construct an image with the same size as the original. Experiment with different padding techniques such as constant padding and mirroring (symmetric padding).
Task 2
Make a function that takes an 8-bits greyscal image as input argument, and
returns a histogram of the graylevel intensities. Make sure that it returns the
same as np.histogram(image.flatten(),bins=256,range=[0,255],density=False)
-
Although it is allowed to use loops, try to avoid using them where it is possible. One loop should suffice.
Task 3
Load the image ‘coins.png’ (found here), and do the following
a) Use the operators >, <, >=, <= to threshold the image using an arbitrary threshold. That is, divide the image pixels into two classes, the ones below and the ones above the threshold, and give them different values (e.g. 0 and 255)
b) Compute the an optimal threshold with Otsu’s algurithm by using the following (or similar) functions
image = cv2.imread('coins.png', cv2.IMREAD_GRAYSCALE)
threshold, thresholded_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
c) Compare the binary image resulting from part a with the one from part b by displaying the images. Do you notice any differences? Display also the difference between the images.
Task 4
Compute the gradient magnitude of the image ‘football.jpg’ (found here). Rescale it to take values between 0 and 255 (inclusive). Threshold the result at e.g. graylevel value 100. Use this to obtain an image containing only the seam of the ball.