Skip to the content.

NumPy and OpenCV Exercises

Back to OpenCV

If you get stuck, check back with the intro tutorials or ask the Vision Sublead or CSE.

Table of Contents

Getting Started

If you haven’t done this stuff to set up your environment, make sure to do so before getting started.

Installing Packages

Linux:

# pip install numpy
# python -m pip install numpy
# python3 -m pip install numpy

Windows:

# py -m pip install numpy
# py -m pip install scipy
# py -m pip install matplotlib
# py -m pip install opencv-python

Importing Packages

import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
from scipy.ndimage import convolve

Load an Image

img_name = mpimg.imread("filename")

Show an Image

plt.imshow(img_name)
plt.show()

or

cv2.imshow("Window Name", image_var)
cv2.waitKey(0)
cv2.destroyAllWindows()

Save an Image

plt.imsave(img_name, "filename")

Exercises

Exercise 1

Create and show/save a 10x10 spiral 1-bit image by modifying np.zeros() using only manual slicing operations (For help, see #3 of Intro to Computer Vision 1).

The completed image, when shown, should look like this:

image

Download Image

Exercise 2

Crop a square out of the “trump-putin.jpg” in such a way that we only see the flower in the center and no trace of either of the two people. Show/save this image. You must use array slicing operations. Don’t worry too much about getting it perfect, just make it so that we don’t see up to their black suits.

Starting Image:

image

Download Image

One possible solution could look like this:

image

Download Image

Exercise 3

Combine “striped-leaf-pattern0.jpg” and “striped-leaf-pattern1.jpg” into one continuous leaf pattern image. Display/save this image. You must use np.where(). The core of this exercise (without the code for loading/showing/saving the images) can be done in a single line of code. (Don’t worry if the displayed image has purple stripes, the saved image should be correct).

Starting Images:

image

Download Image

image

Download Image

Here is what the solution should look like:

image

Download Image

Exercise 4

Consider “strawberry.jpg”. Figure out why the colors are messed up, and “squish” the image to make it look normal. Once done, remove as much of the black and white from this image (don’t worry about the shadow/reflection) such that there’s no more than 2 pixels of slack around the strawberry. Show/save the final image.

Starting Image:

image

Download Image

One possible solution looks like this:

image

Download Image

Exercise 5

Consider “rose-piano.jpg”:

image

Download Image

a) Apply blurring to “rose-piano.jpg” using image convolution with this normalized box blur kernel. You must use scipy’s convolve() function. Display/save this image.

image

One possible solution to part a looks like this:

image

Download Image

One possible solution to part b looks like this:

image

Download Image

image

One possible solution to part c looks like this:

image

Download Image

One possible solution to part d looks like this:

image

Download Image

One possible solution to part e looks like this:

image

Download Image

Exercise 6

Apply k-means clustering to the “lake.jpg” image with varying kernel (K) sizes of 3, 5, and 7. Display/save the images from each kernel size. Refer to numbers 8-10 of the Introduction to Computer Vision 2.

Starting Image:

image

Download Image

The clustered images should look like this:

image

Download Image

image

Download Image

image

Download Image




Completed the exercises and want something more challenging? Check out the intro projects here.