Mercurial Hosting > traffic-intelligence
diff python/cvutils.py @ 672:5473b7460375
moved and rationalized imports in modules
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 26 May 2015 13:53:40 +0200 |
parents | 15e244d2a1b5 |
children | 5505f9dbb28e |
line wrap: on
line diff
--- a/python/cvutils.py Tue May 26 11:40:32 2015 +0200 +++ b/python/cvutils.py Tue May 26 13:53:40 2015 +0200 @@ -17,6 +17,7 @@ skimageAvailable = False from sys import stdout +from numpy import dot, array, append #import aggdraw # agg on top of PIL (antialiased drawing) @@ -67,15 +68,14 @@ def loadPointCorrespondences(filename): '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)''' - from numpy.lib.npyio import loadtxt - from numpy import float32 + from numpy import loadtxt, float32 points = loadtxt(filename, dtype=float32) return (points[:2,:].T, points[2:,:].T) # (world points, image points) def cvMatToArray(cvmat): '''Converts an OpenCV CvMat to numpy array.''' print('Deprecated, use new interface') - from numpy.core.multiarray import zeros + from numpy import zeros a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) for i in xrange(cvmat.rows): for j in xrange(cvmat.cols): @@ -116,7 +116,7 @@ def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): from copy import deepcopy - from numpy import identity, array + from numpy import identity newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) newCameraMatrix = deepcopy(intrinsicCameraMatrix) newCameraMatrix[0,2] = newImgSize[0]/2. @@ -325,7 +325,6 @@ def computeHomographyFromPDTV(camera): '''Returns the homography matrix at ground level from PDTV camera https://bitbucket.org/hakanardo/pdtv''' - from numpy import array # camera = pdtv.load(cameraFilename) srcPoints = [[x,y] for x, y in zip([1.,2.,2.,1.],[1.,1.,2.,2.])] # need floats!! dstPoints = [] @@ -340,7 +339,7 @@ map1 and map2 are the mapping functions from undistorted image to distorted (original image) map1(x,y) = originalx, originaly''' - from numpy import abs, logical_and, unravel_index, dot, sum + from numpy import abs, logical_and, unravel_index, sum from matplotlib.mlab import find distx = abs(map1-x) disty = abs(map2-y) @@ -459,10 +458,6 @@ def projectArray(homography, points): '''Returns the coordinates of the projected points through homography (format: array 2xN points)''' - from numpy.core import dot - from numpy.core.multiarray import array - from numpy.lib.function_base import append - if points.shape[0] != 2: raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) @@ -477,20 +472,18 @@ def project(homography, p): '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] through homography''' - from numpy import array return projectArray(homography, array([[p[0]],[p[1]]])) def projectTrajectory(homography, trajectory): '''Projects a series of points in the format [[x1, x2, ...], [y1, y2, ...]]''' - from numpy.core.multiarray import array return projectArray(homography, array(trajectory)) def invertHomography(homography): '''Returns an inverted homography Unnecessary for reprojection over camera image''' - from numpy.linalg.linalg import inv + from numpy import inv invH = inv(homography) invH /= invH[2,2] return invH @@ -517,7 +510,6 @@ return undistortedTrajectory def projectGInputPoints(homography, points): - from numpy import array return projectTrajectory(homography, array(points+[points[0]]).T) if opencvAvailable: @@ -527,9 +519,7 @@ img1Points are used to compute the translation TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom, other non linear distortion)''' - from numpy.core.multiarray import array - from numpy.lib.function_base import median - from numpy.core.fromnumeric import sum + from numpy import median, sum nextPoints = array([]) (img2Points, status, track_error) = cv2.calcOpticalFlowPyrLK(img1, img2, img1Points, nextPoints, winSize=windowSize, maxLevel=level, criteria=criteria) @@ -568,7 +558,7 @@ def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): from os import listdir - from numpy import array, float32 + from numpy import float32 from matplotlib.pyplot import imread inputData = []