Mercurial Hosting > traffic-intelligence
changeset 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 | a50a69e04c2a |
children | 90bdabc06e9f |
files | python/cvutils.py |
diffstat | 1 files changed, 40 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Fri Jul 12 02:29:49 2013 -0400 +++ b/python/cvutils.py Fri Jul 12 11:28:47 2013 -0400 @@ -4,10 +4,17 @@ import Image, ImageDraw # PIL try: import cv2 - opencvExists = True + opencvAvailable = True except ImportError: print('OpenCV library could not be loaded') - opencvExists = False + opencvAvailable = False +try: + import skimage + skimageAvailable = True +except ImportError: + print('Scikit-image library could not be loaded') + skimageAvailable = False + from sys import stdout import utils @@ -70,7 +77,7 @@ a[i,j] = cvmat[i,j] return a -if opencvExists: +if opencvAvailable: def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0): '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold) @@ -209,7 +216,7 @@ invH /= invH[2,2] return invH -if opencvExists: +if opencvAvailable: def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)): '''Computes the translation of img2 with respect to img1 (loaded using OpenCV as numpy arrays) @@ -235,3 +242,32 @@ else: print(dp) return None + +if skimageAvailable: + def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(3, 3), visualize=False, normalize=False): + from os import listdir + from matplotlib.pyplot import imread, imshow, figure, subplot + from skimage.feature import hog + from skimage import color, transform + from numpy import array, float32 + + inputData = [] + for filename in listdir(imageDirectory): + img = imread(imageDirectory+filename) + bwImg = color.rgb2gray(img) + inputImg = transform.resize(bwImg, rescaleSize) + features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) + if visualize: + hogViz = features[1] + features = features[0] + figure() + subplot(1,2,1) + imshow(img) + subplot(1,2,2) + imshow(hogViz) + inputData.append(features) + + nImages = len(inputData) + return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32) + +