OpenCV 2.4.3和Python [英] OpenCV 2.4.3 and Python

查看:620
本文介绍了OpenCV 2.4.3和Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我开始寻找一个很好的方式来制作一个简单的计算机视觉系统。 OpenCV库是我需要的,但它证明很难用Python学习,特别是在OpenCV 2.4.3更新之后,它们具有非常纤薄的Python相关文档。所以我现在明白,OpenCV有一些变化,对于exaxmple

  import cv 

现在

  import cv2 

还有一些模块丢失。我的意思是,是的,有新的python-opencv语法的例子,但它非常狭窄,被证明是很难理解。
例如:
Python代码的官方文档示例

  cv2.cvtColor(src,code [ ,dst [,dstCn]])

我知道这段代码的意思和使用方法,至少我想我知道。但是编写源代码和颜色代码什么都不给我:

 追溯(最近的最后一次调用):
文件 C:\FILEFOLDER\tut.py,第11行在< module>
cv.cvtColor('proba.jpg','CV_RGB2GRAY')
TypeError:需要一个整数

或者如果我尝试编写像变量的代码:

 追溯(最近的最后一次呼叫):
文件C:\FILEFOLDER\tut.py,第11行在< module>
cv.cvtColor('proba.jpg',CV_RGB2GRAY)
NameError:名称'CV_RGB2GRAY'未定义

所以有没有任何Python相关的参考文档/教程/书/指南最新的OpenCV与根本解释,不会像我这样的新手像C ++或Java的不需要的代码示例混淆?

解决方案

我认为你是采取相反的路线。



其实,新的 cv2 模块,与旧的 cv 接口相比,OpenCV变得更加简单。不仅简单,而且非常快速,高效,因为Numpy的支持。只有这样,我们应该知道如何正确使用它。



这里,您应该使用以下功能:

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

我希望您访问一个SOF,显示两个模块之间的一些比较:所有这些OpenCV Python接口之间有什么不同?



另一个SOF在这里,这是一个简单的演示,说明如何加快Numpy支持的代码:



你的性能比较的OpenCV-Python接口,cv和cv2的性能比较不需要学习C ++或C来使用OpenCV,尽管C ++是官方语言。不过,Python-OpenCV有很好的支持。一旦掌握如何使用OpenCV,您将能够自己将C ++代码转换成Python。那么你也可以从C ++教程中学习OpenCV。例如,我开始学习OpenCV从学习OpenCV由Gary Bradsky完全在C ++。那时候,只有 cv 接口。



正如你在评论中提到的那样,opencvpython.blogspot.com有一些介绍性的教程。我开始把它重点放在OpenCV的新手上。



另外,请查看这个SOF了解更多教程: OpenCV和Python的书籍


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?

解决方案

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?

这篇关于OpenCV 2.4.3和Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆