comparison python/cvutils.py @ 824:28526917a583

merged
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 27 Jun 2016 16:19:34 -0400
parents 26daf35180ad
children 2faabcbde2c4
comparison
equal deleted inserted replaced
823:f6790357f53b 824:28526917a583
144 newCameraMatrix = deepcopy(intrinsicCameraMatrix) 144 newCameraMatrix = deepcopy(intrinsicCameraMatrix)
145 newCameraMatrix[0,2] = newImgSize[0]/2. 145 newCameraMatrix[0,2] = newImgSize[0]/2.
146 newCameraMatrix[1,2] = newImgSize[1]/2. 146 newCameraMatrix[1,2] = newImgSize[1]/2.
147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) 147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
148 148
149 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): 149 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
150 '''Plays the video''' 150 '''Plays the video(s)'''
151 windowName = 'frame' 151 if len(filenames) == 0:
152 print('Empty filename list')
153 return
154 if windowNames is None:
155 windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))]
152 wait = 5 156 wait = 5
153 if rescale == 1.: 157 if rescale == 1.:
154 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 158 for windowName in windowNames:
159 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
155 if frameRate > 0: 160 if frameRate > 0:
156 wait = int(round(1000./frameRate)) 161 wait = int(round(1000./frameRate))
157 if interactive: 162 if interactive:
158 wait = 0 163 wait = 0
159 capture = cv2.VideoCapture(filename) 164 captures = [cv2.VideoCapture(fn) for fn in filenames]
160 if capture.isOpened(): 165 if array([cap.isOpened() for cap in captures]).all():
161 key = -1 166 key = -1
162 ret = True 167 ret = True
163 frameNum = firstFrameNum 168 nFramesShown = 0
164 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) 169 if firstFrameNums is not None:
170 for i in xrange(len(captures)):
171 captures[i].set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i])
165 while ret and not quitKey(key): 172 while ret and not quitKey(key):
166 ret, img = capture.read() 173 rets = []
167 if ret: 174 images = []
175 for cap in captures:
176 ret, img = cap.read()
177 rets.append(ret)
178 images.append(img)
179 if array(rets).all():
168 if printFrames: 180 if printFrames:
169 print('frame {0}'.format(frameNum)) 181 print('frame shown {0}'.format(nFramesShown))
170 if text is not None: 182 for i in xrange(len(filenames)):
171 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 183 if text is not None:
172 cv2.imshow('frame', img)#cvImshow(windowName, img, rescale) 184 cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
185 cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img)
173 key = cv2.waitKey(wait) 186 key = cv2.waitKey(wait)
174 if saveKey(key): 187 if saveKey(key):
175 cv2.imwrite('image-{}.png'.format(frameNum), img) 188 cv2.imwrite('image-{}.png'.format(frameNum), img)
176 frameNum += step 189 nFramesShown += step
177 if step > 1: 190 if step > 1:
178 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) 191 for i in xrange(len(captures)):
192 captures.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown)
179 cv2.destroyAllWindows() 193 cv2.destroyAllWindows()
180 else: 194 else:
181 print('Video capture for {} failed'.format(filename)) 195 print('Video captures for {} failed'.format(filenames))
182 196
183 def infoVideo(filename): 197 def infoVideo(filename):
184 '''Provides all available info on video ''' 198 '''Provides all available info on video '''
185 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", 199 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format",
186 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", 200 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)",