Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 112:67555e968b5e
added tests if OpenCV python libraries are not available
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 18 Jul 2011 14:28:40 -0400 |
parents | abfc54c097d4 |
children | 2bf5b76320c0 |
comparison
equal
deleted
inserted
replaced
111:48e3de4acb65 | 112:67555e968b5e |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 '''Image/Video utilities''' | 2 '''Image/Video utilities''' |
3 | 3 |
4 import Image, ImageDraw # PIL | 4 import Image, ImageDraw # PIL |
5 import cv | 5 try: |
6 import cv | |
7 opencvExists = True | |
8 except ImportError: | |
9 print('OpenCV library could not be loaded') | |
10 opencvExists = False | |
6 from sys import stdout | 11 from sys import stdout |
7 | 12 |
8 #import aggdraw # agg on top of PIL (antialiased drawing) | 13 #import aggdraw # agg on top of PIL (antialiased drawing) |
9 #import utils | 14 #import utils |
10 | 15 |
41 for i in xrange(cvmat.rows): | 46 for i in xrange(cvmat.rows): |
42 for j in xrange(cvmat.cols): | 47 for j in xrange(cvmat.cols): |
43 a[i,j] = cvmat[i,j] | 48 a[i,j] = cvmat[i,j] |
44 return a | 49 return a |
45 | 50 |
46 def arrayToCvMat(a, t = cv.CV_64FC1): | 51 if opencvExists: |
47 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' | 52 def arrayToCvMat(a, t = cv.CV_64FC1): |
48 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) | 53 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' |
49 for i in range(cvmat.rows): | 54 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) |
50 for j in range(cvmat.cols): | 55 for i in range(cvmat.rows): |
51 cvmat[i,j] = a[i,j] | 56 for j in range(cvmat.cols): |
52 return cvmat | 57 cvmat[i,j] = a[i,j] |
58 return cvmat | |
53 | 59 |
54 def printCvMat(cvmat, out = stdout): | 60 def printCvMat(cvmat, out = stdout): |
55 '''Prints the cvmat to out''' | 61 '''Prints the cvmat to out''' |
56 for i in xrange(cvmat.rows): | 62 for i in xrange(cvmat.rows): |
57 for j in xrange(cvmat.cols): | 63 for j in xrange(cvmat.cols): |
93 from numpy.linalg.linalg import inv | 99 from numpy.linalg.linalg import inv |
94 invH = inv(homography) | 100 invH = inv(homography) |
95 invH /= invH[2,2] | 101 invH /= invH[2,2] |
96 return invH | 102 return invH |
97 | 103 |
98 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): | 104 if opencvExists: |
99 '''Computes the translation between of img2 with respect to img1 | 105 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): |
100 (loaded using OpenCV) | 106 '''Computes the translation between of img2 with respect to img1 |
101 img1Points are used to compute the translation | 107 (loaded using OpenCV) |
108 img1Points are used to compute the translation | |
102 | 109 |
103 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)''' | 110 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)''' |
104 from numpy.core.multiarray import zeros | 111 from numpy.core.multiarray import zeros |
105 from numpy.lib.function_base import median | 112 from numpy.lib.function_base import median |
106 | 113 |
107 (img2Points, status, track_error) = cv.CalcOpticalFlowPyrLK(img1, img2, zeros((img1.rows,img1.cols+8)), zeros((img1.rows,img1.cols+8)), img1Points, windowSize, level, criteria, 0) | 114 (img2Points, status, track_error) = cv.CalcOpticalFlowPyrLK(img1, img2, zeros((img1.rows,img1.cols+8)), zeros((img1.rows,img1.cols+8)), img1Points, windowSize, level, criteria, 0) |
108 | 115 |
109 deltaX = [] | 116 deltaX = [] |
110 deltaY = [] | 117 deltaY = [] |
111 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): | 118 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): |
112 if status[k] == 1: | 119 if status[k] == 1: |
113 dx = p2[0]-p1[0] | 120 dx = p2[0]-p1[0] |
114 dy = p2[1]-p1[1] | 121 dy = p2[1]-p1[1] |
115 d = dx**2 + dy**2 | 122 d = dx**2 + dy**2 |
116 if d < maxTranslation: | 123 if d < maxTranslation: |
117 deltaX.append(dx) | 124 deltaX.append(dx) |
118 deltaY.append(dy) | 125 deltaY.append(dy) |
119 if len(deltaX) >= 10: | 126 if len(deltaX) >= 10: |
120 return [median(deltaX), median(deltaY)] | 127 return [median(deltaX), median(deltaY)] |
121 else: | 128 else: |
122 return None | 129 return None |
123 | 130 |
124 class FourWayIntersection: | 131 class FourWayIntersection: |
125 '''Simple class for simple intersection outline''' | 132 '''Simple class for simple intersection outline''' |
126 def __init__(self, dimension, coordX, coordY): | 133 def __init__(self, dimension, coordX, coordY): |
127 self.dimension = dimension | 134 self.dimension = dimension |