Mercurial Hosting > traffic-intelligence
changeset 152:74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Sep 2011 18:44:23 -0400 |
parents | 4af774bb186d |
children | c8a149fccfda |
files | python/cvutils.py python/moving.py python/tests/moving.txt python/tests/utils.txt python/utils.py |
diffstat | 5 files changed, 42 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Tue Sep 06 17:55:06 2011 -0400 +++ b/python/cvutils.py Tue Sep 06 18:44:23 2011 -0400 @@ -58,9 +58,9 @@ return a if opencvExists: - def arrayToCvMat(a, t = cv.CV_64FC1): + def arrayToCvMat(a, t = cv2.cv.CV_64FC1): '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' - cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) + cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t) for i in range(cvmat.rows): for j in range(cvmat.cols): cvmat[i,j] = a[i,j] @@ -135,7 +135,7 @@ return invH if opencvExists: - def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): + def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)): '''Computes the translation between of img2 with respect to img1 (loaded using OpenCV) img1Points are used to compute the translation
--- a/python/moving.py Tue Sep 06 17:55:06 2011 -0400 +++ b/python/moving.py Tue Sep 06 18:44:23 2011 -0400 @@ -199,6 +199,31 @@ from matplotlib.pyplot import scatter scatter([p.x for p in points],[p.y for p in points], c=color) +def segmentIntersection(p1, p2, p3, p4): + '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' + from numpy import matrix + from numpy.linalg import linalg, det + + dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] + dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] + + A = matrix([[dp1.y, -dp1.x], + [dp2.y, -dp2.x]]) + B = matrix([[dp1.y*p1.x-dp1.x*p1.y], + [dp2.y*p3.x-dp2.x*p3.y]]) + + if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: + return None + else: + intersection = linalg.solve(A,B) + if (utils.inBetween(p1.x, p2.x, intersection[0,0]) + and utils.inBetween(p3.x, p4.x, intersection[0,0]) + and utils.inBetween(p1.y, p2.y, intersection[1,0]) + and utils.inBetween(p3.y, p4.y, intersection[1,0])): + return Point(intersection[0,0], intersection[1,0]) + else: + return None + class Trajectory: '''Class for trajectories i.e. a temporal sequence of positions
--- a/python/tests/moving.txt Tue Sep 06 17:55:06 2011 -0400 +++ b/python/tests/moving.txt Tue Sep 06 18:44:23 2011 -0400 @@ -43,6 +43,12 @@ >>> Point(3,2).inPolygon([Point(0,0),Point(4,0),Point(4,3),Point(0,3)]) True +>>> segmentIntersection(Point(0,0),Point(1,1), Point(0,1), Point(1,2)) +>>> segmentIntersection(Point(0,1),Point(1,0), Point(0,2), Point(2,1)) +>>> segmentIntersection(Point(0,0),Point(2,0), Point(1,-1),Point(1,1)) +(1.000000,0.000000) +>>> segmentIntersection(Point(0,1),Point(2,0),Point(1,1),Point(1,2)) + >>> Trajectory().length() 0 >>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]])
--- a/python/tests/utils.txt Tue Sep 06 17:55:06 2011 -0400 +++ b/python/tests/utils.txt Tue Sep 06 18:44:23 2011 -0400 @@ -13,11 +13,12 @@ >>> ceilDecimals(1.23, 1) 1.3 ->>> segmentIntersection(Point(0,0),Point(1,1), Point(0,1), Point(1,2)) ->>> segmentIntersection(Point(0,1),Point(1,0), Point(0,2), Point(2,1)) ->>> segmentIntersection(Point(0,0),Point(2,0), Point(1,-1),Point(1,1)) -(1.000000,0.000000) ->>> segmentIntersection(Point(0,1),Point(2,0),Point(1,1),Point(1,2)) +>>> inBetween(1,2,1.5) +True +>>> inBetween(2.1,1,1.5) +True +>>> inBetween(1,2,0) +False >>> f = openCheck('non_existant_file.txt') File non_existant_file.txt could not be opened.
--- a/python/utils.py Tue Sep 06 17:55:06 2011 -0400 +++ b/python/utils.py Tue Sep 06 18:44:23 2011 -0400 @@ -4,8 +4,6 @@ #from numpy import * #from pylab import * -import moving - __metaclass__ = type commentChar = '#' @@ -119,30 +117,8 @@ tens = pow(10,nDecimals) return ceil(v*tens)/tens -def segmentIntersection(p1, p2, p3, p4): - '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' - from numpy import matrix - from numpy.linalg import linalg, det - - dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] - dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] - - A = matrix([[dp1.y, -dp1.x], - [dp2.y, -dp2.x]]) - B = matrix([[dp1.y*p1.x-dp1.x*p1.y], - [dp2.y*p3.x-dp2.x*p3.y]]) - - if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: - return None - else: - intersection = linalg.solve(A,B) - if (moving.Interval(p1.x, p2.x, True).contains(intersection[0,0]) - and moving.Interval(p3.x, p4.x, True).contains(intersection[0,0]) - and moving.Interval(p1.y, p2.y, True).contains(intersection[1,0]) - and moving.Interval(p3.y, p4.y, True).contains(intersection[1,0])): - return moving.Point(intersection[0,0], intersection[1,0]) - else: - return None +def inBetween(bound1, bound2, x): + return bound1 <= x <= bound2 or bound2 <= x<= bound1 def crossProduct(l1, l2): return l1[0]*l2[1]-l1[1]*l2[0]