# 14-python opencv 获取程序执行时间

• 获取系统时钟数
• 获取系统时钟频率

## 实现过程

### 初始时间

# time start
t1 = cv2.getTickCount()

### 结束时间

# time end
t2 = cv2.getTickCount()

### 计算执行秒数

t = (t2-t1)/cv2.getTickFrequency()
print t

## 源代码

# created by Huang Lu
# 2016/8/26 17:35
# Department of EE, Tsinghua Univ.

import cv2
import numpy as np

# get the hist graph of a gray image
def HistGraphGray(image, color):
hist= cv2.calcHist([image], [0], None, [256], [0.0,255.0])
histGraph = np.zeros([256,256,3], np.uint8)
m = max(hist)
hist = hist * 220 / m
for h in range(256):
n = int(hist[h])
cv2.line(histGraph,(h,255), (h,255-n), color)
return histGraph;

# get the hist graph of a color image
def HistGraphColor(image):
histGraph = np.zeros([256,256,3], np.uint8)
colorBlue = [255, 0, 0]
colorGreen = [0, 255, 0]
colorRed = [0, 0, 255]
b, g, r = cv2.split(image)
bhist = cv2.calcHist([b], [0], None, [256], [0.0,255.0])
ghist = cv2.calcHist([g], [0], None, [256], [0.0,255.0])
rhist = cv2.calcHist([r], [0], None, [256], [0.0,255.0])
bm = max(bhist)
gm = max(ghist)
rm = max(rhist)
bhist = bhist * 220 / bm
rhist = rhist * 220 / rm
ghist = ghist * 220 / gm
for h in range(256):
bn = int(bhist[h])
gn = int(ghist[h])
rn = int(rhist[h])
if h != 0:
cv2.line(histGraph,(h-1,255-bStart), (h,255-bn), colorBlue)
cv2.line(histGraph,(h-1,255-gStart), (h,255-gn), colorGreen)
cv2.line(histGraph,(h-1,255-rStart), (h,255-rn), colorRed)
bStart = bn
gStart = gn
rStart = rn
return histGraph

# main fuction
if __name__ == '__main__':
# time start
t1 = cv2.getTickCount()

# test for a gray image
color = [255, 255, 255]
histGraph1 = HistGraphGray(img1, color)
cv2.imshow("Hist Gray", histGraph1)

# test for a color image
# first tset for three channels
colorRed = [0, 0, 255]
colorGreen = [0, 255, 0]
colorBlue = [255, 0, 0]
b, g, r = cv2.split(img2)
# blue channel
bhistGraph = HistGraphGray(b, colorBlue)
cv2.imshow("Hist Blue", bhistGraph)
# green channel
ghistGraph = HistGraphGray(g, colorGreen)
cv2.imshow("Hist Green", ghistGraph)
# red channel
rhistGraph = HistGraphGray(r, colorRed)
cv2.imshow("Hist Red", rhistGraph)
# get three channels together
histGraph2 = HistGraphColor(img2)
cv2.imshow("Hist Color", histGraph2)

# time end
t2 = cv2.getTickCount()
t = (t2-t1)/cv2.getTickFrequency()
print t

cv2.waitKey(0)
cv2.destroyAllWindows()

• 本文已收录于以下专栏：

## Opencv3.0-python的那些事儿：（九）、Opencv关于如何读取大量视频帧进行处理的方法

• qingyuanluofeng
• 2016年06月09日 01:39
• 4182

## opencv 计算程序运行时间

• ture_dream
• 2017年01月10日 15:53
• 1797

## python得到电脑的开机时间

#先下载psutil库:pip install psutilimport psutil import os,datetime def main(): print "电脑的开机时...
• CosmopolitanMe
• 2017年09月07日 19:58
• 511

## python opencv 获取程序执行时间

• huanglu_thu13
• 2016年08月28日 16:31
• 715

## Python-OpenCV 杂项（三）： 程序性能的检测和优化

• qq_26898461
• 2016年01月04日 10:43
• 711

## Python如何获得本程序的执行时间

• xm1331305
• 2012年09月07日 20:32
• 777

## python记录程序运行时间的几种方法

1.获取当前时间的两种方法： 复制代码 代码如下: import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print ...
• a8039974
• 2018年01月10日 16:35
• 113

## OpenCV getTickCount统计代码运行时间

• pj_sysu
• 2015年04月26日 09:36
• 1620

## Python-OpenCV(5)

• Gavin__Zhou
• 2016年04月06日 09:43
• 1239

## 【opencv】获取程序运行时间

double t = (double)getTickCount(); // do something ...返回该处代码执行所耗的时间，单位为秒 t = ((double)getTickCount()...
• u013617144
• 2016年01月26日 12:01
• 1052

举报原因： 您举报文章：python opencv 获取程序执行时间 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)