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檔案
1import copy
2import os,sys
3import opencv, highgui
4
5class 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
12def 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
28def 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
45top = r'D:\Python26\Lib\site-packages\opencv'
46result = []
47xywh=[1,1,10,10]
48if len(sys.argv)==5:
49 xywh = [ int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])]
50else:
51 print "Using default ROI"
52
53for 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
68htmlFile = os.path.join(top, 'result.html')
69print htmlFile
70PrintHtml(result, htmlFile)
71os.system('explorer.exe "%s"' %htmlFile)