Few days ago I went into searching for a good way to make a simple computer vision system. OpenCV library is something I need but it proved hard to learn with Python especially after OpenCV 2.4.3 update which have very slim Python related documentation. So I now understand that there was a bunch of changes in OpenCV, for exaxmple
And there is bunch of modules that is missing. I mean, yes there are examples of the new python-opencv syntax but it's very narrow and proven to be hard to understand. For example: Example in official documentation for Python code
cv2.cvtColor(src, code[, dst[, dstCn]])
I know what this code means and how to use it, at least I think i know. But writing source and color code does nothing just give me :
Traceback (most recent call last): File "C:\FILEFOLDER\tut.py", line 11, in <module> cv.cvtColor('proba.jpg', 'CV_RGB2GRAY') TypeError: an integer is required
Or if i try to write code like variable:
Traceback (most recent call last): File "C:\FILEFOLDER\tut.py", line 11, in <module> cv.cvtColor('proba.jpg', CV_RGB2GRAY) NameError: name 'CV_RGB2GRAY' is not defined
So is there any Python related reference document/tutorial/book/guide for newest OpenCV with the ground up explanations that does not confuse newbie like me with unwanted code examples for C++ or Java?
answered 2012-11-17 14:25:04 -0600Dan Maynard
Python documentation is a little patchy, but in my opinion worth persisting with. Sometimes the C++ description is very helpful as it says the type of array required.
The main problem with your code examples is that OpenCV Python functions which process images take as input Numpy arrays. This means you have to read the image from .jpg into an array, and then pass it to the function. The result from cvtColor is another Numpy array.
import numpy as np import cv2 im=cv2.imread('c:/temp/i1.jpg',cv2.cv.CV_LOAD_IMAGE_COLOR) type(im) #Shows Numpy array im.shape #Numpy array object shape gives image size cv2.imshow('Colour',im) im_gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) cv2.imshow('Gray',im_gray) #show image on screen cv2.imwrite('c:/temp/i1g.jpg',im_gray) #write to file im_gray[...]=0 #Use Numpy broadcasting cv2.imshow('Turned black',im_gray)
If you unpack the OpenCV source there is a directory of samples called opencv\samples\python2 These are all using the cv2 interface.
answered 2012-11-15 21:34:26 -0600worst guy ever
I sympathise with your confusion at the state of the official documentation, but your example is an issue with your Python syntax, not with OpenCV or its documentation. Importing like so:
will not add symbols in the
cv module to the local namespace. You can either reference the
cv module every time you want to use a symbol from it (e.g.:
cv.CV_RGB2GRAY) or import the symbols you need into the local namespace (e.g.:
from cv import cvtColor, CV_RGB2GRAY) or just import everything:
from cv import *.
I don't know of any better source of documentation, sorry.
Asked: 2012-11-14 09:55:54 -0600
Seen: 3,211 times
Last updated: Nov 17 '12