Use opencv + Python

Source: Internet
Author: User

1. Go to the \ opencv \ Interfaces \ swig \ Python directory and run

Install Python setup-for-win.py

Install

2. After the installation is successful, you will find that the directory opencv is added under python26 \ Lib \ Site-packages, which should contain these files

Adaptors. py
Adaptors. PyC
Cn.bmp
Cn.bmp .jpg
Cv. py
Cv. PyC
Highgui. py
Highgui. PyC
Kalman. py
Kalman. PyC
Maid
MATLAB _ syntax.pyc
_ Cv. PYD
_ Highgui. PYD
_ Init _. py

_ Init _. PyC

The most important is the red file.

 

3. Write a test program

This program is used to test the repeatibility of autolighting. The main principle is to traverse all BMP images in the directory, calculate average and SD within a fixed ROI of these images, and generate an HTML file based on the statistical results.

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
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> </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'
46 result = []
47 xywh = [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])]
50 else:
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
68 htmlfile = OS. Path. Join (top, 'result.html ')
69 print htmlfile
70 printhtml (result, htmlfile)
71os.system('assumer.exe "% s" '% htmlfile)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.