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使用openCV + pythonimport copy
 2使用openCV + pythonimport os,sys
 3使用openCV + pythonimport opencv, highgui
 4使用openCV + python
 5使用openCV + pythonclass    ROI:
 6使用openCV + python    def __init__(self, x,y,w,h):
 7使用openCV + python        self.x = x
 8使用openCV + python        self.y = y
 9使用openCV + python        self.w = w
10使用openCV + python        self.h = h
11使用openCV + python        
12使用openCV + pythondef    ComputeSDAndAvg(fileName, x,y,w,h):
13使用openCV + python    img = opencv.highgui.cvLoadImage(fileName, 0)
14使用openCV + python    sumImg = opencv.cv.cvGetSubRect(img, opencv.cv.cvRect(x,y,w,h) )
15使用openCV + python    avg = opencv.cv.cvAvg(sumImg)
16使用openCV + python    mean = opencv.cv.cvScalar(0) 
17使用openCV + python    std_dev = opencv.cv.cvScalar(0) 
18使用openCV + python    opencv.cv.cvAvgSdv(sumImg, mean, std_dev)
19使用openCV + python    
20使用openCV + python    #Modify image
21使用openCV + python    img = opencv.highgui.cvLoadImage(fileName, 1)
22使用openCV + python    opencv.cv.cvRectangle(img, opencv.cv.cvPoint(x,y),opencv.cv.cvPoint(x+w,y+h),opencv.cv.cvScalar(0,0,255))
23使用openCV + python    fileNameOut = fileName + '.jpg'
24使用openCV + python    opencv.highgui.cvSaveImage(fileNameOut, img)
25使用openCV + python    #return [fileNameOut, avg[0] ]
26使用openCV + python    return [fileNameOut, avg[0] , mean[0], std_dev[0]]    # Note: avg == mean
27使用openCV + python
28使用openCV + pythondef    PrintHtml(result,fileName):
29使用openCV + python    fp = open(fileName, "w")
30使用openCV + python    
31使用openCV + python    fp.write( '<font color=red>===Image compare tool report===</font><br>\n' )
32使用openCV + python    #compute Min-Max avg
33使用openCV + python    a=copy.deepcopy(result)
34使用openCV + python    a.sort(lambda x,y: int(x[1]-y[1]))
35使用openCV + python    fp.write( 'avg MIN %f MAX %f DIFF %f <br>\n' %(a[0][1], a[-1][1], -a[0][1+ a[-1][1]) )
36使用openCV + python    
37使用openCV + python    #print table
38使用openCV + python    fp.write( '<table border=1>\n' )
39使用openCV + python    fp.write( '<tr><td>Image</td><td>Avg</td><td>SD</td></tr>\n' )
40使用openCV + python    for res in result:
41使用openCV + python        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使用openCV + python    fp.write( '</table>\n' )
43使用openCV + python    fp.close()
44使用openCV + python    
45使用openCV + pythontop = r'D:\Python26\Lib\site-packages\opencv'
46使用openCV + pythonresult = []
47使用openCV + pythonxywh=[1,1,10,10]
48使用openCV + pythonif len(sys.argv)==5:
49使用openCV + python    xywh = [ int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])]
50使用openCV + pythonelse:
51使用openCV + python    print "Using default ROI"
52使用openCV + python
53使用openCV + pythonfor root, dirs, files in os.walk(top, topdown=True):    
54使用openCV + python
55使用openCV + python    for name in files:    
56使用openCV + python        if name.lower().endswith('.bmp'):
57使用openCV + python            fullPath = os.path.join(root, name)
58使用openCV + python            if len(fullPath)>20:
59使用openCV + python                print fullPath[:10+ '使用openCV + python' +fullPath[-10:]
60使用openCV + python            else:
61使用openCV + python                print fullPath
62使用openCV + python            [fileNameOut, avg, mean, std_dev ]= ComputeSDAndAvg(fullPath, xywh[0], xywh[1], xywh[2], xywh[3])
63使用openCV + python            result.append([fileNameOut, avg, mean, std_dev ])
64使用openCV + python        
65使用openCV + python    for name in dirs:    
66使用openCV + python        #os.rmdir(os.path.join(root, name))
67使用openCV + python        pass
68使用openCV + pythonhtmlFile = os.path.join(top, 'result.html')        
69使用openCV + pythonprint htmlFile
70使用openCV + pythonPrintHtml(result, htmlFile)
71使用openCV + pythonos.system('explorer.exe "%s"' %htmlFile)

 


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-10-20
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
猜你喜欢
  • 2021-12-25
  • 2021-11-29
  • 2022-02-03
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案