If you are running on your own machine then install the whole tutorial. If you're running somewhere else... maybe in Aber Comp Sci Delph ... then you need to run standalone OpenCV. This is installed in the uni.
First we need to import the relevant libraries: OpenCV itself, Numpy, and a couple of others. Common and Video are simple data handling and opening routines that you can find in the OpenCV Python Samples directory or from the github repo linked above. We'll start each notebook with the same includes - you don't need all of them every time (so this is bad form, really) but it's easier to just copy and paste.
In [2]:
# these imports let you use opencv
import cv2 #opencv itself
import common #some useful opencv functions
import video # some video stuff
import numpy as np # matrix manipulations
The following box is useless if you're not using a notebook - they just enable the online notebook drawing stuff.
In [3]:
#the following are to do with this interactive notebook code
%matplotlib inline
from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
import pylab # this allows you to control figure size
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
It's worth noting here that we're using matplotlib to show the images, as we're using a Jupyter notebook. This means we need to think about things like "colormaps".
When you use OpenCV outside of the notebook context, there are other things to remember - you'll need to create a window to put the image into with
cv2.namedWindow("windowname")
Then to put an image in the window you've created, you can use
cv2.imshow("windowname",image_variable)
This won't actually do anything until you call the waitKey function, which is the function that does most of the work when it comes to OpenCV image display. It waits for user input, storing any keypresses in the keypress variable; however it also does the window redraw. t is the delay in milliseconds.
keypress=cv2.waitKey(t)
This is a gotcha for the opencv interface: even if you have no interest in waiting for a milisecond or two for keyboard input you have to do the waitKey in order to redraw the images.
If at any point you want to try any of this tutorial code outside the notebook context there's a file called "play.py" which has the window management stuff in it- try copying some code from this page into that file.
In [ ]: