简体   繁体   English

OpenCV 2.4.3和Python

[英]OpenCV 2.4.3 and 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. OpenCV库是我需要的东西,但事实证明用Python学习很难,特别是在OpenCV 2.4.3更新之后,它有非常纤薄的Python相关文档。 So I now understand that there was a bunch of changes in OpenCV, for exaxmple 所以我现在明白OpenCV中有很多变化,例如excmple

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. 我的意思是,是的,有新的python-opencv语法的例子,但它非常狭窄,并且被证明很难理解。 For example: Example in official documentation for Python code 例如:Python代码的官方文档中的示例

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? 那么有没有任何与Python相关的参考文档/教程/书籍/指南以及最新的OpenCV,其中有一些基本解释,不会让像我这样的新手与C ++或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. 实际上,使用新的cv2模块,与旧的cv接口相比,OpenCV变得更加简单。 Not just simple, but very fast and highly productive, due to the Numpy support. 由于Numpy的支持,不仅简单,而且速度快,效率高。 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? 我希望您访问一个SOF,它显示了两个模块之间的比较: 所有这些OpenCV Python接口之间有什么不同?

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 另外一个SOF就在这里,这是一个关于如何使用Numpy支持加速代码的简单演示: OpenCV-Python接口,cv和cv2的性能比较

You need not learn C++ or C to use OpenCV, although C++ is the official language. 虽然C ++是官方语言,但您无需学习C ++或C来使用OpenCV。 Still, Python-OpenCV has good support. 不过,Python-OpenCV还是有很好的支持。 Once you get a grip on how to use OpenCV, you will be able to convert C++ codes into Python yourself. 一旦掌握了如何使用OpenCV,您就可以自己将C ++代码转换为Python。 Then you can learn OpenCV from C++ tutorials also. 然后你也可以从C ++教程学习OpenCV。 For example, I started learning OpenCV from "Learning OpenCV" by Gary Bradsky which is completely in C++. 例如,我开始从Gary Bradsky的“Learning OpenCV”学习OpenCV,它完全是用C ++编写的。 At that time, there was only cv interface. 那时,只有cv接口。

As you mentioned in your comments, opencvpython.blogspot.com has some introductory tutorials. 正如您在评论中提到的,opencvpython.blogspot.com有一些入门教程。 I started it focussing newbies in OpenCV. 我开始专注于OpenCV中的新手。

Also, check this SOF for more tutorials : Books for OpenCV and Python? 另外,请查看此SOF以获取更多教程: OpenCV和Python的书籍?

To take it from another angle and allow you to run older code with new OpenCV installation versions... 从另一个角度来看它,并允许您使用新的OpenCV安装版本运行旧代码...

First off the move from cv to cv2 has to do with the library using different data structures for a lot of functions. 首先从cv迁移到cv2与使用不同数据结构的库有很多功能。 The easiest way to tell if a function has changed between cv2 and cv is that cv functions start with a capital. 判断函数是否在cv2和cv之间发生变化的最简单方法是cv函数以大写字母开头。 Reworked cv2 functions seem to always have the first letter in lowercase. 重写的cv2函数似乎总是以小写字母表示第一个字母。 So if you are using an old book or old examples, you can still use the legacy cv. 因此,如果您使用旧书或旧示例,您仍然可以使用旧版cv。 cv is now simply embedded in cv2. cv现在只是嵌入在cv2中。 Simply use the following at the top of your scripts 只需在脚本顶部使用以下内容即可

    import cv2
    import cv2.cv as cv #required for old code not to be changed

This allows you to simply run older code without changing it. 这使您可以简单地运行旧代码而无需更改它。 I will demonstrate with your function call here. 我将在这里演示您的函数调用。 You had... 你有过...

    cv.cvtColor('proba.jpg', 'CV_RGB2GRAY')

The first thing I notice is that your function may be called wrong. 我注意到的第一件事是你的功能可能被称为错误。 (Given first letter of function is lower case it should start with cv2 not cv). (鉴于函数的第一个字母是小写,它应该以cv2而不是cv开头)。 Second is the 'code' you are passing the function. 第二个是你传递函数的'代码'。 'Codes' are members (coding noob here, forgive me if some of my vocab is inaccurate) of cv2 and cv but not always the same. 对于cv2和cv,'代码'是成员(编码noob在这里,请原谅我,如果我的一些词汇是不准确的),但并不总是相同。 You have 'CV_RGB2GRAY'. 你有'CV_RGB2GRAY'。 First off, no quotes. 首先,没有报价。 This is a cv 'code' not cv2. 这是一个cv'代码'而不是cv2。 Also you are missing the 'cv.' 你也错过了'cv。' in front. 在前。 To demonstrate here is how I believe your function should be called for old cv version: 为了演示这里我是如何相信你的函数应该为旧的cv版本调用:

    cv.CvtColor('proba.jpg', cv.CV_RGB2GRAY) #Assuming you used listed imports
    cv2.cv.CvtColor('proba.jpg', cv2.cv.CV_RGB2GRAY) #Assuming you skipped second import

And now cv2... 而现在cv2 ......

    cv2.cvtColor('proba.jpg', cv2.COLOR_RGB2GRAY)

There you go, I hope this helps. 你去,我希望这会有所帮助。 Remember that given python runs off of scripts you can type anything you are unsure of directly into command line. 请记住,给定python运行脚本,您可以直接在命令行中键入任何不确定的内容。 This does wonders for helping me build my understanding (I first used python 5 days ago). 这对于帮助我建立理解(我5天前第一次使用python)确实很奇怪。 For example if you were wondering why it wanted an integer in your function, when you type 例如,如果您想知道为什么它需要函数中的整数,当您键入时

    cv.CV_RGB2GRAY

directly into the python command line, it spits '7' (handy that it is an int) back at you. 直接进入python命令行,它会向你吐出'7'(方便它是一个int)。 The cv2 version spits out '7L'. cv2版本吐出'7L'。 Just remember to use the WaitKey() function now and again in some form otherwise highgui may not have the required time to process some commands, in some situations. 只需记住现在以某种形式再次使用WaitKey()函数,否则在某些情况下,highgui可能没有处理某些命令所需的时间。 Well that wraps it up. 好吧,它包装起来。 Sorry if I covered some things that were already covered or referenced to. 很抱歉,如果我介绍了一些已经涵盖或参考过的内容。 If I did feel free to delete it, admins. 如果我确实随意删除它,管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM