comparison python/cvutils.py @ 708:a37c565f4b68

merged dev
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jul 2015 14:17:44 -0400
parents 94b291a5f933
children 35bc5e30a53f
comparison
equal deleted inserted replaced
707:7efa36b9bcfd 708:a37c565f4b68
15 except ImportError: 15 except ImportError:
16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') 16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)')
17 skimageAvailable = False 17 skimageAvailable = False
18 18
19 from sys import stdout 19 from sys import stdout
20 from numpy import dot, array, append 20 from numpy import dot, array, append, float32
21 21
22 #import aggdraw # agg on top of PIL (antialiased drawing) 22 #import aggdraw # agg on top of PIL (antialiased drawing)
23 23
24 24
25 cvRed = (0,0,255) 25 cvRed = (0,0,255)
124 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) 124 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
125 125
126 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): 126 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
127 '''Plays the video''' 127 '''Plays the video'''
128 windowName = 'frame' 128 windowName = 'frame'
129 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 129 if rescale == 1.:
130 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
130 wait = 5 131 wait = 5
131 if frameRate > 0: 132 if frameRate > 0:
132 wait = int(round(1000./frameRate)) 133 wait = int(round(1000./frameRate))
133 if interactive: 134 if interactive:
134 wait = 0 135 wait = 0
198 '''Returns nFrames images from the video sequence''' 199 '''Returns nFrames images from the video sequence'''
199 from math import floor, log10 200 from math import floor, log10
200 images = [] 201 images = []
201 capture = cv2.VideoCapture(videoFilename) 202 capture = cv2.VideoCapture(videoFilename)
202 if capture.isOpened(): 203 if capture.isOpened():
203 nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1 204 rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
205 if rawCount < 0:
206 rawCount = firstFrameNum+nFrames+1
207 nDigits = int(floor(log10(rawCount)))+1
204 ret = False 208 ret = False
205 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) 209 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
206 imgNum = 0 210 imgNum = 0
207 while imgNum<nFrames: 211 while imgNum<nFrames:
208 ret, img = capture.read() 212 ret, img = capture.read()
230 return fps 234 return fps
231 else: 235 else:
232 print('Video capture for {} failed'.format(videoFilename)) 236 print('Video capture for {} failed'.format(videoFilename))
233 return None 237 return None
234 238
235 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, pixelThreshold = 800): 239 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800):
236 'Computes the bounding box of object at frameNum' 240 'Computes the bounding box of object at frameNum'
237 x = [] 241 x = []
238 y = [] 242 y = []
239 if obj.hasFeatures(): 243 if obj.hasFeatures():
240 for f in obj.getFeatures(): 244 for f in obj.getFeatures():
251 a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm)) 255 a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm))
252 yCropMin = int(max(0, .5 * (ymin + ymax - a))) 256 yCropMin = int(max(0, .5 * (ymin + ymax - a)))
253 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) 257 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a)))
254 xCropMin = int(max(0, .5 * (xmin + xmax - a))) 258 xCropMin = int(max(0, .5 * (xmin + xmax - a)))
255 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) 259 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a)))
256 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > pixelThreshold: 260 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels:
257 croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax] 261 croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax]
258 else: 262 else:
259 croppedImg = [] 263 croppedImg = None
260 return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax 264 return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax
261 265
262 266
263 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): 267 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.):
264 '''Displays the objects overlaid frame by frame over the video ''' 268 '''Displays the objects overlaid frame by frame over the video '''
268 capture = cv2.VideoCapture(videoFilename) 272 capture = cv2.VideoCapture(videoFilename)
269 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) 273 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
270 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) 274 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
271 275
272 windowName = 'frame' 276 windowName = 'frame'
273 #cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 277 if rescale == 1.:
278 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
274 279
275 if undistort: # setup undistortion 280 if undistort: # setup undistortion
276 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) 281 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
277 if capture.isOpened(): 282 if capture.isOpened():
278 key = -1 283 key = -1
536 else: 541 else:
537 print(dp) 542 print(dp)
538 return None 543 return None
539 544
540 if skimageAvailable: 545 if skimageAvailable:
546 from skimage.feature import hog
547 from skimage import color, transform
548
541 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): 549 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
542 from skimage.feature import hog
543 from skimage import color, transform
544
545 bwImg = color.rgb2gray(image) 550 bwImg = color.rgb2gray(image)
546 inputImg = transform.resize(bwImg, rescaleSize) 551 inputImg = transform.resize(bwImg, rescaleSize)
547 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) 552 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
548 if visualize: 553 if visualize:
549 from matplotlib.pyplot import imshow, figure, subplot 554 from matplotlib.pyplot import imshow, figure, subplot
550 hogViz = features[1] 555 hogViz = features[1]
551 features = features[0] 556 features = features[0]
552 figure() 557 figure()
553 subplot(1,2,1) 558 subplot(1,2,1)
554 imshow(img) 559 imshow(inputImg)
555 subplot(1,2,2) 560 subplot(1,2,2)
556 imshow(hogViz) 561 imshow(hogViz)
557 return features 562 return float32(features)
558 563
559 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): 564 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
560 from os import listdir 565 from os import listdir
561 from numpy import float32
562 from matplotlib.pyplot import imread 566 from matplotlib.pyplot import imread
563 567
564 inputData = [] 568 inputData = []
565 for filename in listdir(imageDirectory): 569 for filename in listdir(imageDirectory):
566 img = imread(imageDirectory+filename) 570 img = imread(imageDirectory+filename)