Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 365:22ddb8f85495
added function to create HOG training set
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 12 Jul 2013 11:28:47 -0400 |
parents | 5f75d6c23ed5 |
children | 387cc0142211 |
comparison
equal
deleted
inserted
replaced
364:a50a69e04c2a | 365:22ddb8f85495 |
---|---|
2 '''Image/Video utilities''' | 2 '''Image/Video utilities''' |
3 | 3 |
4 import Image, ImageDraw # PIL | 4 import Image, ImageDraw # PIL |
5 try: | 5 try: |
6 import cv2 | 6 import cv2 |
7 opencvExists = True | 7 opencvAvailable = True |
8 except ImportError: | 8 except ImportError: |
9 print('OpenCV library could not be loaded') | 9 print('OpenCV library could not be loaded') |
10 opencvExists = False | 10 opencvAvailable = False |
11 try: | |
12 import skimage | |
13 skimageAvailable = True | |
14 except ImportError: | |
15 print('Scikit-image library could not be loaded') | |
16 skimageAvailable = False | |
17 | |
11 from sys import stdout | 18 from sys import stdout |
12 | 19 |
13 import utils | 20 import utils |
14 | 21 |
15 #import aggdraw # agg on top of PIL (antialiased drawing) | 22 #import aggdraw # agg on top of PIL (antialiased drawing) |
68 for i in xrange(cvmat.rows): | 75 for i in xrange(cvmat.rows): |
69 for j in xrange(cvmat.cols): | 76 for j in xrange(cvmat.cols): |
70 a[i,j] = cvmat[i,j] | 77 a[i,j] = cvmat[i,j] |
71 return a | 78 return a |
72 | 79 |
73 if opencvExists: | 80 if opencvAvailable: |
74 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): | 81 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): |
75 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' | 82 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
76 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) | 83 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) |
77 return H | 84 return H |
78 | 85 |
207 from numpy.linalg.linalg import inv | 214 from numpy.linalg.linalg import inv |
208 invH = inv(homography) | 215 invH = inv(homography) |
209 invH /= invH[2,2] | 216 invH /= invH[2,2] |
210 return invH | 217 return invH |
211 | 218 |
212 if opencvExists: | 219 if opencvAvailable: |
213 def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)): | 220 def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)): |
214 '''Computes the translation of img2 with respect to img1 | 221 '''Computes the translation of img2 with respect to img1 |
215 (loaded using OpenCV as numpy arrays) | 222 (loaded using OpenCV as numpy arrays) |
216 img1Points are used to compute the translation | 223 img1Points are used to compute the translation |
217 | 224 |
233 if len(delta) >= minNMatches: | 240 if len(delta) >= minNMatches: |
234 return median(delta, axis=0) | 241 return median(delta, axis=0) |
235 else: | 242 else: |
236 print(dp) | 243 print(dp) |
237 return None | 244 return None |
245 | |
246 if skimageAvailable: | |
247 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(3, 3), visualize=False, normalize=False): | |
248 from os import listdir | |
249 from matplotlib.pyplot import imread, imshow, figure, subplot | |
250 from skimage.feature import hog | |
251 from skimage import color, transform | |
252 from numpy import array, float32 | |
253 | |
254 inputData = [] | |
255 for filename in listdir(imageDirectory): | |
256 img = imread(imageDirectory+filename) | |
257 bwImg = color.rgb2gray(img) | |
258 inputImg = transform.resize(bwImg, rescaleSize) | |
259 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) | |
260 if visualize: | |
261 hogViz = features[1] | |
262 features = features[0] | |
263 figure() | |
264 subplot(1,2,1) | |
265 imshow(img) | |
266 subplot(1,2,2) | |
267 imshow(hogViz) | |
268 inputData.append(features) | |
269 | |
270 nImages = len(inputData) | |
271 return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32) | |
272 | |
273 |