OpenCV-Python接口,cv和cv2的性能比较 [英] Performance comparison of OpenCV-Python interfaces, cv and cv2
问题描述
几天后,我开始使用新的OpenCV-Python介面, cv2
。
关于 cv
和 cv2
接口的比较。
关于易用性,新的 cv2
界面已经提高了很多,使用 cv2
但是速度怎么样?
我做了两个小代码片段,一个在 cv
,另一个在 cv2
以下是代码:
$ b b $ b
cv2接口
:
导入时间
import numpy as np
import cv2
grey = cv2.imread('sir.jpg' 0)
width = gray.shape [0]
height = gray.shape [1]
h = np.empty([width,height,3])
t = time()
for x in xrange(width):
for x 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
打印time taken =,t2
========================== =========================
结果是:
耗时= 14.4029130936
==================== ==================================
cv interface:
import cv,time
grey = cv.LoadImage('sir.jpg',0)
h = cv.CreateImage(cv.GetSize(gray),8,3)
t = time.time
for i in xrange(gray.width):
for x 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
printtime taken =,t2
cv.ShowImage('img',h)
cv.WaitKey(0)
=============================== =======================
结果是:
time taken = 1.16368889809
======================== =============================
查看,这里老 cv
大约比
。所得到的图像是相同的。 (输入图片大小为720x540) cv2
快12倍
为什么会发生这种情况?
cv2比cv慢吗?
还是我在这里犯了什么错误?对于上述代码,在cv2中有更快的方法吗?
cv2.imread()返回的图像是一个数组对象的NumPy。因此,您可以使用NumPy的函数来加速计算。
下面的程序显示如何通过使用itemarl
导入时间
import numpy as np
import cv2
gray = cv2.imread('lena_full.jpg',0)
height,width = gray.shape
h = np.empty((height,width,3),np.uint8)
b $ bt = time.time()
for i in xrange(height):
for x in xrange(width):
k = gray.item(i,j)
如果k == 127:
h.itemset(i,j,0,255)
h.itemset(i,j,1,255)
h.itemset 2,255)
elif k> 127:
h.itemset(i,j,0,0)
h.itemset(i,j,1,0)
h.itemset(i,j, k)
else:
h.itemset(i,j,0,k)
h.itemset(i,j,1,0)
h.itemset j,2,0)
print time.time() - t
程序显示如何首先创建调色板,并使用NumPy的数组索引获得结果:
t = time.time
palette = []
for i in xrange(256):
if i == 127:
palette.append((255,255,255))
elif i> 127:
palette.append((0,0,255-i))
else:
palette.append((i,0,0))
palette = np.array palette,np.uint8)
h2 = palette [gray]
print time.time() - t
print np.all == h2)
输出为:
0.453000068665
0.0309998989105
True
cv版本输出是:
0.468999862671
注意:轴0的长度是图片的高度,轴1的长度是图片的宽度
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?
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.
import time
import numpy as np
import cv2
gray = cv2.imread('lena_full.jpg',0)
height, width = gray.shape
h = np.empty((height,width,3), np.uint8)
t = time.time()
for i in xrange(height):
for j in xrange(width):
k = gray.item(i, j)
if k == 127:
h.itemset(i, j, 0, 255)
h.itemset(i, j, 1, 255)
h.itemset(i, j, 2, 255)
elif k > 127:
h.itemset(i, j, 0, 0)
h.itemset(i, j, 1, 0)
h.itemset(i, j, 2, 255-k)
else:
h.itemset(i, j, 0, k)
h.itemset(i, j, 1, 0)
h.itemset(i, j, 2, 0)
print time.time()-t
And the following program show how to create the palette first, and use NumPy's array index to get the result:
t = time.time()
palette = []
for i in xrange(256):
if i == 127:
palette.append((255, 255, 255))
elif i > 127:
palette.append((0,0,255-i))
else:
palette.append((i, 0, 0))
palette = np.array(palette, np.uint8)
h2 = palette[gray]
print time.time() - t
print np.all(h==h2)
The output is:
0.453000068665
0.0309998989105
True
The cv version output is :
0.468999862671
Note: the length of axis 0 is the height of the image, the length of axis 1 is the width of the image
这篇关于OpenCV-Python接口,cv和cv2的性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!