Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 216:51acf43e421a
modified the function to read and save images from a movie
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 21 Jun 2012 00:57:26 -0400 |
| parents | 966c2cd2bd9f |
| children | b5772df11b37 |
comparison
equal
deleted
inserted
replaced
| 215:5e2983b05d4e | 216:51acf43e421a |
|---|---|
| 101 ret, img = capture.read() | 101 ret, img = capture.read() |
| 102 if ret: | 102 if ret: |
| 103 cv2.imshow('frame', img) | 103 cv2.imshow('frame', img) |
| 104 key = cv2.waitKey(5) | 104 key = cv2.waitKey(5) |
| 105 | 105 |
| 106 def getImagesFromVideo(filename, nImages = 1): | 106 def getImagesFromVideo(filename, nImages = 1, saveImage = False): |
| 107 '''Returns nImages images from the video sequence''' | 107 '''Returns nImages images from the video sequence''' |
| 108 images = [] | 108 images = [] |
| 109 capture = cv2.VideoCapture(filename) | 109 capture = cv2.VideoCapture(filename) |
| 110 if capture.isOpened(): | 110 if capture.isOpened(): |
| 111 ret = False | 111 ret = False |
| 112 while len(images)<nImages: | 112 numImg = 0 |
| 113 while numImg<nImages: | |
| 113 ret, img = capture.read() | 114 ret, img = capture.read() |
| 114 while not ret: | 115 i = 0 |
| 116 while not ret and i<10: | |
| 115 ret, img = capture.read() | 117 ret, img = capture.read() |
| 118 i += 1 | |
| 116 if img.size>0: | 119 if img.size>0: |
| 117 images.append(img) | 120 numImg +=1 |
| 121 if saveImage: | |
| 122 cv2.imwrite('image{0:04d}.png'.format(numImg), img) | |
| 123 else: | |
| 124 images.append(img) | |
| 118 return images | 125 return images |
| 119 | 126 |
| 120 def displayTrajectories(videoFilename, objects, homography = None): | 127 def displayTrajectories(videoFilename, objects, homography = None): |
| 121 '''Displays the objects overlaid frame by frame over the video ''' | 128 '''Displays the objects overlaid frame by frame over the video ''' |
| 122 capture = cv2.VideoCapture(videoFilename) | 129 capture = cv2.VideoCapture(videoFilename) |
