OpenCV 2.1 is a powerful tool for aiding developers with computer vision tasks. It provides robust support for all the basic and some complex tasks. As an open source platform is it accessible to many people. Most development is conducted in C++ which can be somewhat inaccessible for non-programmers who wish to develop and use these tools. Here is a tutorial covering the python bindings for this library, so far there does not seem to much in the way of Python instructions.
This tutorial will predominantly use the native python bindings to opencv. The principles, data-types and functions are all applicable to variant bindings (pyopen/ctypes). Tutorials looking at machine learning are conducted with pyopencv, this has complete bindings to Opencv, with machine learning functionality included. I can be contacted via the 'contact' page if anyone has queries or any enquiries.
The easiest way to get python and opencv working is to use our Launchpad PPA simply run the following command in the terminal.
$ sudo add-apt-repository repo
$ sudo apt-get python-opencv or sudo apt-get opencv
We try to maintain a working version of OpenCV for all of our tutorials via this PPA targeted at Ubuntu. If you have any problems please try and use our builds first.
Willowgarage installation guide
A more in depth guide to installing OpenCV is available from from willowgarage.com, here.
All examples are given with respect to Ubuntu Linux
Python examples are implemented "Just in time". To compile C++ examples, go to the sample folder.
$ chmod +x ./build_all.sh # Make sure the file is executable
$ sudo ./build_all.sh
In order to run Python examples.
$ python filename.py
OpenCV Python "Hello World"
OpenCV is a powerful computer vision library that is written in C. To ease the terrible burden of the memory management and the sheer mental labour that is C, there is a Python API. The clean and versatile nature of Python works wonderfully with the tools available in the OpenCV library.
What is lacking is a comprehensive 'quick start guide' to applying and using computer vision in this way. There is however a series of examples in home/OpenCV/Samples/Python (Or C, if you're feeling brave and Octave (Matlab) for those of a more academic inclination). Getting Started OpenCv documentation can be found here where there is a beginner's cookbook, but has been known to be overwhelming when first starting out. So here it is in basic steps, but when you're on your feet, this website will probably be your new home.
This tutorial was written using OpenCV 2.1. Make sure you have the up to date version as there have been python binding issues in earlier versions.
Hello world Python OpenCV example (pocv.py). (Run from the command line "python pocv.py")
print 'Hello World'
So that's a super-basic python example. We shall change this so that it opens an image and writes 'Hello World'.
Returning to pocv.py.
import cv #Import functions from OpenCV
image=cv.LoadImage('picture.png', cv.CV_LOAD_IMAGE_COLOR) #Load the image
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font
x = x position of text
y = y position of text
cv.PutText(frame,"Hello World!!!", (x,y),font, 255) #Draw the text
cv.ShowImage('a_window', image) #Show the image
cv.SaveImage('image.png', image) #Saves the image
And that's all you need to get started. Let's break it down.
Firstly you have to import the functions from opencv so that your program can use them, like with any python script.
Next is a bit of GUI work. Pocv.py creates a window to contain the image. This is best done at the beginning as it gets it set-up and out of the way. The window is given an identifier, 'a_window' in this case, and it is told to autosize to the size of the image with the second argument cv.CV_WINDOW_AUTOSIZE.
Next is the function to load the image from the same directory. It can be modified to reference folders. The second argument is the format of the image when it has been loaded. There are two alternative parameters CV_LOAD_IMAGE_GREYSCALE and CV_LOAD_IMAGE_UNCHANGED which are self explanatory.
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
A font type is created so that opencv can write to the image in a way you specify. Here the font type is the first argument cv.CV_FONT_HERSHEY_SIMPLEX. Next is the vertical scale of the font which affects how tall the text is. Horizontal scale follows and affects the width of the characters in the text. To add further modifications a shear value can be entered to the letters slope by a factor of 0-1 where 1 is 45 degrees from the vertical. Character thickness is 3 in this example making the text quite bold. Line type is the last specified parameter which is the line type of which only 3 are supported.
cv.PutText(frame,"Hello World", (5,20),font, 255)
The puttext function adds the text to the frame. Arguments consists of the location to be written to (image), the test to be written (Hello World), the co-ordinates within the image to write the text, the font that was previous initialised and the colour (R,B,G).
This function displays the image in the window that was initialised at the beginning.
The waitkey is being used in the procedure to allow time for the image to be displayed before the the program terminates.
Finally saving a copy of the image with the writing on it saves the image as a specified file name and file type.
Now that is the basic structure of an opencv program. Try experimenting replace the load image with this for video files
g_capture = cv.CreateFileCapture('somevideo.avi')
However, this will just get you one frame of the video. You'll have to iterate over the video file in order to work with the entire file.
Things to start thinking about so that you can use this tool and get things done. Images are stores as matrices where row and column co-ordinates reference pixel values in the image. If it is a colour image it has 3 colour values and if it is grey scale it has two, where white is 255 and black is 0. There are further variants on the colour values that are beyond the scope of this tutorial, however you may not encounter them at all if your only using basic image processing tools. Happy hacking!
By the end of the first 4 tutorials you should be able to process a video stream to the simple standard of this video.... (OpenCV Facebook group page). This is a Facebook group page for OpenCV users, where you can receive help and advice from other developers working with OpenCV. Also available on YouTube
Click here for the next part of the tutorial