Python for Computer Vision 2
Operators
-
Arithmetic
-
Inplace operations
-
Relational - > < = !=
-
Logical
-
any(a) and all(a)
-
logical_and
-
A and B
for lists
-
where
Data structures
-
hashtable, dict
-
lists, queues, deques
-
array <-> list
-
import collections
Sets and Statistics
-
Sets
a = set([1, 2, 4, 5, 5])
b = set([3, 5, 5])
-
Operations - union, intersect, unque1d, intersect1d
-
Statistics
Debugging example
-
Standard python debugger
-
Post mortem debugging
-
Ipython debugger
-
auto break on error with %pdb
-
Can be used standalone or within ipython
-
Advantages over pdb
-
Tab completion
-
Syntax colouring
-
Plot functions to help see problems
-
Place breakpoints in code
-
import pdb; pdb.set_trace()
-
Set break point by line number
pdb buggy.py
b 23
c 23
Fancy indexing
Ndimage
Ndvision
Refactoring
-
Generally easier
-
Duck typing
-
Changing method signatures
-
add default argument
-
Named/labelled arguments makes order less important
-
Tools
-
Rope
-
Bicycle Repair man
-
Search and replace easier
Publication quality plots
-
Latex within plots
-
Combining with pdflatex
-
Vector graphics ideal
-
2D plots
-
3D plots
-
Vector graphics possible but often not suitable
-
Fonts for publication
-
pdffonts utility
-
Have to be embedded and subsetted for publication
Publication quality plots code
import matplotlib.pyplot as plt
from matplotlib import rc
fig = plt.gcf()
rc('text', usetex=True)
rc('font', family='serif')
rc('legend', fontsize='medium')
plt.plot(np.arange(10)**2)
plt.xlabel('$x$')
plt.ylabel('$x^2$')
plt.show()
plt.savefig('graph.pdf')
Visualisation
-
2D is a subset of 3D
-
matplotlib
-
import matplotlib.pyplot as plt
-
Capable of 3D as well (not GPU rendered)
-
3D plotting with mayavi2
-
Alternatives
-
Vpython/python visual
-
python-vtk - Visualization toolkit bindings for python
Example Visualisation
Example Visualisation Code
#! /usr/bin/ipython -wthread
import pylab
import enthought.mayavi.mlab as mlab
I = pylab.imread('dichroic.png')
mlab.surf(100*I[...,0])
mlab.savefig('dichroicsurf.png')
Simple animations
from pylab import *
import time
ion()
tstart = time.time() # for profiling
x = arange(0,2*pi,0.01) # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
line.set_ydata(sin(x+i/10.0)) # update the data
draw() # redraw the canvas
print 'FPS:' , 200/(time.time()-tstart)
Examining Video
-
Load video as 4D array
-
image height, image width, RGB, frame number
-
Large arrays
-
memmap
-
hdf5
-
Display frame
-
Plot pixel colour variation over time
-
Example video_array.py
-
Exercise for reader
-
Generate video thumbnails/comic strip
Computer Vision
-
python opencv
-
Grab frame from camera
-
Frame differencing demo
-
Camscan demo
-
Surf to look at images
-
Dichroic filter eample
Frame differencing demo
import sys
import time
import cv
def get_image():
return cv.QueryFrame(camera)
#camera = cv.CreateCameraCapture(0)
camera = cv.CaptureFromFile('/jcr/coding/opencv/tree.avi')
cv.NamedWindow('Frame Difference')
cv.NamedWindow('Original')
cv.StartWindowThread()
lastim = get_image()
D = cv.CloneImage(lastim)
while True:
im = get_image()
if im == None: break
cv.AbsDiff(im, lastim , D)
cv.ShowImage('Frame Difference', D)
cv.ShowImage('Original', im)
lastim = cv.CloneImage(im)
keypress = cv.WaitKey(200)
Data analysis
-
Data
-
Loading
-
Plotting
-
Analysing
-
Processing
-
plotfile example
Example Parsing of a data file
-
Side by side of matlab code
-
Equivalent python code
-
Load whole file into memory as an array
-
load and parse line by line
-
load chunks of file as arrays and parse
-
Make a string appear like a file
-
known as memory files
-
cStringIO
-
StringIO
Performance
-
Use built in numpy functions
-
Apply along axis
-
Calling C
-
Inline C with weave
-
Google working on fast python
Misc
-
openrave with python
-
RANSAC demo
-
Optimizing
-
Python C integration
Debugging C called from python
-
Possible when using cpython implementation of python
-
get the PID
-
gdb PID
-
More info
Relevant Software Libraries and Tools
-
OpenCV - Large C++/Python/C computer vision library
-
PCL - Point cloud processing library
-
Ndimage - python N-dimensional array library
-
Ndvision - video processing based on N-dimensional arrays
-
MROL - Multi-resolution occupied voxel lists for efficient environment representation and mapping
Further information
-
Scipy website
-
python help files
-
Ipython
-
Mathesaurus
-
Interesting links
Scientific programming with Python
-
Scientific programming with python and svn
-
Why python?