The following are notes used for the Python tutorial.
Go here for OpenCV installation notes.
The best tutorial I’ve found for using Python with computer vision: http://scipy-lectures.github.com/advanced/image_processing/index.html
Suggestion: use ipython by either running “ipython qtconsole –pylab” in the command line or in Windows running the program Pylab. In order to do this you need Qt installed – it will give you instructions if you don’t have it.
Tutorial Outline
- Arrays vs Matrices
- Generally use arrays, use numpy.asmatrix() if you want actual matrices (ie for multiplication)
- Any array operation (ie im1+im2) is element-wise
- Slicing: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
- General format for slicing an image: img[a:b:c, a:b:c] Where a is the starting index, b is the end index, and c is the iterator. For example to take every other index in the first row from 0 to 6 you would do img[0, 0:6:2]
- Broadcasting: http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
- Reading images and plotting data:
- See the aforementioned scipy tutorial (http://scipy-lectures.github.com/advanced/image_processing/index.html)
Other resources:
- Numpy Tutorial: http://www.scipy.org/Tentative_NumPy_Tutorial
- Numpy cookbook: http://www.scipy.org/Cookbook
- Numpy functions: http://www.scipy.org/Numpy_Example_List_With_Doc
- Numpy functions by category: http://www.scipy.org/Numpy_Functions_by_Category
- OpenCV Cookbook: http://opencv.willowgarage.com/documentation/python/cookbook.html
Frequently Asked Questions
Question: What is the best way to experiment with images and test my code?
Answer: iPython has a number of tools (such as auto-complete) that make developing with it much more pleasant. The best way to launch this in a command line is to type “ipython –pylab” or “ipython qtconsole –pylab” This automatically loads the plotting tools (pylab) for you. On windows you can simply use the program “Pylab” in the Enthought program folder.
Question: What if my scipy.misc.imread() gives an error?
Answer: You may not have the Python Imaging Library (PIL) installed. It should automatically be installed on Windows and Linux. To install on a MAc use Macports with the command “sudo port install py27-pil”
Question: What is the best way to read an image?
Answer: Use scipy.misc.imread()
Question: When I display a grayscale image using imshow() it looks weird and multi-colored. How do I fix this?
Answer: By default matplotlib colorizes single-valued data such as a grayscale image. To change the colormap so it looks normal use plt.imshow(im, cmap=plt.cm.Greys_r). Alternatively, you can change the default colormap for this session by typing plt.gray() once in your file.