OpenCV-Python接口、cv和cv2的性能比较
几天前,我开始使用新的 OpenCV-Python 接口 cv2
。
我的问题是关于 cv 和 cv2 接口的比较。
在易用性方面,新的cv2
界面有了很大的改进,使用cv2
确实非常简单和有趣。
但速度呢?
我制作了两个小代码片段,一个在 cv
中,另一个在 cv2
中,以检查性能。两者执行相同的功能,访问图像的像素,对其进行测试,进行一些修改等。
以下是代码:
cv2 接口
:
import time
import numpy as np
import cv2
gray = cv2.imread('sir.jpg',0)
width = gray.shape[0]
height = gray.shape[1]
h = np.empty([width,height,3])
t = time.time()
for i in xrange(width):
for j in xrange(height):
if gray[i,j]==127:
h[i,j]=[255,255,255]
elif gray[i,j]>127:
h[i,j]=[0,0,255-gray[i,j]]
else:
h[i,j]=[gray[i,j],0,0]
t2 = time.time()-t
print "time taken = ",t2
======= ================================================
结果是:
所用时间 = 14.4029130936
=================================================== ====
简历界面:
import cv,time
gray = cv.LoadImage('sir.jpg',0)
h = cv.CreateImage(cv.GetSize(gray),8,3)
t=time.time()
for i in xrange(gray.width):
for j in xrange(gray.height):
k = cv.Get2D(gray,j,i)[0]
if k==127:
cv.Set2D(h,j,i,(255,255,255))
elif k>127:
cv.Set2D(h,j,i,(0,0,255-k))
else:
cv.Set2D(h,j,i,(k,0,0))
t2 = time.time()-t
print "time taken = ",t2
cv.ShowImage('img',h)
cv.WaitKey(0)
=============================== =========================
结果是:
花费的时间 = 1.16368889809
=================================================== =====
看吧,旧的 cv
比 cv2
快12 倍
。结果图像是相同的。 (输入图像的尺寸为720x540)
为什么会发生这种情况?
cv2 比 cv 慢吗?
或者我在这里犯了什么错误?对于上述代码,cv2 中是否有更快的方法?
A few days back, I started using new OpenCV-Python interface, cv2
.
My question is regarding the comparison of cv
and cv2
interface.
Regarding the ease of use, new cv2
interface has improved far greater, and it is really easy and fun to work with cv2
.
But what about speed?
I made two small code snipplets, one in cv
and another in cv2
, to check the performances. Both does the same function, access pixels of an image, test it, make some modifications, etc.
Below is the code:
cv2 interface
:
import time
import numpy as np
import cv2
gray = cv2.imread('sir.jpg',0)
width = gray.shape[0]
height = gray.shape[1]
h = np.empty([width,height,3])
t = time.time()
for i in xrange(width):
for j in xrange(height):
if gray[i,j]==127:
h[i,j]=[255,255,255]
elif gray[i,j]>127:
h[i,j]=[0,0,255-gray[i,j]]
else:
h[i,j]=[gray[i,j],0,0]
t2 = time.time()-t
print "time taken = ",t2
=====================================================
And result is:
time taken = 14.4029130936
======================================================
cv interface:
import cv,time
gray = cv.LoadImage('sir.jpg',0)
h = cv.CreateImage(cv.GetSize(gray),8,3)
t=time.time()
for i in xrange(gray.width):
for j in xrange(gray.height):
k = cv.Get2D(gray,j,i)[0]
if k==127:
cv.Set2D(h,j,i,(255,255,255))
elif k>127:
cv.Set2D(h,j,i,(0,0,255-k))
else:
cv.Set2D(h,j,i,(k,0,0))
t2 = time.time()-t
print "time taken = ",t2
cv.ShowImage('img',h)
cv.WaitKey(0)
======================================================
The result is:
time taken = 1.16368889809
=======================================================
See, here old cv
is about 12 times faster
than cv2
. And resulting images are same. (input image is of size 720x540)
Why does this happen?
Is cv2 slower compared to cv?
Or am I making any mistake here? Is there a faster method in cv2 for the above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cv2.imread()返回的图像是NumPy的数组对象。所以你可以使用NumPy的函数来加速计算。
下面的程序展示了如何使用 ndarray 对象的 item()、itemset() 方法来加速你的 origin for 循环版本。
下面的程序展示了如何首先创建调色板,并使用 NumPy 的数组索引来获取结果:
输出为:
cv 版本输出为:
注:轴 0 的长度是图像的高度,轴1的长度是图像的宽度
The image returned by cv2.imread() is an array object of NumPy. So you can use NumPy's functions to speedup calculation.
The following program shows how to speedup your origin for loop version by using item(), itemset() method of ndarray object.
And the following program show how to create the palette first, and use NumPy's array index to get the result:
The output is:
The cv version output is :
Note: the length of axis 0 is the height of the image, the length of axis 1 is the width of the image