OpenCV、cv、cv2的区别?

这篇博客讨论了OpenCV的两个Python接口:cv和cv2。cv接口保留了所有OpenCV数据类型,如cvMat,但操作较慢,需要特殊函数处理图像ROI。而cv2接口则将所有内容转换为NumPy数组和Python原生对象,提供了更快的性能和便利的操作,如直接使用数组切片处理图像ROI。此外,cv2返回的轮廓是NumPy数组,更易于处理。鉴于cv2的简化和高效,作者推荐使用cv2。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Officially, OpenCV releases two types of Python interfaces, cv and cv2.

cv:

I started working on cv. In this, all OpenCV data types are preserved as such. For example, when loaded, images are of format cvMat, same as in C++.

For array operations, there are several functions like cvSet2D, cvGet2D, etc. And some discussions say, they are slower.

For imageROI, you need special functions like cvSetImageROI.

If you find contours, cvSeq structures are returned which is not so good to work with compared to Python lists or NumPy arrays.

(And I think, soon its development will be stopped. Earlier, there was only cv. Later, OpenCV came with both cv and cv2. Now, there in the latest releases, there is only the cv2 module, and cv is a subclass inside cv2. You need to call import cv2.cv as cv to access it.)

cv2:

And the latest one is cv2. In this, everything is returned as NumPy objects like ndarray and native Python objects like lists,tuples,dictionary, etc. So due to this NumPy support, you can do any numpy operation here. NumPy is a highly stable and fast array processing library.

For example, if you load an image, an ndarray is returned.

array[i,j] gives you the pixel value at (i,j) position.

Also, for imageROI, array slicing can be used like ROI=array[c1:c2,r1:r2]. No need of separate functions.

To add two images, there isn’t a need to call any function, just do res = img1+img2. (But NumPy addition is a modulo operation for uint8 arrays like images. See the article Difference between Matrix Arithmetic in OpenCV and Numpy to know more.

Contours returned are lists of Numpy arrays. You can find a detailed discussion about Contours in Contours - 1 : Getting Started.

In short, with cv2 everything is simplified and pretty fast.

A simple discussion on how NumPy speed up cv2 is in Stack Overflow question Performance comparison of OpenCV-Python interfaces, cv and cv2.

pyopencv:

I don’t know much about this since I haven’t used it. But it seems to have stopped further development.

I think it would be better to stick on to official libraries.

In short, I would recommend you to use cv2!

EDIT: You can see installation procedure for the cv2 module in Install OpenCV in Windows for Python.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值