Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 993:e8eabef7857c
update to OpenCV3 for python
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 16 May 2018 21:06:52 -0400 |
parents | 132d84ce9f0c |
children | 933670761a57 |
comparison
equal
deleted
inserted
replaced
992:2cd1ce245024 | 993:e8eabef7857c |
---|---|
91 if opencvAvailable: | 91 if opencvAvailable: |
92 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=3.0): | 92 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=3.0): |
93 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' | 93 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
94 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) | 94 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) |
95 return H | 95 return H |
96 | |
97 def arrayToCvMat(a, t = cv2.CV_64FC1): | |
98 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' | |
99 print('Deprecated, use new interface') | |
100 cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t) | |
101 for i in range(cvmat.rows): | |
102 for j in range(cvmat.cols): | |
103 cvmat[i,j] = a[i,j] | |
104 return cvmat | |
105 | 96 |
106 def cvPlot(img, positions, color, lastCoordinate = None, **kwargs): | 97 def cvPlot(img, positions, color, lastCoordinate = None, **kwargs): |
107 if lastCoordinate is None: | 98 if lastCoordinate is None: |
108 last = positions.length()-1 | 99 last = positions.length()-1 |
109 elif lastCoordinate >=0: | 100 elif lastCoordinate >=0: |
150 key = -1 | 141 key = -1 |
151 ret = True | 142 ret = True |
152 nFramesShown = 0 | 143 nFramesShown = 0 |
153 if firstFrameNums is not None: | 144 if firstFrameNums is not None: |
154 for i in xrange(len(captures)): | 145 for i in xrange(len(captures)): |
155 captures[i].set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i]) | 146 captures[i].set(cv2.PROP_POS_FRAMES, firstFrameNums[i]) |
156 while ret and not quitKey(key): | 147 while ret and not quitKey(key): |
157 rets = [] | 148 rets = [] |
158 images = [] | 149 images = [] |
159 for cap in captures: | 150 for cap in captures: |
160 ret, img = cap.read() | 151 ret, img = cap.read() |
172 if saveKey(key): | 163 if saveKey(key): |
173 cv2.imwrite('image-{}.png'.format(frameNum), img) | 164 cv2.imwrite('image-{}.png'.format(frameNum), img) |
174 nFramesShown += step | 165 nFramesShown += step |
175 if step > 1: | 166 if step > 1: |
176 for i in xrange(len(captures)): | 167 for i in xrange(len(captures)): |
177 captures[i].set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown) | 168 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown) |
178 cv2.destroyAllWindows() | 169 cv2.destroyAllWindows() |
179 else: | 170 else: |
180 print('Video captures for {} failed'.format(filenames)) | 171 print('Video captures for {} failed'.format(filenames)) |
181 | 172 |
182 def infoVideo(filename): | 173 def infoVideo(filename): |
183 '''Provides all available info on video ''' | 174 '''Provides all available info on video ''' |
184 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", | 175 cvPropertyNames = {cv2.CAP_PROP_FORMAT: "format", |
185 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", | 176 cv2.CAP_PROP_FOURCC: "codec (fourcc)", |
186 cv2.cv.CV_CAP_PROP_FPS: "fps", | 177 cv2.CAP_PROP_FPS: "fps", |
187 cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames", | 178 cv2.CAP_PROP_FRAME_COUNT: "number of frames", |
188 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh", | 179 cv2.CAP_PROP_FRAME_HEIGHT: "heigh", |
189 cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width", | 180 cv2.CAP_PROP_FRAME_WIDTH: "width", |
190 cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification", | 181 cv2.CAP_PROP_RECTIFICATION: "rectification", |
191 cv2.cv.CV_CAP_PROP_SATURATION: "saturation"} | 182 cv2.CAP_PROP_SATURATION: "saturation"} |
192 capture = cv2.VideoCapture(filename) | 183 capture = cv2.VideoCapture(filename) |
193 videoProperties = {} | 184 videoProperties = {} |
194 if capture.isOpened(): | 185 if capture.isOpened(): |
195 for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS | 186 for cvprop in [#cv2.CAP_PROP_BRIGHTNESS |
196 #cv2.cv.CV_CAP_PROP_CONTRAST | 187 #cv2.CAP_PROP_CONTRAST |
197 #cv2.cv.CV_CAP_PROP_CONVERT_RGB | 188 #cv2.CAP_PROP_CONVERT_RGB |
198 #cv2.cv.CV_CAP_PROP_EXPOSURE | 189 #cv2.CAP_PROP_EXPOSURE |
199 cv2.cv.CV_CAP_PROP_FORMAT, | 190 cv2.CAP_PROP_FORMAT, |
200 cv2.cv.CV_CAP_PROP_FOURCC, | 191 cv2.CAP_PROP_FOURCC, |
201 cv2.cv.CV_CAP_PROP_FPS, | 192 cv2.CAP_PROP_FPS, |
202 cv2.cv.CV_CAP_PROP_FRAME_COUNT, | 193 cv2.CAP_PROP_FRAME_COUNT, |
203 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, | 194 cv2.CAP_PROP_FRAME_HEIGHT, |
204 cv2.cv.CV_CAP_PROP_FRAME_WIDTH, | 195 cv2.CAP_PROP_FRAME_WIDTH, |
205 #cv2.cv.CV_CAP_PROP_GAIN, | 196 #cv2.CAP_PROP_GAIN, |
206 #cv2.cv.CV_CAP_PROP_HUE | 197 #cv2.CAP_PROP_HUE |
207 #cv2.cv.CV_CAP_PROP_MODE | 198 #cv2.CAP_PROP_MODE |
208 #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO | 199 #cv2.CAP_PROP_POS_AVI_RATIO |
209 #cv2.cv.CV_CAP_PROP_POS_FRAMES | 200 #cv2.CAP_PROP_POS_FRAMES |
210 #cv2.cv.CV_CAP_PROP_POS_MSEC | 201 #cv2.CAP_PROP_POS_MSEC |
211 #cv2.cv.CV_CAP_PROP_RECTIFICATION, | 202 #cv2.CAP_PROP_RECTIFICATION, |
212 #cv2.cv.CV_CAP_PROP_SATURATION | 203 #cv2.CAP_PROP_SATURATION |
213 ]: | 204 ]: |
214 prop = capture.get(cvprop) | 205 prop = capture.get(cvprop) |
215 if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0: | 206 if cvprop == cv2.CAP_PROP_FOURCC and prop > 0: |
216 prop = int2FOURCC(int(prop)) | 207 prop = int2FOURCC(int(prop)) |
217 videoProperties[cvPropertyNames[cvprop]] = prop | 208 videoProperties[cvPropertyNames[cvprop]] = prop |
218 else: | 209 else: |
219 print('Video capture for {} failed'.format(filename)) | 210 print('Video capture for {} failed'.format(filename)) |
220 return videoProperties | 211 return videoProperties |
222 def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'): | 213 def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'): |
223 '''Returns nFrames images from the video sequence''' | 214 '''Returns nFrames images from the video sequence''' |
224 images = [] | 215 images = [] |
225 capture = cv2.VideoCapture(videoFilename) | 216 capture = cv2.VideoCapture(videoFilename) |
226 if capture.isOpened(): | 217 if capture.isOpened(): |
227 rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) | 218 rawCount = capture.get(cv2.CAP_PROP_FRAME_COUNT) |
228 if rawCount < 0: | 219 if rawCount < 0: |
229 rawCount = lastFrameNum+1 | 220 rawCount = lastFrameNum+1 |
230 nDigits = int(floor(log10(rawCount)))+1 | 221 nDigits = int(floor(log10(rawCount)))+1 |
231 ret = False | 222 ret = False |
232 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 223 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) |
233 frameNum = firstFrameNum | 224 frameNum = firstFrameNum |
234 while frameNum<lastFrameNum and frameNum<rawCount: | 225 while frameNum<lastFrameNum and frameNum<rawCount: |
235 ret, img = capture.read() | 226 ret, img = capture.read() |
236 i = 0 | 227 i = 0 |
237 while not ret and i<10: | 228 while not ret and i<10: |
243 cv2.imwrite(outputPrefix+frameNumStr+'.png', img) | 234 cv2.imwrite(outputPrefix+frameNumStr+'.png', img) |
244 else: | 235 else: |
245 images.append(img) | 236 images.append(img) |
246 frameNum +=step | 237 frameNum +=step |
247 if step > 1: | 238 if step > 1: |
248 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 239 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) |
249 capture.release() | 240 capture.release() |
250 else: | 241 else: |
251 print('Video capture for {} failed'.format(videoFilename)) | 242 print('Video capture for {} failed'.format(videoFilename)) |
252 return images | 243 return images |
253 | 244 |
254 def getFPS(videoFilename): | 245 def getFPS(videoFilename): |
255 capture = cv2.VideoCapture(videoFilename) | 246 capture = cv2.VideoCapture(videoFilename) |
256 if capture.isOpened(): | 247 if capture.isOpened(): |
257 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) | 248 fps = capture.get(cv2.CAP_PROP_FPS) |
258 capture.release() | 249 capture.release() |
259 return fps | 250 return fps |
260 else: | 251 else: |
261 print('Video capture for {} failed'.format(videoFilename)) | 252 print('Video capture for {} failed'.format(videoFilename)) |
262 return None | 253 return None |
298 colorType = 'colorblind' | 289 colorType = 'colorblind' |
299 else: | 290 else: |
300 colorType = 'default' | 291 colorType = 'default' |
301 | 292 |
302 capture = cv2.VideoCapture(videoFilename) | 293 capture = cv2.VideoCapture(videoFilename) |
303 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 294 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) |
304 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 295 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
305 | 296 |
306 windowName = 'frame' | 297 windowName = 'frame' |
307 if rescale == 1.: | 298 if rescale == 1.: |
308 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) | 299 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
309 | 300 |
311 [map1, map2], newCameraMatrix = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) | 302 [map1, map2], newCameraMatrix = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) |
312 if capture.isOpened(): | 303 if capture.isOpened(): |
313 key = -1 | 304 key = -1 |
314 ret = True | 305 ret = True |
315 frameNum = firstFrameNum | 306 frameNum = firstFrameNum |
316 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 307 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) |
317 if lastFrameNumArg is None: | 308 if lastFrameNumArg is None: |
318 lastFrameNum = float("inf") | 309 lastFrameNum = float("inf") |
319 else: | 310 else: |
320 lastFrameNum = lastFrameNumArg | 311 lastFrameNum = lastFrameNumArg |
321 if nZerosFilenameArg is None: | 312 if nZerosFilenameArg is None: |
372 key = cv2.waitKey() | 363 key = cv2.waitKey() |
373 if saveAllImages or saveKey(key): | 364 if saveAllImages or saveKey(key): |
374 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) | 365 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) |
375 frameNum += nFramesStep | 366 frameNum += nFramesStep |
376 if nFramesStep > 1: | 367 if nFramesStep > 1: |
377 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 368 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) |
378 cv2.destroyAllWindows() | 369 cv2.destroyAllWindows() |
379 else: | 370 else: |
380 print('Cannot load file ' + videoFilename) | 371 print('Cannot load file ' + videoFilename) |
381 | 372 |
382 def computeHomographyFromPDTV(camera): | 373 def computeHomographyFromPDTV(camera): |
494 if len(objpoints) == 0 or len(imgpoints) == 0: | 485 if len(objpoints) == 0 or len(imgpoints) == 0: |
495 return None | 486 return None |
496 try: | 487 try: |
497 flags = 0 | 488 flags = 0 |
498 if fixK2: | 489 if fixK2: |
499 flags += cv2.cv.CV_CALIB_FIX_K2 | 490 flags += cv2.CALIB_FIX_K2 |
500 if fixK3: | 491 if fixK3: |
501 flags += cv2.cv.CV_CALIB_FIX_K3 | 492 flags += cv2.CALIB_FIX_K3 |
502 if zeroTangent: | 493 if zeroTangent: |
503 flags += cv2.cv.CV_CALIB_ZERO_TANGENT_DIST | 494 flags += cv2.CALIB_ZERO_TANGENT_DIST |
504 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None, flags = flags) | 495 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None, flags = flags) |
505 except NameError: | 496 except NameError: |
506 return None | 497 return None |
507 savetxt('intrinsic-camera.txt', camera_matrix) | 498 savetxt('intrinsic-camera.txt', camera_matrix) |
508 print('error: {}'.format(ret)) | 499 print('error: {}'.format(ret)) |