Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV 2.4.3 and Python

Tags:

python

opencv

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

import cv

is now

import cv2

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?

like image 235
Domagoj Avatar asked Nov 14 '12 15:11

Domagoj


People also ask

What version of Python is compatible with OpenCV?

Install python 3.7 with anaconda package and use this command to install opencv.

Is OpenCV compatible with Python 3?

There are two major versions of Python, Python2, and Python3. We will be covering the latest version of Python throughout the tutorials as it supports newer features. The OpenCV doesn't care much about the version of Python, the bindings will work just the same.

Is OpenCV-Python different from OpenCV?

OpenCV.org is pleased to announce that the popular and long-running package OpenCV-Python is now an official OpenCV project. What is OpenCV-Python? It's a package that contains pre-built OpenCV with dependencies and Python bindings, so there's no need to install OpenCV separately.

Does OpenCV work with Python?

OpenCV now supports a multitude of algorithms related to Computer Vision and Machine Learning and is expanding day by day. OpenCV supports a wide variety of programming languages such as C++, Python, Java, etc., and is available on different platforms including Windows, Linux, OS X, Android, and iOS.


1 Answers

I think you are taking it in the reverse path.

Actually, with the new cv2 module, OpenCV has become far more simple compared to old cv interface. Not just simple, but very fast and highly productive, due to the Numpy support. Only thing is that, we should know how to use it appropriately.

Here, you should use the function as follows :

img = cv2.imread('pic.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

I would like you to visit one SOF which shows some comparison between both the modules : What is different between all these OpenCV Python interfaces?

Another one SOF is here, which is a simple demonstration on how you can speed up the code with Numpy support : Performance comparison of OpenCV-Python interfaces, cv and cv2

You need not learn C++ or C to use OpenCV, although C++ is the official language. Still, Python-OpenCV has good support. Once you get a grip on how to use OpenCV, you will be able to convert C++ codes into Python yourself. Then you can learn OpenCV from C++ tutorials also. For example, I started learning OpenCV from "Learning OpenCV" by Gary Bradsky which is completely in C++. At that time, there was only cv interface.

As you mentioned in your comments, opencvpython.blogspot.com has some introductory tutorials. I started it focussing newbies in OpenCV.

Also, check this SOF for more tutorials : Books for OpenCV and Python?

like image 118
Abid Rahman K Avatar answered Sep 19 '22 15:09

Abid Rahman K