久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

OpenCV-Python接口、cv和cv2的性能比較

Performance comparison of OpenCV-Python interfaces, cv and cv2(OpenCV-Python接口、cv和cv2的性能比較)
本文介紹了OpenCV-Python接口、cv和cv2的性能比較的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

幾天前,我開始使用新的 OpenCV-Python 接口,cv2.

A few days back, I started using new OpenCV-Python interface, cv2.

我的問題是關(guān)于 cvcv2 接口的比較.

My question is regarding the comparison of cv and cv2 interface.

在易用性方面,新的 cv2 界面有了很大的改進(jìn),使用 cv2 真的很簡單有趣.

Regarding the ease of use, new cv2 interface has improved far greater, and it is really easy and fun to work with cv2.

但是速度呢?

我制作了兩個(gè)小代碼片段,一個(gè)在 cv 中,另一個(gè)在 cv2 中,以檢查性能.兩者都執(zhí)行相同的功能,訪問圖像的像素,對其進(jìn)行測試,進(jìn)行一些修改等.

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.

下面是代碼:

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

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

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

結(jié)果是:

所用時(shí)間 = 14.4029130936

time taken = 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)

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

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

結(jié)果是:

所用時(shí)間 = 1.16368889809

time taken = 1.16368889809

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

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

看,這里舊的 cvcv212 倍.結(jié)果圖像是相同的.(輸入圖像大小為 720x540)

See, here old cv is about 12 times faster than cv2. And resulting images are same. (input image is of size 720x540)

為什么會這樣?

cv2 比 cv 慢嗎?

Is cv2 slower compared to cv?

或者我在這里犯了什么錯(cuò)誤?以上代碼在cv2中有更快的方法嗎?

Or am I making any mistake here? Is there a faster method in cv2 for the above code?

推薦答案

cv2.imread()返回的圖片是NumPy的數(shù)組對象.所以你可以使用 NumPy 的函數(shù)來加速計(jì)算.

The image returned by cv2.imread() is an array object of NumPy. So you can use NumPy's functions to speedup calculation.

下面的程序展示了如何使用 ndarray 對象的 item(), itemset() 方法來加速你的 origin for 循環(huán)版本.

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

下面的程序展示了如何首先創(chuàng)建調(diào)色板,并使用 NumPy 的數(shù)組索引來獲取結(jié)果:

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)

輸出是:

0.453000068665
0.0309998989105
True

cv 版本輸出為:

0.468999862671

注意:0軸的長度是圖片的高度,1軸的長度是圖片的寬度

這篇關(guān)于OpenCV-Python接口、cv和cv2的性能比較的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久国产亚洲 | 国产精品欧美一区二区三区不卡 | 免费亚洲成人 | 日韩一二三区视频 | 人成精品| 综合第一页 | 国产视频一二三区 | 欧美一区二区三区在线免费观看 | 精品久久久久一区二区国产 | 不卡一区二区三区四区 | 国产免费又黄又爽又刺激蜜月al | 精品久久久久久亚洲精品 | 91欧美精品 | 国产欧美精品区一区二区三区 | 国产一区二区三区久久久久久久久 | 无码日韩精品一区二区免费 | 欧美一级黑人aaaaaaa做受 | 亚洲人在线| 精精国产xxxx视频在线播放 | 人人澡人人爱 | 中文字幕视频在线观看 | 欧美性生活视频 | 97国产爽爽爽久久久 | 性做久久久久久免费观看欧美 | 黄色成人在线 | 色婷婷av一区二区三区软件 | 欧美国产视频一区二区 | 久操伊人 | 日韩欧美天堂 | 国产精品久久久久久久午夜 | 国产精品99久久久久 | 国产伦精品一区二区三区照片91 | 欧美一级淫片免费视频黄 | 91综合网 | 亚洲精品一区二区三区中文字幕 | 久久婷婷色 | 国产性色视频 | 亚洲黄色av | 亚洲 91| 国产精品久久久久久久白浊 | eeuss国产一区二区三区四区 |