annotate trafficintelligence/cvutils.py @ 1278:8e61ff3cd503 default tip

correct bug to take into account first frame num in config, and other related bugs in dltrack.py
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 27 Jun 2024 15:31:36 -0400
parents ef68d4ba7dae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #! /usr/bin/env python
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
2 '''Image/Video utilities'''
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
1029
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
4 from sys import stdout
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
5 from os import listdir
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
6 from subprocess import run
1245
371c718e57d7 interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1242
diff changeset
7 from math import floor, log10, ceil, inf
1029
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
8 from time import time
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
9
1168
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
10 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil, nonzero, dtype
1029
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
11 from numpy.linalg import inv
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
12 from matplotlib.pyplot import imread, imsave, imshow, figure, subplot
665
15e244d2a1b5 corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 661
diff changeset
13
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
14 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
15 import cv2
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
16 opencvAvailable = True
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
17 except ImportError:
501
c81cbd6953fb update to classify speed to remove data at both ends
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 491
diff changeset
18 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
19 opencvAvailable = False
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
20 try:
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
21 import skimage
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
22 skimageAvailable = True
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
23 except ImportError:
410
91954c76d12c minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 406
diff changeset
24 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
25 skimageAvailable = False
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
26
1029
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
27 from trafficintelligence import utils, moving
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
28
969
5d788d2e8ffc work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 967
diff changeset
29 videoFilenameExtensions = ['mov', 'avi', 'mp4', 'MOV', 'AVI', 'MP4']
1003
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
30 trackerExe = 'feature-based-tracking'
981
c3e690c5536e corrected bug in display trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 978
diff changeset
31 #importaggdraw # 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
32
1253
ef68d4ba7dae added loading ego vehicle in kitti 2D format and method to plot outline
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1245
diff changeset
33 colors = 'rgbcymk'
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
34 cvRed = {'default': (0,0,255),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
35 'colorblind': (0,114,178)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
36 cvGreen = {'default': (0,255,0),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
37 'colorblind': (0,158,115)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
38 cvBlue = {'default': (255,0,0),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
39 'colorblind': (213,94,0)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
40 cvCyan = {'default': (255, 255, 0),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
41 'colorblind': (240,228,66)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
42 cvYellow = {'default': (0, 255, 255),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
43 'colorblind': (86,180,233)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
44 cvMagenta = {'default': (255, 0, 255),
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
45 'colorblind': (204,121,167)}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
46 cvWhite = {k: (255, 255, 255) for k in ['default', 'colorblind']}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
47 cvBlack = {k: (0,0,0) for k in ['default', 'colorblind']}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
48
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
49 cvColors3 = {k: utils.PlottingPropertyValues([cvRed[k], cvGreen[k], cvBlue[k]]) for k in ['default', 'colorblind']}
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
50 cvColors = {k: utils.PlottingPropertyValues([cvRed[k], cvGreen[k], cvBlue[k], cvCyan[k], cvYellow[k], cvMagenta[k], cvWhite[k], cvBlack[k]]) for k in ['default', 'colorblind']}
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
51
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
52 def quitKey(key):
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
53 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
54
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
55 def saveKey(key):
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
56 return chr(key&255) == 's'
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
57
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
58 def int2FOURCC(x):
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
59 fourcc = ''
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
60 for i in range(4):
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
61 fourcc += chr((x >> 8*i)&255)
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
62 return fourcc
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
63
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
64 def rgb2gray(rgb):
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
65 return dot(rgb[...,:3], [0.299, 0.587, 0.144])
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
66
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
67 def matlab2PointCorrespondences(filename):
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
68 '''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
69 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
70 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
71 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
72
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
73 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
74 '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)'''
1168
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
75 points = loadtxt(filename, dtype=dtype('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
76 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
77
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
78 def cvMatToArray(cvmat):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
79 '''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
80 print('Deprecated, use new interface')
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
81 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
82 for i in range(cvmat.rows):
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
83 for j in range(cvmat.cols):
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
84 a[i,j] = cvmat[i,j]
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
85 return a
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
86
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
87 def createWhiteImage(height, width, filename):
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
88 img = ones((height, width, 3), uint8)*255
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
89 imsave(filename, img)
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
90
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
91 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
92 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
93 '''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
94 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
95 return H
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
96
658
6668f541b915 Added **kwargs passthrough to cvPlot()->cv2.line() for greater drawing flexibility.
pstaub
parents: 657
diff changeset
97 def cvPlot(img, positions, color, lastCoordinate = None, **kwargs):
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
98 if lastCoordinate is None:
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
99 last = positions.length()-1
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
100 elif lastCoordinate >=0:
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
101 last = min(positions.length()-1, lastCoordinate)
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
102 for i in range(0, last):
658
6668f541b915 Added **kwargs passthrough to cvPlot()->cv2.line() for greater drawing flexibility.
pstaub
parents: 657
diff changeset
103 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
104
385
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
105 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
106 '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
107 if rescale != 1.:
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
108 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale)))
1029
c6cf75a2ed08 reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1028
diff changeset
109 resizedImg = cv2.resize(img, size)
385
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
110 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
111 else:
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
112 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
113
510
b0dac840c24f compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 509
diff changeset
114 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients):
b0dac840c24f compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 509
diff changeset
115 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication)))
924
a71455bd8367 work in progress on undistortion acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 904
diff changeset
116 newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, newImgSize, True)
933
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
117 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), None, newCameraMatrix, newImgSize, cv2.CV_32FC1), newCameraMatrix
510
b0dac840c24f compute homography works with undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 509
diff changeset
118
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
119 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1, colorBlind = False):
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
120 '''Plays the video(s)'''
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
121 if colorBlind:
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
122 colorType = 'colorblind'
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
123 else:
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
124 colorType = 'default'
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
125 if len(filenames) == 0:
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
126 print('Empty filename list')
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
127 return
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
128 if windowNames is None:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
129 windowNames = ['frame{}'.format(i) for i in range(len(filenames))]
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
130 wait = 5
685
94b291a5f933 several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 680
diff changeset
131 if rescale == 1.:
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
132 for windowName in windowNames:
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
133 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
134 if frameRate > 0:
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
135 wait = int(round(1000./frameRate))
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
136 if interactive:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
137 wait = 0
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
138 captures = [cv2.VideoCapture(fn) for fn in filenames]
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
139 if array([cap.isOpened() for cap in captures]).all():
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
140 key = -1
227
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
141 ret = True
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
142 nFramesShown = 0
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
143 if firstFrameNums is not None:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
144 for i in range(len(captures)):
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
145 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i])
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
146 while ret and not quitKey(key):
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
147 rets = []
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
148 images = []
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
149 for cap in captures:
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
150 ret, img = cap.read()
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
151 rets.append(ret)
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
152 images.append(img)
958
747a5c68bd3c minor improvement and bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 950
diff changeset
153 ret = array(rets).all()
747a5c68bd3c minor improvement and bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 950
diff changeset
154 if ret:
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
155 if printFrames:
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
156 print('frame shown {0}'.format(nFramesShown))
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
157 for i in range(len(filenames)):
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
158 if text is not None:
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
159 cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed[colorType])
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
160 cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img)
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
161 key = cv2.waitKey(wait)
625
9202628a4130 saving image when playing video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 549
diff changeset
162 if saveKey(key):
9202628a4130 saving image when playing video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 549
diff changeset
163 cv2.imwrite('image-{}.png'.format(frameNum), img)
821
26daf35180ad finished modification and demo script to replay synchronized video (with same frame rate)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 820
diff changeset
164 nFramesShown += step
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
165 if step > 1:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
166 for i in range(len(captures)):
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
167 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown)
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
168 cv2.destroyAllWindows()
435
17185fe77316 added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 429
diff changeset
169 else:
820
e73e7b644428 generalized play-video for several files (already synchronized
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 807
diff changeset
170 print('Video captures for {} failed'.format(filenames))
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
171
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
172 def infoVideo(filename):
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
173 '''Provides all available info on video '''
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
174 cvPropertyNames = {cv2.CAP_PROP_FORMAT: "format",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
175 cv2.CAP_PROP_FOURCC: "codec (fourcc)",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
176 cv2.CAP_PROP_FPS: "fps",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
177 cv2.CAP_PROP_FRAME_COUNT: "number of frames",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
178 cv2.CAP_PROP_FRAME_HEIGHT: "heigh",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
179 cv2.CAP_PROP_FRAME_WIDTH: "width",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
180 cv2.CAP_PROP_RECTIFICATION: "rectification",
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
181 cv2.CAP_PROP_SATURATION: "saturation"}
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
182 capture = cv2.VideoCapture(filename)
967
373e8ef6ee25 modified function to access video property
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 959
diff changeset
183 videoProperties = {}
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
184 if capture.isOpened():
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
185 for cvprop in [#cv2.CAP_PROP_BRIGHTNESS
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
186 #cv2.CAP_PROP_CONTRAST
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
187 #cv2.CAP_PROP_CONVERT_RGB
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
188 #cv2.CAP_PROP_EXPOSURE
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
189 cv2.CAP_PROP_FORMAT,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
190 cv2.CAP_PROP_FOURCC,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
191 cv2.CAP_PROP_FPS,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
192 cv2.CAP_PROP_FRAME_COUNT,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
193 cv2.CAP_PROP_FRAME_HEIGHT,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
194 cv2.CAP_PROP_FRAME_WIDTH,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
195 #cv2.CAP_PROP_GAIN,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
196 #cv2.CAP_PROP_HUE
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
197 #cv2.CAP_PROP_MODE
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
198 #cv2.CAP_PROP_POS_AVI_RATIO
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
199 #cv2.CAP_PROP_POS_FRAMES
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
200 #cv2.CAP_PROP_POS_MSEC
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
201 #cv2.CAP_PROP_RECTIFICATION,
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
202 #cv2.CAP_PROP_SATURATION
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
203 ]:
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
204 prop = capture.get(cvprop)
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
205 if cvprop == cv2.CAP_PROP_FOURCC and prop > 0:
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
206 prop = int2FOURCC(int(prop))
967
373e8ef6ee25 modified function to access video property
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 959
diff changeset
207 videoProperties[cvPropertyNames[cvprop]] = prop
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
208 else:
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
209 print('Video capture for {} failed'.format(filename))
967
373e8ef6ee25 modified function to access video property
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 959
diff changeset
210 return videoProperties
657
51269511229b added script printing video info
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 639
diff changeset
211
798
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
212 def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'):
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
213 '''Returns nFrames images from the video sequence'''
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
214 images = []
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
215 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
216 if capture.isOpened():
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
217 rawCount = capture.get(cv2.CAP_PROP_FRAME_COUNT)
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
218 if rawCount < 0:
798
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
219 rawCount = lastFrameNum+1
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
220 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
221 ret = False
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
222 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
798
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
223 frameNum = firstFrameNum
858
2faabcbde2c4 minor improvements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 821
diff changeset
224 while frameNum<lastFrameNum and frameNum<rawCount:
171
8e7b354666ec corrected bug in to get images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
225 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
226 i = 0
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
227 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
228 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
229 i += 1
799
0662c87a61c9 minor modification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 798
diff changeset
230 if img is not None and 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
231 if saveImage:
798
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
232 frameNumStr = format(frameNum, '0{}d'.format(nDigits))
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
233 cv2.imwrite(outputPrefix+frameNumStr+'.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
234 else:
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
235 images.append(img)
798
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
236 frameNum +=step
5b99b676265e modified to get images very time step
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 788
diff changeset
237 if step > 1:
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
238 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum)
398
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
239 capture.release()
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
240 else:
435
17185fe77316 added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 429
diff changeset
241 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
242 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
243
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
244 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
245 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
246 if capture.isOpened():
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
247 fps = capture.get(cv2.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
248 capture.release()
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
249 return fps
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
250 else:
435
17185fe77316 added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 429
diff changeset
251 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
252 return None
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
253
928
063d1267585d work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 926
diff changeset
254 def imageBoxSize(obj, frameNum, width, height, px = 0.2, py = 0.2):
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
255 'Computes the bounding box size 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
256 x = []
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
257 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
258 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
259 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
260 if f.existsAtInstant(frameNum):
929
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 928
diff changeset
261 p = f.getPositionAtInstant(frameNum)
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 928
diff changeset
262 x.append(p.x)
be28a3538dc9 work in progress on projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 928
diff changeset
263 y.append(p.y)
416
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
264 xmin = min(x)
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
265 xmax = max(x)
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
266 ymin = min(y)
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
267 ymax = max(y)
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
268 xMm = px * (xmax - xmin)
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
269 yMm = py * (ymax - ymin)
1125
b358bed29ab4 updates and bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1112
diff changeset
270 a = max(ymax - ymin + (2 * yMm), xmax - xmin + (2 * xMm))
416
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
271 yCropMin = int(max(0, .5 * (ymin + ymax - a)))
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
272 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a)))
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
273 xCropMin = int(max(0, .5 * (xmin + xmax - a)))
8fdbc13dad8b cleaned imagebox code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 411
diff changeset
274 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a)))
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
275 return yCropMin, yCropMax, xCropMin, xCropMax
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
276
928
063d1267585d work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 926
diff changeset
277 def imageBox(img, obj, frameNum, width, height, px = 0.2, py = 0.2, minNPixels = 800):
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
278 'Computes the bounding box of object at frameNum'
928
063d1267585d work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 926
diff changeset
279 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, width, height, px, py)
1125
b358bed29ab4 updates and bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1112
diff changeset
280 if yCropMax > yCropMin and xCropMax > xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels:
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
281 return 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
282 else:
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
283 return None
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
284
1004
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
285 def tracking(configFilename, grouping, videoFilename = None, dbFilename = None, homographyFilename = None, maskFilename = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, dryRun = False):
1003
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
286 '''Runs the tracker in a subprocess
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
287 if grouping is True, it is feature grouping
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
288 otherwise it is feature tracking'''
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
289 if grouping:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
290 trackingMode = '--gf'
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
291 else:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
292 trackingMode = '--tf'
1021
16932cefabc1 work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1008
diff changeset
293 cmd = [trackerExe, configFilename, trackingMode, '--quiet']
1003
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
294
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
295 if videoFilename is not None:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
296 cmd += ['--video-filename', videoFilename]
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
297 if dbFilename is not None:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
298 cmd += ['--database-filename', dbFilename]
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
299 if homographyFilename is not None:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
300 cmd += ['--homography-filename', homographyFilename]
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
301 if maskFilename is not None:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
302 cmd += ['--mask-filename', maskFilename]
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
303 if undistort:
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
304 cmd += ['--undistort', 'true']
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
305 if intrinsicCameraMatrix is not None: # we currently have to save a file
1004
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
306 intrinsicCameraFilename = '/tmp/intrinsic-{}.txt'.format(time())
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
307 savetxt(intrinsicCameraFilename, intrinsicCameraMatrix)
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
308 cmd += ['--intrinsic-camera-filename', intrinsicCameraFilename]
1003
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
309 if distortionCoefficients is not None:
1008
a5f2309bb1ff solved issue with subprocess
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1007
diff changeset
310 cmd += ['--distortion-coefficients '+' '.join([str(x) for x in distortionCoefficients])]
1004
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
311 if dryRun:
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
312 print(cmd)
75601be6019f work on process
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1003
diff changeset
313 else:
1007
192de96e5255 solved config filename bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1005
diff changeset
314 run(cmd)
1003
75af46516b2b work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
315
1245
371c718e57d7 interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1242
diff changeset
316 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNum = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, nZerosFilenameArg = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}, colorBlind = False, **kwargs):
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
317 '''Displays the objects overlaid frame by frame over the video '''
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
318 if colorBlind:
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
319 colorType = 'colorblind'
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
320 else:
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
321 colorType = 'default'
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
322
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
323 capture = cv2.VideoCapture(videoFilename)
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
324 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
325 height = int(capture.get(cv2.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
326
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
327 windowName = 'frame'
685
94b291a5f933 several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 680
diff changeset
328 if rescale == 1.:
94b291a5f933 several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 680
diff changeset
329 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
330
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
331 if undistort: # setup undistortion
933
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
332 [map1, map2], newCameraMatrix = 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
333 if capture.isOpened():
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
334 key = -1
227
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
335 ret = True
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
336 frameNum = firstFrameNum
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
337 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
989
132d84ce9f0c bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 981
diff changeset
338 if nZerosFilenameArg is None:
1245
371c718e57d7 interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1242
diff changeset
339 if lastFrameNum is None or lastFrameNum == inf:
989
132d84ce9f0c bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 981
diff changeset
340 nZerosFilename = int(ceil(log10(objects[-1].getLastInstant())))
981
c3e690c5536e corrected bug in display trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 978
diff changeset
341 else:
989
132d84ce9f0c bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 981
diff changeset
342 nZerosFilename = int(ceil(log10(lastFrameNum)))
132d84ce9f0c bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 981
diff changeset
343 else:
132d84ce9f0c bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 981
diff changeset
344 nZerosFilename = nZerosFilenameArg
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
345 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
346 ret, img = capture.read()
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
347 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
348 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
349 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
350 if printFrames:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
351 print('frame {0}'.format(frameNum))
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
352 # plot objects
947
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
353 for obj in objects[:]:
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
354 if obj.existsAtInstant(frameNum):
236
eb4525853030 added script to display trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 235
diff changeset
355 if not hasattr(obj, 'projectedPositions'):
933
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
356 obj.projectedPositions = obj.getPositions().homographyProject(homography)
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
357 if undistort:
8ac7f61c6e4f major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 932
diff changeset
358 obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix)
1158
7eb972942f22 solving bug to pass kwargs for plotting
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1125
diff changeset
359 cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant(), **kwargs)
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
360 if frameNum not in boundingBoxes and obj.hasFeatures():
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 903
diff changeset
361 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height)
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
362 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 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
363 objDescription = '{} '.format(obj.num)
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
364 if moving.userTypeNames[obj.userType] != 'unknown':
1242
4cd8ace3552f major update for classification, allowing the use of neural network classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1205
diff changeset
365 s = moving.userTypeNames[obj.userType]
4cd8ace3552f major update for classification, allowing the use of neural network classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1205
diff changeset
366 objDescription += s[0].upper()+s[1]
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
367 if len(annotations) > 0: # if we loaded annotations, but there is no match
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
368 if frameNum not in toMatches[obj.getNum()]:
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
369 objDescription += " FA"
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
370 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvColors[colorType][obj.getNum()])
947
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
371 if obj.getLastInstant() == frameNum:
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
372 objects.remove(obj)
725
35bc5e30a53f slight reorganization of display-trajectories (more efficient filtering of list of objects for long sequences)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 685
diff changeset
373 # plot object bounding boxes
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 993
diff changeset
374 if frameNum in boundingBoxes:
725
35bc5e30a53f slight reorganization of display-trajectories (more efficient filtering of list of objects for long sequences)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 685
diff changeset
375 for rect in boundingBoxes[frameNum]:
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
376 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvColors[colorType][obj.getNum()])
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
377 # plot ground truth
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
378 if len(annotations) > 0:
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
379 for gt in annotations:
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
380 if gt.existsAtInstant(frameNum):
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
381 if frameNum in gtMatches[gt.getNum()]:
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
382 color = cvColors[colorType][gtMatches[gt.getNum()][frameNum]] # same color as object
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
383 else:
868
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
384 color = cvRed[colorType]
1fdafa9f6bf4 added colors more friendly for color blind people (thanks Ryan Louie!)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 864
diff changeset
385 cv2.putText(img, 'Miss', gt.topLeftPositions[frameNum-gt.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, color)
726
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
386 cv2.rectangle(img, gt.topLeftPositions[frameNum-gt.getFirstInstant()].asint().astuple(), gt.bottomRightPositions[frameNum-gt.getFirstInstant()].asint().astuple(), color)
43ae3a1af290 added functionality to display matchings between ground truth and tracked objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 725
diff changeset
387 # saving images and going to next
482
f6415f012640 adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 478
diff changeset
388 if not saveAllImages:
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
389 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
390 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
391 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
392 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
393 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
394 if nFramesStep > 1:
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
395 capture.set(cv2.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
396 cv2.destroyAllWindows()
435
17185fe77316 added error messages if video not opened
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 429
diff changeset
397 else:
774
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 772
diff changeset
398 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
399
639
4e7925cb4f8f modified tsai camera homography computation to avoid using os dependent temporary files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 638
diff changeset
400 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
401 '''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
402 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
403 # 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
404 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
405 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
406 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
407 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
408 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
409 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
410 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
411
895
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
412 def getIntrinsicCameraMatrix(cameraData):
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
413 return array([[cameraData['f']*cameraData['Sx']/cameraData['dx'], 0, cameraData['Cx']],
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
414 [0, cameraData['f']/cameraData['dy'], cameraData['Cy']],
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
415 [0, 0, 1.]])
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
416
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
417 def getDistortionCoefficients(cameraData):
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
418 return array([cameraData['k']]+4*[0])
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
419
474
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
420 def undistortedCoordinates(map1, map2, x, y, maxDistance = 1.):
468
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
421 '''Returns the coordinates of a point in undistorted image
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
422 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
423 to distorted (original image)
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
424 map1(x,y) = originalx, originaly'''
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
425 distx = npabs(map1-x)
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
426 disty = npabs(map2-y)
472
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
427 indices = logical_and(distx<maxDistance, disty<maxDistance)
1112
956a66096e91 removed code now available in simulation project, and issue with deprecated find function in matplotlib
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1029
diff changeset
428 closeCoordinates = unravel_index(nonzero(indices), distx.shape) # returns i,j, ie y,x
468
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
429 xWeights = 1-distx[indices]
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
430 yWeights = 1-disty[indices]
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
431 return dot(xWeights, closeCoordinates[1])/npsum(xWeights), dot(yWeights, closeCoordinates[0])/npsum(yWeights)
468
5304299e53a5 added coordinate undistortion function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 435
diff changeset
432
474
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
433 def undistortTrajectoryFromCVMapping(map1, map2, t):
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
434 '''test 'perfect' inversion'''
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
435 undistortedTrajectory = moving.Trajectory()
474
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
436 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
437 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
438 if not isnan(res).any():
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
439 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
440 else:
774
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 772
diff changeset
441 print('{} {} {}'.format(i,p,res))
474
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
442 return undistortedTrajectory
59903d14d244 added functions to undistort trajectories from inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 473
diff changeset
443
472
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
444 def computeInverseMapping(originalImageSize, map1, map2):
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
445 '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
446 invMap1 = -ones(originalImageSize)
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
447 invMap2 = -ones(originalImageSize)
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
448 for x in range(0,originalImageSize[1]):
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
449 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
450 res = undistortedCoordinates(x,y, map1, map2)
472
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
451 if not isnan(res).any():
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
452 invMap1[y,x] = res[0]
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
453 invMap2[y,x] = res[1]
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
454 return invMap1, invMap2
a50c026fdf14 functions to compute inverse mapping
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 468
diff changeset
455
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
456 def intrinsicCameraCalibration(path, checkerBoardSize=[6,7], secondPassSearch=False, display=False, fixK2 = True, fixK3 = True, zeroTangent = True):
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
457 ''' Camera calibration searches through all the images (jpg or png) located
897
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
458 in _path_ for matches to a checkerboard pattern of size checkboardSize.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
459 These images should all be of the same camera with the same resolution.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
460
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
461 For best results, use an asymetric board and ensure that the image has
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
462 very high contrast, including the background.
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
463
897
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
464 cherckerBoardSize is the number of internal corners (7x10 squares have 6x9 internal corners)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
465
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
466 The code below is based off of:
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
467 https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
468 Modified by Paul St-Aubin
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 895
diff changeset
469 '''
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
470 import glob, os
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
471
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
472 # termination criteria
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
473 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
474
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
475 # 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
476 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
477 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
478
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
479 # 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
480 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
481 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
482
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
483 ## 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
484 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
485 for fname in images:
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
486 img = cv2.imread(fname)
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
487 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
488
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
489 # Find the chess board corners
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
490 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
491
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
492 # 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
493 if ret:
774
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 772
diff changeset
494 print('Found pattern in '+fname)
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
495
772
e92a96f2bdd3 minor bug corrections
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 769
diff changeset
496 if secondPassSearch:
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
497 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
498
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
499 objpoints.append(objp)
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
500 imgpoints.append(corners)
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
501
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
502 # Draw and display the corners
772
e92a96f2bdd3 minor bug corrections
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 769
diff changeset
503 if display:
950
c03d2c0a4c04 corrected bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 947
diff changeset
504 cv2.drawChessboardCorners(img, (checkerBoardSize[1],checkerBoardSize[0]), corners, ret)
772
e92a96f2bdd3 minor bug corrections
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 769
diff changeset
505 if img is not None:
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
506 cv2.imshow('img',img)
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
507 cv2.waitKey(0)
774
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 772
diff changeset
508 else:
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 772
diff changeset
509 print('Pattern not found in '+fname)
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
510 ## 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
511 cv2.destroyAllWindows()
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
512 if len(objpoints) == 0 or len(imgpoints) == 0:
895
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
513 return None
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
514 try:
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
515 flags = 0
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
516 if fixK2:
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
517 flags += cv2.CALIB_FIX_K2
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
518 if fixK3:
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
519 flags += cv2.CALIB_FIX_K3
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
520 if zeroTangent:
993
e8eabef7857c update to OpenCV3 for python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 989
diff changeset
521 flags += cv2.CALIB_ZERO_TANGENT_DIST
926
dbd81710d515 new feature tracking in image space with point undistortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 924
diff changeset
522 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None, flags = flags)
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
523 except NameError:
895
739acd338cc0 added script to extract camera info from tacal file by Lund University (T analyst)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 894
diff changeset
524 return None
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
525 savetxt('intrinsic-camera.txt', camera_matrix)
978
184f1dd307f9 corrected print and exception statements for Python 3
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 969
diff changeset
526 print('error: {}'.format(ret))
544
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
527 return camera_matrix, dist_coeffs
749672171789 added function to calibrate a camera intrinsic parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 538
diff changeset
528
545
9816fab353f3 added function to undistort image, mostly for checking camera calibration results
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 544
diff changeset
529 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
530 '''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
531 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
532 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
533 [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
534 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
535
1205
3905b393ade0 kitti loading code seems to be working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1174
diff changeset
536 def cartesian2Homogeneous(m):
3905b393ade0 kitti loading code seems to be working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1174
diff changeset
537 'Transforms n x m matrix to n x (m+1) by adding last column of 1s'
1253
ef68d4ba7dae added loading ego vehicle in kitti 2D format and method to plot outline
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1245
diff changeset
538 if len(m.shape) == 2:
ef68d4ba7dae added loading ego vehicle in kitti 2D format and method to plot outline
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1245
diff changeset
539 homoM = ones((m.shape[0], m.shape[1]+1))
ef68d4ba7dae added loading ego vehicle in kitti 2D format and method to plot outline
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1245
diff changeset
540 else:
ef68d4ba7dae added loading ego vehicle in kitti 2D format and method to plot outline
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1245
diff changeset
541 homoM = ones((1, len(m)+1))
1205
3905b393ade0 kitti loading code seems to be working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1174
diff changeset
542 homoM[:,:-1] = m
3905b393ade0 kitti loading code seems to be working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1174
diff changeset
543 return homoM
3905b393ade0 kitti loading code seems to be working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1174
diff changeset
544
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
545 def homographyProject(points, homography, output3D = False):
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
546 '''Returns the coordinates of the points (2xN array) projected through homography'''
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
547 if points.shape[0] != 2:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
548 raise Exception('points of dimension {}'.format(points.shape))
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
549
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
550 if homography is not None and homography.size>0:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
551 if output3D:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
552 outputDim = 3
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
553 else:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
554 outputDim = 2
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
555 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
556 prod = dot(homography, augmentedPoints)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
557 return prod[:outputDim,:]/prod[2]
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
558 elif output3D:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
559 return append(points,[[1]*points.shape[1]], 0) # 3xN
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
560 else:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
561 return points
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
562
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
563 def imageToWorldProject(points, intrinsicCameraMatrix = None, distortionCoefficients = None, homography = None):
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
564 '''Projects points (2xN array) from image (video) space to world space
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
565 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
566 2. through homograph projection (from ideal point (no camera) to world)'''
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
567 if points.shape[0] != 2:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
568 raise Exception('points of dimension {}'.format(points.shape))
1168
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
569 if points.dtype != dtype('float64'):
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
570 points = array(points, dtype = dtype('float64'))
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
571
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
572 if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
573 undistortedPoints = cv2.undistortPoints(points.T.reshape(1,points.shape[1], 2), intrinsicCameraMatrix, distortionCoefficients).reshape(-1,2)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
574 return homographyProject(undistortedPoints.T, homography)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
575 else:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
576 return homographyProject(points, homography)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
577
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
578 def worldToImageProject(points, intrinsicCameraMatrix = None, distortionCoefficients = None, homography = None):
1174
2f89dc3d99e5 clarification of worldToImageProject method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1168
diff changeset
579 '''Projects points (2xN array) from world space to image (video) space
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
580 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients
1174
2f89dc3d99e5 clarification of worldToImageProject method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1168
diff changeset
581 2. through homograph projection (from ideal point (no camera) to world)
2f89dc3d99e5 clarification of worldToImageProject method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1168
diff changeset
582
2f89dc3d99e5 clarification of worldToImageProject method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1168
diff changeset
583 Warning: this usually means to invert the homography if computer from image (ideal point) to world space, eg using numpy.linalg.inv'''
932
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
584 if points.shape[0] != 2:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
585 raise Exception('points of dimension {}'.format(points.shape))
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
586
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
587 if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
588 projected3D = homographyProject(points, homography, True)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
589 projected, jacobian = cv2.projectPoints(projected3D.T, (0.,0.,0.), (0.,0.,0.), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
590 return projected.reshape(-1,2).T
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
591 else:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
592 return homographyProject(points, homography)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
593
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
594 def newCameraProject(points, newCameraMatrix):
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
595 '''Projects points (2xN array) as if seen by camera
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
596 (or reverse by inverting the camera matrix)'''
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
597 if points.shape[0] != 2:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
598 raise Exception('points of dimension {}'.format(points.shape))
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
599
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
600 if newCameraMatrix is not None:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
601 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
602 projected = dot(newCameraMatrix, augmentedPoints)
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
603 return projected[:2,:]
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
604 else:
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
605 return points
66f382852e61 added new projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 931
diff changeset
606
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
607 if opencvAvailable:
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
608 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
609 '''Computes the translation of img2 with respect to img1
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
610 (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
611 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
612
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
613 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom, other non linear distortion)'''
100
2a3cafcf5faf added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 99
diff changeset
614
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
615 nextPoints = array([])
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
616 (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
617 # 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
618 delta = []
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
619 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
620 if status[k] == 1:
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
621 dp = p2-p1
769
dfdb2a3722cc moved import statements together
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 766
diff changeset
622 d = npsum(dp**2)
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
623 if d < maxTranslation2:
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
624 delta.append(dp)
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
625 if len(delta) >= minNMatches:
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
626 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
627 else:
156
2eef5620c0b3 added key values for opencv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 154
diff changeset
628 print(dp)
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
629 return None
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
630
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
631 if skimageAvailable:
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
632 from skimage.feature import hog
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
633 from skimage import color, transform
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
634
959
4f32d82ca390 corrected error due to change in Hog (scikit image)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 958
diff changeset
635 def HOG(image, rescaleSize = (64, 64), orientations = 9, pixelsPerCell = (8,8), cellsPerBlock = (2,2), blockNorm = 'L1', visualize = False, transformSqrt = False):
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
636 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
637 inputImg = transform.resize(bwImg, rescaleSize)
959
4f32d82ca390 corrected error due to change in Hog (scikit image)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 958
diff changeset
638 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt, True)
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
639 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
640 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
641 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
642 figure()
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
643 subplot(1,2,1)
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
644 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
645 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
646 imshow(hogViz)
680
da1352b89d02 classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 673
diff changeset
647 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
648
959
4f32d82ca390 corrected error due to change in Hog (scikit image)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 958
diff changeset
649 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64,64), orientations = 9, pixelsPerCell = (8,8), blockNorm = 'L1', cellsPerBlock = (2, 2), visualize = False, transformSqrt = False):
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
650 inputData = []
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
651 for filename in listdir(imageDirectory):
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
652 img = imread(imageDirectory+filename)
959
4f32d82ca390 corrected error due to change in Hog (scikit image)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 958
diff changeset
653 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt)
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
654 inputData.append(features)
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
655
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
656 nImages = len(inputData)
1168
d71a4d174b1a corrected potential bug with dtype in image to world projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 1158
diff changeset
657 return array(inputData, dtype = dtype('float32')), array([classLabel]*nImages)
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
658
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
659
807
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
660 #########################
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
661 # running tests
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
662 #########################
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
663
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
664 if __name__ == "__main__":
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
665 import doctest
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
666 import unittest
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
667 suite = doctest.DocFileSuite('tests/cvutils.txt')
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
668 #suite = doctest.DocTestSuite()
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
669 unittest.TextTestRunner().run(suite)
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
670 #doctest.testmod()
52aa03260f03 reversed all code to OpenCV 2.4.13
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 799
diff changeset
671 #doctest.testfile("example.txt")