changeset 411:31604ef1cad4

added hog functions and the display of road user types if known
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 21 Aug 2013 00:00:23 -0400
parents 91954c76d12c
children 97cb5c969ef2
files c/feature-based-tracking.cpp python/cvutils.py python/moving.py
diffstat 3 files changed, 58 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/c/feature-based-tracking.cpp	Wed Aug 14 14:23:50 2013 -0400
+++ b/c/feature-based-tracking.cpp	Wed Aug 21 00:00:23 2013 -0400
@@ -323,7 +323,7 @@
 	vector<unsigned int> featureNumbers;
 	for (unsigned int j=0; j<featureGroups[i].size(); ++j)
 	  featureNumbers.push_back(featureGroups[i][j]->getId());
-	trajectoryDB->writeObject(savedObjectId, featureNumbers, -1, 1, string("objects"), string("objects_features"));
+	trajectoryDB->writeObject(savedObjectId, featureNumbers, 0 /* unknown */, 1, string("objects"), string("objects_features"));
 	savedObjectId++;
       }
     }
--- a/python/cvutils.py	Wed Aug 14 14:23:50 2013 -0400
+++ b/python/cvutils.py	Wed Aug 21 00:00:23 2013 -0400
@@ -173,9 +173,38 @@
             print 'Cannot load file ' + videoFilename
             return None
 
+    def imageBox(img, obj, frameNum, homography, width, height, px=.2, py=.2, PixelThreshold=800):
+        x = []
+        y = []
+        for f in obj.features:
+            if f.existsAtInstant(frameNum):
+                x.append(f.getPositionAtInstant(frameNum).project(homography).x)
+                y.append(f.getPositionAtInstant(frameNum).project(homography).y)
+        Xmin = min(x)
+        Xmax = max(x)
+        Ymin = min(y)
+        Ymax = max(y)
+        XMm = px * (Xmax - Xmin)
+        YMm = py * (Ymax - Ymin)
+        a = max(Ymax - Ymin + (2 * YMm), Xmax - (Xmin + 2 * XMm))
+        Y_Crop_min = int(max(0, .5 * (Ymin + Ymax - a)))
+        Y_Crop_Max = int(min(height - 1, .5 * (Ymin + Ymax + a)))
+        X_Crop_min = int(max(0, .5 * (Xmin + Xmax - a)))
+        X_Crop_Max = int(min(width - 1, .5 * (Xmin + Xmax + a)))
+        if Y_Crop_Max != Y_Crop_min and X_Crop_Max != X_Crop_min and (Y_Crop_Max - Y_Crop_min) * (X_Crop_Max - X_Crop_min) > PixelThreshold:
+            img_crop = img[Y_Crop_min : Y_Crop_Max, X_Crop_min : X_Crop_Max]
+        else:
+            img_crop = []
+        return img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max
+
+
     def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
         '''Displays the objects overlaid frame by frame over the video '''
+        from moving import userTypeNames
+
         capture = cv2.VideoCapture(videoFilename)
+        width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
+        height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
         if capture.isOpened():
             key = -1
             ret = True
@@ -202,7 +231,13 @@
                             if frameNum in boundingBoxes.keys():
                                 for rect in boundingBoxes[frameNum]:
                                     cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
-                            cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
+                            objDescription = '{} '.format(obj.num)
+                            if userTypeNames[obj.userType] != 'unknown':
+                                objDescription += userTypeNames[obj.userType][0].upper()
+                            cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
+                            if obj.features != None:
+                                img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max = imageBox(img, obj, frameNum, homography, width, height)
+                                cv2.rectangle(img, (X_Crop_min, Y_Crop_min), (X_Crop_Max, Y_Crop_Max), cvBlue, 1)
                     cvImshow('frame', img, rescale)
                     key = cv2.waitKey()
                     if saveKey(key):
@@ -299,27 +334,33 @@
             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
+    def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
         from skimage.feature import hog
         from skimage import color, transform
+
+        bwImg = color.rgb2gray(image)
+        inputImg = transform.resize(bwImg, rescaleSize)
+        features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
+        if visualize:
+            from matplotlib.pyplot import imshow, figure, subplot
+            hogViz = features[1]
+            features = features[0]
+            figure()
+            subplot(1,2,1)
+            imshow(img)
+            subplot(1,2,2)
+            imshow(hogViz)
+        return features
+
+    def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
+        from os import listdir
         from numpy import array, float32
+        from matplotlib.pyplot import imread
 
         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)
+            features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
             inputData.append(features)
 
         nImages = len(inputData)
--- a/python/moving.py	Wed Aug 14 14:23:50 2013 -0400
+++ b/python/moving.py	Wed Aug 21 00:00:23 2013 -0400
@@ -631,7 +631,7 @@
     with a trajectory and a geometry (constant volume over time) and a usertype (e.g. road user) coded as a number (see 
     '''
 
-    def __init__(self, num = None, timeInterval = None, positions = None, velocities = None, geometry = None, userType = None):
+    def __init__(self, num = None, timeInterval = None, positions = None, velocities = None, geometry = None, userType = userType2Num['unknown']):
         super(MovingObject, self).__init__(num, timeInterval)
         self.positions = positions
         self.velocities = velocities