Mercurial > hg > nsaunier > traffic-intelligence
annotate python/cvutils.py @ 682:fbe29be25501 dev
corrected bug
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 05 Jun 2015 02:35:29 +0200 |
| parents | da1352b89d02 |
| children | 94b291a5f933 |
| rev | line source |
|---|---|
| 28 | 1 #! /usr/bin/env python |
| 2 '''Image/Video utilities''' | |
| 3 | |
|
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
4 import utils |
|
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
5 |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
6 try: |
|
151
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
7 import cv2 |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
8 opencvAvailable = True |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
9 except ImportError: |
|
501
c81cbd6953fb
update to classify speed to remove data at both ends
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
10 print('OpenCV library could not be loaded (video replay functions will not be available)') # TODO change to logging module |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
11 opencvAvailable = False |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
12 try: |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
13 import skimage |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
14 skimageAvailable = True |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
15 except ImportError: |
|
410
91954c76d12c
minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
406
diff
changeset
|
16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
17 skimageAvailable = False |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
18 |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
19 from sys import stdout |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
20 from numpy import dot, array, append, float32 |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
21 |
|
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
22 #import aggdraw # agg on top of PIL (antialiased drawing) |
|
151
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
23 |
| 28 | 24 |
|
151
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
25 cvRed = (0,0,255) |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
26 cvGreen = (0,255,0) |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
27 cvBlue = (255,0,0) |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
28 cvColors = utils.PlottingPropertyValues([cvRed, |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
29 cvGreen, |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
30 cvBlue]) |
|
4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
150
diff
changeset
|
31 |
|
305
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
32 def quitKey(key): |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
33 return chr(key&255)== 'q' or chr(key&255) == 'Q' |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
34 |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
35 def saveKey(key): |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
36 return chr(key&255) == 's' |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
37 |
|
657
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
38 def int2FOURCC(x): |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
39 fourcc = '' |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
40 for i in xrange(4): |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
41 fourcc += unichr((x >> 8*i)&255) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
42 return fourcc |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
43 |
|
515
727e3c529519
renamed all draw functions to plot for consistency
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
511
diff
changeset
|
44 def plotLines(filename, origins, destinations, w = 1, resultFilename='image.png'): |
| 28 | 45 '''Draws lines over the image ''' |
|
410
91954c76d12c
minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
406
diff
changeset
|
46 import Image, ImageDraw # PIL |
| 28 | 47 |
| 48 img = Image.open(filename) | |
| 49 | |
| 50 draw = ImageDraw.Draw(img) | |
| 51 #draw = aggdraw.Draw(img) | |
| 52 #pen = aggdraw.Pen("red", width) | |
| 53 for p1, p2 in zip(origins, destinations): | |
| 54 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0)) | |
| 55 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) | |
|
515
727e3c529519
renamed all draw functions to plot for consistency
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
511
diff
changeset
|
56 del draw |
| 28 | 57 |
| 58 #out = utils.openCheck(resultFilename) | |
| 59 img.save(resultFilename) | |
| 60 | |
|
159
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
61 def matlab2PointCorrespondences(filename): |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
62 '''Loads and converts the point correspondences saved |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
63 by the matlab camera calibration tool''' |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
64 from numpy.lib.io import loadtxt, savetxt |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
65 from numpy.lib.function_base import append |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
66 points = loadtxt(filename, delimiter=',') |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
67 savetxt(utils.removeExtension(filename)+'-point-correspondences.txt',append(points[:,:2].T, points[:,3:].T, axis=0)) |
|
115f7f90286d
updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
156
diff
changeset
|
68 |
|
160
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
159
diff
changeset
|
69 def loadPointCorrespondences(filename): |
|
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
159
diff
changeset
|
70 '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)''' |
|
672
5473b7460375
moved and rationalized imports in modules
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
71 from numpy import loadtxt, float32 |
|
160
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
159
diff
changeset
|
72 points = loadtxt(filename, dtype=float32) |
|
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
159
diff
changeset
|
73 return (points[:2,:].T, points[2:,:].T) # (world points, image points) |
|
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
159
diff
changeset
|
74 |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
75 def cvMatToArray(cvmat): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
76 '''Converts an OpenCV CvMat to numpy array.''' |
|
384
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
77 print('Deprecated, use new interface') |
|
672
5473b7460375
moved and rationalized imports in modules
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
78 from numpy import zeros |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
79 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
80 for i in xrange(cvmat.rows): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
81 for j in xrange(cvmat.cols): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
82 a[i,j] = cvmat[i,j] |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
83 return a |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
84 |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
85 if opencvAvailable: |
|
402
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
86 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=3.0): |
|
302
9d88a4d97473
corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
266
diff
changeset
|
87 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
|
9d88a4d97473
corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
266
diff
changeset
|
88 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) |
|
9d88a4d97473
corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
266
diff
changeset
|
89 return H |
|
9d88a4d97473
corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
266
diff
changeset
|
90 |
|
406
37c7b46f6e21
update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
402
diff
changeset
|
91 def arrayToCvMat(a, t = cv2.CV_64FC1): |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
92 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' |
|
384
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
93 print('Deprecated, use new interface') |
|
152
74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
151
diff
changeset
|
94 cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t) |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
95 for i in range(cvmat.rows): |
|
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
96 for j in range(cvmat.cols): |
|
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
97 cvmat[i,j] = a[i,j] |
|
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
98 return cvmat |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
99 |
|
658
6668f541b915
Added **kwargs passthrough to cvPlot()->cv2.line() for greater drawing flexibility.
pstaub
parents:
657
diff
changeset
|
100 def cvPlot(img, positions, color, lastCoordinate = None, **kwargs): |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
101 last = lastCoordinate+1 |
|
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
102 if lastCoordinate is not None and lastCoordinate >=0: |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
103 last = min(positions.length()-1, lastCoordinate) |
|
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
104 for i in range(0, last-1): |
|
658
6668f541b915
Added **kwargs passthrough to cvPlot()->cv2.line() for greater drawing flexibility.
pstaub
parents:
657
diff
changeset
|
105 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color, **kwargs) |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
106 |
|
385
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
107 def cvImshow(windowName, img, rescale = 1.0): |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
108 'Rescales the image (in particular if too large)' |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
109 from cv2 import resize |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
110 if rescale != 1.: |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
111 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
112 resizedImg = resize(img, size) |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
113 cv2.imshow(windowName, resizedImg) |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
114 else: |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
115 cv2.imshow(windowName, img) |
|
1917db662aa7
added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
384
diff
changeset
|
116 |
|
510
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
117 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
118 from copy import deepcopy |
|
672
5473b7460375
moved and rationalized imports in modules
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
119 from numpy import identity |
|
510
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
120 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
121 newCameraMatrix = deepcopy(intrinsicCameraMatrix) |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
122 newCameraMatrix[0,2] = newImgSize[0]/2. |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
123 newCameraMatrix[1,2] = newImgSize[1]/2. |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
124 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) |
|
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
125 |
|
657
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
126 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): |
|
150
404f3cade05f
added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
149
diff
changeset
|
127 '''Plays the video''' |
|
544
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
128 windowName = 'frame' |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
129 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
|
305
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
130 wait = 5 |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
131 if frameRate > 0: |
|
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
132 wait = int(round(1000./frameRate)) |
|
381
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
133 if interactive: |
|
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
134 wait = 0 |
|
149
0f552c8b1650
added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
135 capture = cv2.VideoCapture(filename) |
|
0f552c8b1650
added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
136 if capture.isOpened(): |
|
0f552c8b1650
added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
137 key = -1 |
|
227
b7612c6d5702
cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
226
diff
changeset
|
138 ret = True |
|
b7612c6d5702
cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
226
diff
changeset
|
139 frameNum = firstFrameNum |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
140 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
|
305
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
141 while ret and not quitKey(key): |
|
657
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
142 #ret, img = capture.read() |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
143 for i in xrange(step): |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
144 ret, img = capture.read() |
|
149
0f552c8b1650
added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
145 if ret: |
|
381
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
146 if printFrames: |
|
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
147 print('frame {0}'.format(frameNum)) |
|
657
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
148 frameNum+=step |
|
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
149 if text is not None: |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
150 cv2.putText(img, text, (10,50), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
|
544
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
151 cvImshow(windowName, img, rescale) |
|
305
ca9131968bce
added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
303
diff
changeset
|
152 key = cv2.waitKey(wait) |
|
625
9202628a4130
saving image when playing video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
549
diff
changeset
|
153 if saveKey(key): |
|
9202628a4130
saving image when playing video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
549
diff
changeset
|
154 cv2.imwrite('image-{}.png'.format(frameNum), img) |
|
346
5f75d6c23ed5
added opencv function to destroy OpenCV windows (seems to work only on Windows)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
319
diff
changeset
|
155 cv2.destroyAllWindows() |
|
435
17185fe77316
added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
429
diff
changeset
|
156 else: |
|
538
bd1ad468e928
corrected bug and added capability to save undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
528
diff
changeset
|
157 print('Video capture for {} failed'.format(filename)) |
|
149
0f552c8b1650
added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
158 |
|
657
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
159 def infoVideo(filename): |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
160 '''Provides all available info on video ''' |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
161 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
162 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
163 cv2.cv.CV_CAP_PROP_FPS: "fps", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
164 cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
165 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
166 cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
167 cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification", |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
168 cv2.cv.CV_CAP_PROP_SATURATION: "saturation"} |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
169 capture = cv2.VideoCapture(filename) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
170 if capture.isOpened(): |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
171 for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
172 #cv2.cv.CV_CAP_PROP_CONTRAST |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
173 #cv2.cv.CV_CAP_PROP_CONVERT_RGB |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
174 #cv2.cv.CV_CAP_PROP_EXPOSURE |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
175 cv2.cv.CV_CAP_PROP_FORMAT, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
176 cv2.cv.CV_CAP_PROP_FOURCC, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
177 cv2.cv.CV_CAP_PROP_FPS, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
178 cv2.cv.CV_CAP_PROP_FRAME_COUNT, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
179 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
180 cv2.cv.CV_CAP_PROP_FRAME_WIDTH, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
181 #cv2.cv.CV_CAP_PROP_GAIN, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
182 #cv2.cv.CV_CAP_PROP_HUE |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
183 #cv2.cv.CV_CAP_PROP_MODE |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
184 #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
185 #cv2.cv.CV_CAP_PROP_POS_FRAMES |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
186 #cv2.cv.CV_CAP_PROP_POS_MSEC |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
187 #cv2.cv.CV_CAP_PROP_RECTIFICATION, |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
188 #cv2.cv.CV_CAP_PROP_SATURATION |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
189 ]: |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
190 prop = capture.get(cvprop) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
191 if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0: |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
192 prop = int2FOURCC(int(prop)) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
193 print('Video {}: {}'.format(cvPropertyNames[cvprop], prop)) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
194 else: |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
195 print('Video capture for {} failed'.format(filename)) |
|
51269511229b
added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
639
diff
changeset
|
196 |
|
396
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
197 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'): |
|
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
198 '''Returns nFrames images from the video sequence''' |
|
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
199 from math import floor, log10 |
|
150
404f3cade05f
added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
149
diff
changeset
|
200 images = [] |
|
396
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
201 capture = cv2.VideoCapture(videoFilename) |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
202 if capture.isOpened(): |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
203 rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) |
|
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
204 if rawCount < 0: |
|
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
205 rawCount = firstFrameNum+nFrames+1 |
|
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
206 nDigits = int(floor(log10(rawCount)))+1 |
|
150
404f3cade05f
added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
149
diff
changeset
|
207 ret = False |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
208 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
|
396
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
209 imgNum = 0 |
|
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
210 while imgNum<nFrames: |
|
171
8e7b354666ec
corrected bug in to get images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
160
diff
changeset
|
211 ret, img = capture.read() |
|
216
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
212 i = 0 |
|
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
213 while not ret and i<10: |
|
150
404f3cade05f
added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
149
diff
changeset
|
214 ret, img = capture.read() |
|
216
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
215 i += 1 |
|
171
8e7b354666ec
corrected bug in to get images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
160
diff
changeset
|
216 if img.size>0: |
|
216
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
217 if saveImage: |
|
396
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
218 imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits)) |
|
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
219 cv2.imwrite(outputPrefix+imgNumStr+'.png', img) |
|
216
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
220 else: |
|
51acf43e421a
modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
204
diff
changeset
|
221 images.append(img) |
|
396
167f6ec44ec5
cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
222 imgNum +=1 |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
223 capture.release() |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
224 else: |
|
435
17185fe77316
added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
429
diff
changeset
|
225 print('Video capture for {} failed'.format(videoFilename)) |
|
150
404f3cade05f
added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
149
diff
changeset
|
226 return images |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
227 |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
228 def getFPS(videoFilename): |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
229 capture = cv2.VideoCapture(videoFilename) |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
230 if capture.isOpened(): |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
231 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
232 capture.release() |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
233 return fps |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
234 else: |
|
435
17185fe77316
added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
429
diff
changeset
|
235 print('Video capture for {} failed'.format(videoFilename)) |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
236 return None |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
396
diff
changeset
|
237 |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
238 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800): |
|
416
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
239 'Computes the bounding box of object at frameNum' |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
240 x = [] |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
241 y = [] |
|
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
242 if obj.hasFeatures(): |
|
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
243 for f in obj.getFeatures(): |
|
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
244 if f.existsAtInstant(frameNum): |
|
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
245 projectedPosition = f.getPositionAtInstant(frameNum).project(homography) |
|
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
246 x.append(projectedPosition.x) |
|
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
247 y.append(projectedPosition.y) |
|
416
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
248 xmin = min(x) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
249 xmax = max(x) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
250 ymin = min(y) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
251 ymax = max(y) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
252 xMm = px * (xmax - xmin) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
253 yMm = py * (ymax - ymin) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
254 a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm)) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
255 yCropMin = int(max(0, .5 * (ymin + ymax - a))) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
256 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
257 xCropMin = int(max(0, .5 * (xmin + xmax - a))) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
258 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
259 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels: |
|
520
fd9641cbd24b
added function to classify object at instant from SVM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
515
diff
changeset
|
260 croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax] |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
261 else: |
|
520
fd9641cbd24b
added function to classify object at instant from SVM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
515
diff
changeset
|
262 croppedImg = [] |
| 548 | 263 return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
264 |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
265 |
|
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
266 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
267 '''Displays the objects overlaid frame by frame over the video ''' |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
268 from moving import userTypeNames |
|
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
269 from math import ceil, log10 |
|
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
270 |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
271 capture = cv2.VideoCapture(videoFilename) |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
272 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
|
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
273 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
|
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
274 |
|
544
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
275 windowName = 'frame' |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
276 #cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
277 |
|
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
278 if undistort: # setup undistortion |
|
510
b0dac840c24f
compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
279 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
280 if capture.isOpened(): |
|
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
281 key = -1 |
|
227
b7612c6d5702
cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
226
diff
changeset
|
282 ret = True |
|
b7612c6d5702
cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
226
diff
changeset
|
283 frameNum = firstFrameNum |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
284 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
|
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
285 if lastFrameNumArg is None: |
|
248
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
243
diff
changeset
|
286 from sys import maxint |
|
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
243
diff
changeset
|
287 lastFrameNum = maxint |
|
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
243
diff
changeset
|
288 else: |
|
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
243
diff
changeset
|
289 lastFrameNum = lastFrameNumArg |
|
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
290 nZerosFilename = int(ceil(log10(lastFrameNum))) |
|
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
625
diff
changeset
|
291 while ret and not quitKey(key) and frameNum <= lastFrameNum: |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
292 ret, img = capture.read() |
|
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
293 if ret: |
|
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
294 if undistort: |
|
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
295 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) |
|
381
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
296 if printFrames: |
|
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
365
diff
changeset
|
297 print('frame {0}'.format(frameNum)) |
|
203
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
298 for obj in objects: |
|
e2f31813ade6
added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
171
diff
changeset
|
299 if obj.existsAtInstant(frameNum): |
|
236
eb4525853030
added script to display trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
300 if not hasattr(obj, 'projectedPositions'): |
|
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
301 if homography is not None: |
|
218
b5772df11b37
corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
216
diff
changeset
|
302 obj.projectedPositions = obj.positions.project(homography) |
|
b5772df11b37
corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
216
diff
changeset
|
303 else: |
|
b5772df11b37
corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
216
diff
changeset
|
304 obj.projectedPositions = obj.positions |
|
515
727e3c529519
renamed all draw functions to plot for consistency
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
511
diff
changeset
|
305 cvPlot(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) |
|
393
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
385
diff
changeset
|
306 if frameNum in boundingBoxes.keys(): |
|
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
385
diff
changeset
|
307 for rect in boundingBoxes[frameNum]: |
|
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
385
diff
changeset
|
308 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed) |
|
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
658
diff
changeset
|
309 elif obj.hasFeatures(): |
|
416
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
310 imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(img, obj, frameNum, homography, width, height) |
|
8fdbc13dad8b
cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
411
diff
changeset
|
311 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
312 objDescription = '{} '.format(obj.num) |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
313 if userTypeNames[obj.userType] != 'unknown': |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
314 objDescription += userTypeNames[obj.userType][0].upper() |
|
429
2be846d36dec
reverted to stable OpenCV release 2.4.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
416
diff
changeset
|
315 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
|
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
316 if not saveAllImages: |
|
544
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
317 cvImshow(windowName, img, rescale) |
|
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
318 key = cv2.waitKey() |
|
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
319 if saveAllImages or saveKey(key): |
|
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
478
diff
changeset
|
320 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) |
|
478
d337bffd7283
Display of points in compute homography and step option to replay videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
474
diff
changeset
|
321 frameNum += nFramesStep |
|
d337bffd7283
Display of points in compute homography and step option to replay videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
474
diff
changeset
|
322 if nFramesStep > 1: |
|
d337bffd7283
Display of points in compute homography and step option to replay videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
474
diff
changeset
|
323 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) |
|
346
5f75d6c23ed5
added opencv function to destroy OpenCV windows (seems to work only on Windows)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
319
diff
changeset
|
324 cv2.destroyAllWindows() |
|
435
17185fe77316
added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
429
diff
changeset
|
325 else: |
|
17185fe77316
added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
429
diff
changeset
|
326 print 'Cannot load file ' + videoFilename |
|
402
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
327 |
|
639
4e7925cb4f8f
modified tsai camera homography computation to avoid using os dependent temporary files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
638
diff
changeset
|
328 def computeHomographyFromPDTV(camera): |
|
4e7925cb4f8f
modified tsai camera homography computation to avoid using os dependent temporary files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
638
diff
changeset
|
329 '''Returns the homography matrix at ground level from PDTV camera |
|
402
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
330 https://bitbucket.org/hakanardo/pdtv''' |
|
639
4e7925cb4f8f
modified tsai camera homography computation to avoid using os dependent temporary files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
638
diff
changeset
|
331 # camera = pdtv.load(cameraFilename) |
|
402
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
332 srcPoints = [[x,y] for x, y in zip([1.,2.,2.,1.],[1.,1.,2.,2.])] # need floats!! |
|
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
333 dstPoints = [] |
|
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
334 for srcPoint in srcPoints: |
|
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
335 projected = camera.image_to_world(tuple(srcPoint)) |
|
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
336 dstPoints.append([projected[0], projected[1]]) |
|
638
852f5de42d01
added functionality to read Aliaksei Tsai camera model data
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
337 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method = 0) # No need for different methods for finding homography |
|
402
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
338 return H |
|
f29204e68aab
function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
339 |
|
474
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
340 def undistortedCoordinates(map1, map2, x, y, maxDistance = 1.): |
|
468
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
341 '''Returns the coordinates of a point in undistorted image |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
342 map1 and map2 are the mapping functions from undistorted image |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
343 to distorted (original image) |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
344 map1(x,y) = originalx, originaly''' |
|
672
5473b7460375
moved and rationalized imports in modules
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
345 from numpy import abs, logical_and, unravel_index, sum |
|
468
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
346 from matplotlib.mlab import find |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
347 distx = abs(map1-x) |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
348 disty = abs(map2-y) |
|
472
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
349 indices = logical_and(distx<maxDistance, disty<maxDistance) |
|
468
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
350 closeCoordinates = unravel_index(find(indices), distx.shape) # returns i,j, ie y,x |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
351 xWeights = 1-distx[indices] |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
352 yWeights = 1-disty[indices] |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
353 return dot(xWeights, closeCoordinates[1])/sum(xWeights), dot(yWeights, closeCoordinates[0])/sum(yWeights) |
|
5304299e53a5
added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
435
diff
changeset
|
354 |
|
474
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
355 def undistortTrajectoryFromCVMapping(map1, map2, t): |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
356 '''test 'perfect' inversion''' |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
357 from moving import Trajectory |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
358 from numpy import isnan |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
359 undistortedTrajectory = Trajectory() |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
360 for i,p in enumerate(t): |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
361 res = undistortedCoordinates(map1, map2, p.x,p.y) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
362 if not isnan(res).any(): |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
363 undistortedTrajectory.addPositionXY(res[0], res[1]) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
364 else: |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
365 print i,p,res |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
366 return undistortedTrajectory |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
367 |
|
472
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
368 def computeInverseMapping(originalImageSize, map1, map2): |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
369 'Computes inverse mapping from maps provided by cv2.initUndistortRectifyMap' |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
370 from numpy import ones, isnan |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
371 invMap1 = -ones(originalImageSize) |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
372 invMap2 = -ones(originalImageSize) |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
373 for x in range(0,originalImageSize[1]): |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
374 for y in range(0,originalImageSize[0]): |
|
474
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
375 res = undistortedCoordinates(x,y, map1, map2) |
|
472
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
376 if not isnan(res).any(): |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
377 invMap1[y,x] = res[0] |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
378 invMap2[y,x] = res[1] |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
379 return invMap1, invMap2 |
|
a50c026fdf14
functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
468
diff
changeset
|
380 |
|
544
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
381 def cameraIntrinsicCalibration(path, checkerBoardSize=[6,7], secondPassSearch=False, display=False): |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
382 ''' Camera calibration searches through all the images (jpg or png) located |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
383 in _path_ for matches to a checkerboard pattern of size checkboardSize. |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
384 These images should all be of the same camera with the same resolution. |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
385 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
386 For best results, use an asymetric board and ensure that the image has |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
387 very high contrast, including the background. Suitable checkerboard: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
388 http://ftp.isr.ist.utl.pt/pub/roswiki/attachments/camera_calibration(2f)Tutorials(2f)StereoCalibration/check-108.png |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
389 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
390 The code below is based off of: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
391 https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
392 Modified by Paul St-Aubin |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
393 ''' |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
394 from numpy import zeros, mgrid, float32, savetxt |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
395 import glob, os |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
396 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
397 # termination criteria |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
398 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
399 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
400 # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
401 objp = zeros((checkerBoardSize[0]*checkerBoardSize[1],3), float32) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
402 objp[:,:2] = mgrid[0:checkerBoardSize[1],0:checkerBoardSize[0]].T.reshape(-1,2) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
403 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
404 # Arrays to store object points and image points from all the images. |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
405 objpoints = [] # 3d point in real world space |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
406 imgpoints = [] # 2d points in image plane. |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
407 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
408 ## Loop throuhg all images in _path_ |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
409 images = glob.glob(os.path.join(path,'*.[jJ][pP][gG]'))+glob.glob(os.path.join(path,'*.[jJ][pP][eE][gG]'))+glob.glob(os.path.join(path,'*.[pP][nN][gG]')) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
410 for fname in images: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
411 img = cv2.imread(fname) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
412 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
413 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
414 # Find the chess board corners |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
415 ret, corners = cv2.findChessboardCorners(gray, (checkerBoardSize[1],checkerBoardSize[0]), None) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
416 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
417 # If found, add object points, image points (after refining them) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
418 if ret: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
419 print 'Found pattern in '+fname |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
420 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
421 if(secondPassSearch): |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
422 corners = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
423 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
424 objpoints.append(objp) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
425 imgpoints.append(corners) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
426 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
427 # Draw and display the corners |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
428 if(display): |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
429 img = cv2.drawChessboardCorners(img, (checkerBoardSize[1],checkerBoardSize[0]), corners, ret) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
430 if(img): |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
431 cv2.imshow('img',img) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
432 cv2.waitKey(0) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
433 |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
434 ## Close up image loading and calibrate |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
435 cv2.destroyAllWindows() |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
436 if len(objpoints) == 0 or len(imgpoints) == 0: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
437 return False |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
438 try: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
439 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
440 except NameError: |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
441 return False |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
442 savetxt('intrinsic-camera.txt', camera_matrix) |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
443 return camera_matrix, dist_coeffs |
|
749672171789
added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
538
diff
changeset
|
444 |
|
545
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
445 def undistortImage(img, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., interpolation=cv2.INTER_LINEAR): |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
446 '''Undistorts the image passed in argument''' |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
447 width = img.shape[1] |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
448 height = img.shape[0] |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
449 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
450 return cv2.remap(img, map1, map2, interpolation=interpolation) |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
451 |
|
9816fab353f3
added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
544
diff
changeset
|
452 |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
453 def printCvMat(cvmat, out = stdout): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
454 '''Prints the cvmat to out''' |
|
384
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
455 print('Deprecated, use new interface') |
|
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
456 for i in xrange(cvmat.rows): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
457 for j in xrange(cvmat.cols): |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
458 out.write('{0} '.format(cvmat[i,j])) |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
459 out.write('\n') |
|
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
460 |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
461 def projectArray(homography, points): |
|
384
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
462 '''Returns the coordinates of the projected points through homography |
|
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
463 (format: array 2xN points)''' |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
464 if points.shape[0] != 2: |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
465 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
466 |
|
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
467 if (homography is not None) and homography.size>0: |
|
639
4e7925cb4f8f
modified tsai camera homography computation to avoid using os dependent temporary files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
638
diff
changeset
|
468 #alternatively, on could use cv2.convertpointstohomogeneous and other conversion to/from homogeneous coordinates |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
469 augmentedPoints = append(points,[[1]*points.shape[1]], 0) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
470 prod = dot(homography, augmentedPoints) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
471 return prod[0:2]/prod[2] |
| 28 | 472 else: |
| 547 | 473 return points |
| 28 | 474 |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
475 def project(homography, p): |
| 319 | 476 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
477 through homography''' |
|
243
e0988a8ace0c
started adapting and moving to other modules Mohamed's work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
236
diff
changeset
|
478 return projectArray(homography, array([[p[0]],[p[1]]])) |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
479 |
| 28 | 480 def projectTrajectory(homography, trajectory): |
| 481 '''Projects a series of points in the format | |
| 482 [[x1, x2, ...], | |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
483 [y1, y2, ...]]''' |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
484 return projectArray(homography, array(trajectory)) |
| 28 | 485 |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
486 def invertHomography(homography): |
|
384
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
487 '''Returns an inverted homography |
|
6da9cf5609aa
adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
488 Unnecessary for reprojection over camera image''' |
|
673
5505f9dbb28e
corrected import bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
672
diff
changeset
|
489 from numpy.linalg import inv |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
490 invH = inv(homography) |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
491 invH /= invH[2,2] |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
492 return invH |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
493 |
|
474
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
494 def undistortTrajectory(invMap1, invMap2, positions): |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
495 from numpy import floor, ceil |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
496 floorPositions = floor(positions) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
497 #ceilPositions = ceil(positions) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
498 undistortedTrajectory = [[],[]] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
499 for i in xrange(len(positions[0])): |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
500 x,y = None, None |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
501 if positions[0][i]+1 < invMap1.shape[1] and positions[1][i]+1 < invMap1.shape[0]: |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
502 floorX = invMap1[floorPositions[1][i], floorPositions[0][i]] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
503 floorY = invMap2[floorPositions[1][i], floorPositions[0][i]] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
504 ceilX = invMap1[floorPositions[1][i]+1, floorPositions[0][i]+1] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
505 ceilY = invMap2[floorPositions[1][i]+1, floorPositions[0][i]+1] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
506 #ceilX = invMap1[ceilPositions[1][i], ceilPositions[0][i]] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
507 #ceilY = invMap2[ceilPositions[1][i], ceilPositions[0][i]] |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
508 if floorX >=0 and floorY >=0 and ceilX >=0 and ceilY >=0: |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
509 x = floorX+(positions[0][i]-floorPositions[0][i])*(ceilX-floorX) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
510 y = floorY+(positions[1][i]-floorPositions[1][i])*(ceilY-floorY) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
511 undistortedTrajectory[0].append(x) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
512 undistortedTrajectory[1].append(y) |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
513 return undistortedTrajectory |
|
59903d14d244
added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
473
diff
changeset
|
514 |
|
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
515 def projectGInputPoints(homography, points): |
|
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
516 return projectTrajectory(homography, array(points+[points[0]]).T) |
|
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
517 |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
518 if opencvAvailable: |
|
154
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
519 def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)): |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
520 '''Computes the translation of img2 with respect to img1 |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
521 (loaded using OpenCV as numpy arrays) |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
522 img1Points are used to compute the translation |
|
100
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
523 |
|
154
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
524 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom, other non linear distortion)''' |
|
672
5473b7460375
moved and rationalized imports in modules
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
525 from numpy import median, sum |
|
100
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
526 |
|
154
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
527 nextPoints = array([]) |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
528 (img2Points, status, track_error) = cv2.calcOpticalFlowPyrLK(img1, img2, img1Points, nextPoints, winSize=windowSize, maxLevel=level, criteria=criteria) |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
529 # calcOpticalFlowPyrLK(prevImg, nextImg, prevPts[, nextPts[, status[, err[, winSize[, maxLevel[, criteria[, derivLambda[, flags]]]]]]]]) -> nextPts, status, err |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
530 delta = [] |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
531 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): |
|
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
532 if status[k] == 1: |
|
154
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
533 dp = p2-p1 |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
534 d = sum(dp**2) |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
535 if d < maxTranslation2: |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
536 delta.append(dp) |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
537 if len(delta) >= minNMatches: |
|
668710d4c773
updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
538 return median(delta, axis=0) |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
539 else: |
|
156
2eef5620c0b3
added key values for opencv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
154
diff
changeset
|
540 print(dp) |
|
112
67555e968b5e
added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
102
diff
changeset
|
541 return None |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
542 |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
543 if skimageAvailable: |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
544 from skimage.feature import hog |
|
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
545 from skimage import color, transform |
|
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
546 |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
547 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
548 bwImg = color.rgb2gray(image) |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
549 inputImg = transform.resize(bwImg, rescaleSize) |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
550 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
551 if visualize: |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
552 from matplotlib.pyplot import imshow, figure, subplot |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
553 hogViz = features[1] |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
554 features = features[0] |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
555 figure() |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
556 subplot(1,2,1) |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
557 imshow(inputImg) |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
558 subplot(1,2,2) |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
559 imshow(hogViz) |
|
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
673
diff
changeset
|
560 return float32(features) |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
561 |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
562 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
563 from os import listdir |
|
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
564 from matplotlib.pyplot import imread |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
565 |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
566 inputData = [] |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
567 for filename in listdir(imageDirectory): |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
568 img = imread(imageDirectory+filename) |
|
411
31604ef1cad4
added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
410
diff
changeset
|
569 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) |
|
365
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
570 inputData.append(features) |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
571 |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
572 nImages = len(inputData) |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
573 return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32) |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
574 |
|
22ddb8f85495
added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
346
diff
changeset
|
575 |
