您的位置:首页 > 运维架构

opencv-性能测量与改进技术

2017-10-20 17:17 232 查看
参考:

1、http://docs.opencv.org/3.3.0/  官方文档api

2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程

3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程

5、https://docs.opencv.org/3.3.0/index.html 
官方英文教程

6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials

7、https://www.learnopencv.com/

8、http://answers.opencv.org/questions/ OpenCV论坛

注:安装的版本
opencv_python-3.3.0-cp36-cp36m-win_amd64.whl

参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

性能测量与改进技术


目标

To measure the performance of your code.
Some tips to improve the performance of your code.
You will see these functions : cv2.getTickCountcv2.getTickFrequency etc.

Python also provides a module time which is helpful in measuring the time of execution.

Another module profile helps to get detailed report on the code,


使用OpenCV测量性能

e1 = cv2.getTickCount()
# your code execution
e2 = cv2.getTickCount()
time = (e2 - e1)/ cv2.getTickFrequency()


import cv2
from datetime import datetime
import time

# Load two images
img1 = cv2.imread('messi5.jpg')

e1 = cv2.getTickCount()
start_time=datetime.now()
start_time2=time.time()

for i in range(5,49,2):
img1 = cv2.medianBlur(img1,i)
e2 = cv2.getTickCount()
t = (e2 - e1)/cv2.getTickFrequency()
print(t)
print((datetime.now()-start_time).total_seconds())
print(time.time()-start_time2)

# Result I got is 0.521107655 seconds
# Load two imagesimg1 = cv2.imread('messi5.jpg')e1 = cv2.getTickCount()start_time=datetime.now()for i in range(5,49,2): img1 = cv2.medianBlur(img1,i)e2 = cv2.getTickCount()t = (e2 - e1)/cv2.getTickFrequency()print(t)print((datetime.now()-start_time).total_seconds())# Result I got is 0.521107655 seconds

OpenCV中的默认优化

# check if optimization is enabled
In [5]: cv2.useOptimized()
Out[5]: True

In [6]: %timeit res = cv2.medianBlur(img,49)
10 loops, best of 3: 34.9 ms per loop

# Disable it
In [7]: cv2.setUseOptimized(False)

In [8]: cv2.useOptimized()
Out[8]: False

In [9]: %timeit res = cv2.medianBlur(img,49)
10 loops, best of 3: 64.1 ms per loop


测量IPython中的性能

In [10]: x = 5

In [11]: %timeit y=x**2
10000000 loops, best of 3: 73 ns per loop

In [12]: %timeit y=x*x
10000000 loops, best of 3: 58.3 ns per loop

In [15]: z = np.uint8([5])

In [17]: %timeit y=z*z
1000000 loops, best of 3: 1.25 us per loop

In [19]: %timeit y=np.square(z)
1000000 loops, best of 3: 1.16 us per loop


In [35]: %timeit z = cv2.countNonZero(img)
100000 loops, best of 3: 15.8 us per loop

In [36]: %timeit z = np.count_nonzero(img)
1000 loops, best of 3: 370 us per loop

Normally, OpenCV functions are faster
than Numpy functions. So for same operation, OpenCV functions are preferred. But, there can be exceptions, especially when Numpy works with views instead of copies.

Additional Resources

Python Optimization Techniques
Scipy Lecture Notes - Advanced
Numpy
Timing and Profiling in IPython
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: