def ComputeHOG(self, trainData, testData):
     print(
         'ComputeHog Function is called to calculate Histogram of Oriented Gradient'
     )
     trainSize = trainData.shape[0]
     testSize = testData.shape[0]
     if self.imgNormalize is True:
         trainData = trainData * 255
         testData = testData * 255
         trainData = trainData.astype('uint8')
         testData = testData.astype('uint8')
     else:
         print('Images already Normalized')
     hog = HOGDescriptor(_winSize=(self.winsize, self.winsize),
                         _blockSize=(self.blocksize, self.blocksize),
                         _blockStride=(self.blockstride, self.blockstride),
                         _cellSize=(self.cellsize, self.cellsize),
                         _nbins=self.nbin)
     hogTrain = np.zeros((trainSize, hog.getDescriptorSize()),
                         dtype="float32")
     for i in range(0, trainSize):
         trainImage = trainData[i].reshape(self.winsize, self.winsize)
         h = hog.compute(trainImage)
         hogTrain[i, :] = h[:, 0]
     hogTest = np.zeros((testSize, hog.getDescriptorSize()),
                        dtype="float32")
     for i in range(0, testSize):
         testImage = testData[i].reshape(self.winsize, self.winsize)
         h = hog.compute(testImage)
         hogTest[i, :] = h[:, 0]
         print("Extracted HoG features (", h.size, " per image)")
         return hogTrain, hogTest
Exemple #2
0
def get_hog_features(img,
                     orient,
                     pix_per_cell,
                     cell_per_block,
                     vis=False,
                     feature_vec=True,
                     fast=False):
    # Call with two outputs if vis==True
    if vis:
        features, hog_image = hog(img,
                                  orientations=orient,
                                  pixels_per_cell=(pix_per_cell, pix_per_cell),
                                  cells_per_block=(cell_per_block,
                                                   cell_per_block),
                                  transform_sqrt=True,
                                  visualise=vis,
                                  feature_vector=feature_vec)
        return features, hog_image
    # Otherwise call with one output
    elif fast:
        w = img.shape[0]
        h = img.shape[1]
        cell_size = (pix_per_cell, pix_per_cell)
        block_size = (pix_per_cell * cell_per_block,
                      pix_per_cell * cell_per_block)
        block_stride = (pix_per_cell, pix_per_cell)
        opencv_hog = HOGDescriptor((w, h), block_size, block_stride, cell_size,
                                   orient)
        features = opencv_hog.compute(img)
        if not feature_vec:
            new_shape = ((w - block_size[0]) // block_stride[0] + 1,
                         (h - block_size[1]) // block_stride[1] + 1,
                         cell_per_block, cell_per_block, orient)
            features = features.reshape(new_shape)
        return features
    else:
        features = hog(img,
                       orientations=orient,
                       pixels_per_cell=(pix_per_cell, pix_per_cell),
                       cells_per_block=(cell_per_block, cell_per_block),
                       transform_sqrt=True,
                       visualise=vis,
                       feature_vector=feature_vec)
        return features
Exemple #3
0
 def cmptFeatures(self, image):
     winSize = (16, 48)
     blockSize = (16, 16)
     blockStride = (8, 8)
     cellSize = (8, 8)
     nbins = 9
     derivAperture = 1
     winSigma = 4.
     histogramNormType = 0
     L2HysThreshold = 2.0000000000000001e-01
     gammaCorrection = 100
     nlevels = 32
     hog = HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins,
                         derivAperture, winSigma, histogramNormType,
                         L2HysThreshold, gammaCorrection, nlevels)
     winStride = (8, 8)
     padding = (16, 16)
     hist = hog.compute(image, winStride, padding)
     return np.reshape(hist, 4500)
#plt.title("Grayscale Histogram")
#plt.xlabel("Bins")
#plt.ylabel("# of Pixels")
#plt.plot(hist)
#plt.xlim([0, 256])

# -------------------------------- OpenCV: HoG --------------------------------

# Compute the histogram of oriented gradients
# http://docs.opencv.org/modules/gpu/doc/object_detection.html
hog = HOGDescriptor(_winSize=(w,w), _blockSize=(4,4), _blockStride=(2,2), _cellSize=(2,2), _nbins=9)

hogTrain = np.zeros((trainSize, hog.getDescriptorSize()), dtype="float32")
for i in xrange(0,trainSize):
    image = dataTrain[i].reshape(w,w)
    h = hog.compute(image)
    hogTrain[i,:] = h[:,0]

hogTest = np.zeros((testSize, hog.getDescriptorSize()), dtype="float32")
for i in xrange(0,testSize):
    image = dataTest[i].reshape(w,w)
    h = hog.compute(image)
    hogTest[i,:] = h[:,0]

print "Extracted HoG features (", h.size, " per image)"

# -------------------- Machine Learning: Feature Selection --------------------

selector = SelectKBest(chi2, k=500)
XTrain = selector.fit_transform(hogTrain, yTrain)
XTest = selector.transform(hogTest)