简体   繁体   English

OpenCV-Python接口,cv和cv2的性能比较

[英]Performance comparison of OpenCV-Python interfaces, cv and cv2

A few days back, I started using new OpenCV-Python interface, cv2 . 几天前,我开始使用新的OpenCV-Python接口cv2

My question is regarding the comparison of cv and cv2 interface. 我的问题是关于cvcv2接口的比较。

Regarding the ease of use, new cv2 interface has improved far greater, and it is really easy and fun to work with cv2 . 关于易用性,新cv2界面改进大得多,这是非常容易和有趣的共事cv2

But what about speed? 但速度怎么样?

I made two small code snipplets, one in cv and another in cv2 , to check the performances. 我制作了两个小代码snipplet,一个在cv ,另一个在cv2 ,以检查性能。 Both does the same function, access pixels of an image, test it, make some modifications, etc. 两者都具有相同的功能,访问图像的像素,测试它,进行一些修改等。

Below is the code: 以下是代码:


cv2 interface : 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 所用时间= 14.4029130936

====================================================== ================================================== ====

cv interface: cv界面:

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 所用时间= 1.16368889809

======================================================= ================================================== =====

See, here old cv is about 12 times faster than cv2 . 看,这里旧的cvcv2 12 times faster12 times faster And resulting images are same. 结果图像是相同的。 (input image is of size 720x540) (输入图像大小为720x540)

Why does this happen? 为什么会这样?

Is cv2 slower compared to cv? cv2与cv相比是否较慢?

Or am I making any mistake here? 或者我在这里犯了什么错误? Is there a faster method in cv2 for the above code? 对于上面的代码,cv2中有更快的方法吗?

The image returned by cv2.imread() is an array object of NumPy. cv2.imread()返回的图像是NumPy的数组对象。 So you can use NumPy's functions to speedup calculation. 因此,您可以使用NumPy的功能来加速计算。

The following program shows how to speedup your origin for loop version by using item(), itemset() method of ndarray object. 以下程序显示如何使用ndarray对象的item(),itemset()方法加速循环版本的原点。

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: 以下程序首先显示如何创建调色板,并使用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(h==h2)

The output is: 输出是:

0.453000068665
0.0309998989105
True

The cv version output is : cv版本输出是:

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 注意:轴0的长度是图像的高度,轴1的长度是图像的宽度

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

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