comparison python/cvutils.py @ 396:167f6ec44ec5

cleaned function to save images
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 28 Jul 2013 16:30:11 -0400
parents eaf7765221d9
children 3399bd48cb40
comparison
equal deleted inserted replaced
395:6fba1ab040f1 396:167f6ec44ec5
133 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 133 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
134 cvImshow('frame', img, rescale) 134 cvImshow('frame', img, rescale)
135 key = cv2.waitKey(wait) 135 key = cv2.waitKey(wait)
136 cv2.destroyAllWindows() 136 cv2.destroyAllWindows()
137 137
138 def getImagesFromVideo(filename, nImages = 1, saveImage = False): 138 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'):
139 '''Returns nImages images from the video sequence''' 139 '''Returns nFrames images from the video sequence'''
140 from math import floor, log10
140 images = [] 141 images = []
141 capture = cv2.VideoCapture(filename) 142 capture = cv2.VideoCapture(videoFilename)
143 nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1
142 if capture.isOpened(): 144 if capture.isOpened():
143 ret = False 145 ret = False
144 numImg = 0 146 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
145 while numImg<nImages: 147 imgNum = 0
148 while imgNum<nFrames:
146 ret, img = capture.read() 149 ret, img = capture.read()
147 i = 0 150 i = 0
148 while not ret and i<10: 151 while not ret and i<10:
149 ret, img = capture.read() 152 ret, img = capture.read()
150 i += 1 153 i += 1
151 if img.size>0: 154 if img.size>0:
152 numImg +=1
153 if saveImage: 155 if saveImage:
154 cv2.imwrite('image{0:04d}.png'.format(numImg), img) 156 imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits))
157 cv2.imwrite(outputPrefix+imgNumStr+'.png', img)
155 else: 158 else:
156 images.append(img) 159 images.append(img)
160 imgNum +=1
157 return images 161 return images
158 162
159 def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.): 163 def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
160 '''Displays the objects overlaid frame by frame over the video ''' 164 '''Displays the objects overlaid frame by frame over the video '''
161 capture = cv2.VideoCapture(videoFilename) 165 capture = cv2.VideoCapture(videoFilename)