Example 1

From Devipedia

Jump to: navigation, search

I'm working through the book Learning OpenCV by Gary Bradski & Adrian Kaehler. Being a Python lover, I'm going to try out / port the sample programs from C to Python as I read along. The Python bindings were reportedly "semi-automatically created from the C/C++ headers and therefore contains no Python documentation. Because all identifiers are identical to their C/C++ counterparts, you can consult the standard manuals that come with OpenCV."


On page 17 we see our first sample program in C. Here's a Python version. Note that functions that exist in the global namespace in C/C++ are spread between the opencv.highgui and opencv.cv namespaces in Python. I'm just importing both to keep the syntax as close as possible to the C/C++ examples in the book.

import sys
from opencv.highgui import *
from opencv.cv import *

if __name__ == '__main__':

    img = cvLoadImage(sys.argv[1])
    cvNamedWindow("Example", CV_WINDOW_AUTOSIZE)
    cvShowImage("Example", img)
    cvWaitKey(0)
    cvReleaseImage(img)
    cvDestroyWindow("Example")

Invoke with:

dvenable@dvenable:~/Ubuntu One/src/py/opencv$ python example1.py ~/cartoon.jpg 

And---drum roll please---the cartoon displays in a Window! Yeah, not too exciting, but you've got to start somewhere. Also, closing the window with the 'X' causes...

*** glibc detected *** python: double free or corruption (fasttop): 0x088ec820 *

...but let's not quibble with memory corruption just yet. We're just getting started.

Personal tools