1. 进入\OpenCV\interfaces\swig\python目录,执行
python setup-for-win.py install
进行安装
2.安装成功后,会发现在Python26\Lib\site-packages下多了opencv这一目录,里面应该有这些文件
adaptors.py
adaptors.pyc
cn.bmp
cn.bmp.jpg
cv.py
cv.pyc
highgui.py
highgui.pyc
kalman.py
kalman.pyc
matlab_syntax.py
matlab_syntax.pyc
_cv.pyd
_highgui.pyd
__init__.py
__init__.pyc
其中最重要的是红色的文件
3.写一个测试程序
这个程序用来做autolighting的repeatibility测试。主要原理是:遍历目录下所有的bmp图像,对这些图像的固定一ROI内计算average和SD,并将统计结果生成html文件
1
import copy
2
import os,sys
3
import opencv, highgui
4
5
class ROI:
6
def __init__(self, x,y,w,h):
7
self.x = x
8
self.y = y
9
self.w = w
10
self.h = h
11
12
def ComputeSDAndAvg(fileName, x,y,w,h):
13
img = opencv.highgui.cvLoadImage(fileName, 0)
14
sumImg = opencv.cv.cvGetSubRect(img, opencv.cv.cvRect(x,y,w,h) )
15
avg = opencv.cv.cvAvg(sumImg)
16
mean = opencv.cv.cvScalar(0)
17
std_dev = opencv.cv.cvScalar(0)
18
opencv.cv.cvAvgSdv(sumImg, mean, std_dev)
19
20
#Modify image
21
img = opencv.highgui.cvLoadImage(fileName, 1)
22
opencv.cv.cvRectangle(img, opencv.cv.cvPoint(x,y),opencv.cv.cvPoint(x+w,y+h),opencv.cv.cvScalar(0,0,255))
23
fileNameOut = fileName + '.jpg'
24
opencv.highgui.cvSaveImage(fileNameOut, img)
25
#return [fileNameOut, avg[0] ]
26
return [fileNameOut, avg[0] , mean[0], std_dev[0]] # Note: avg == mean
27
28
def PrintHtml(result,fileName):
29
fp = open(fileName, "w")
30
31
fp.write( '<font color=red>===Image compare tool report===</font><br>\n' )
32
#compute Min-Max avg
33
a=copy.deepcopy(result)
34
a.sort(lambda x,y: int(x[1]-y[1]))
35
fp.write( 'avg MIN %f MAX %f DIFF %f <br>\n' %(a[0][1], a[-1][1], -a[0][1] + a[-1][1]) )
36
37
#print table
38
fp.write( '<table border=1>\n' )
39
fp.write( '<tr><td>Image</td><td>Avg</td><td>SD</td></tr>\n' )
40
for res in result:
41
fp.write( '<tr><td> <img width=400 height=400 src=%s > </td><td>%f,%f</td><td>%f</td></tr>\n' %(res[0], res[1], res[2], res[3]) )
42
fp.write( '</table>\n' )
43
fp.close()
44
45
top = r'D:\Python26\Lib\site-packages\opencv'
46
result = []
47
xywh=[1,1,10,10]
48
if len(sys.argv)==5:
49
xywh = [ int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])]
50
else:
51
print "Using default ROI"
52
53
for root, dirs, files in os.walk(top, topdown=True):
54
55
for name in files:
56
if name.lower().endswith('.bmp'):
57
fullPath = os.path.join(root, name)
58
if len(fullPath)>20:
59
print fullPath[:10] + '
' +fullPath[-10:]
60
else:
61
print fullPath
62
[fileNameOut, avg, mean, std_dev ]= ComputeSDAndAvg(fullPath, xywh[0], xywh[1], xywh[2], xywh[3])
63
result.append([fileNameOut, avg, mean, std_dev ])
64
65
for name in dirs:
66
#os.rmdir(os.path.join(root, name))
67
pass
68
htmlFile = os.path.join(top, 'result.html')
69
print htmlFile
70
PrintHtml(result, htmlFile)
71
os.system('explorer.exe "%s"' %htmlFile)

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59


60

61

62

63

64

65

66

67

68

69

70

71
