""" Extract HOG feature of images.But we decide to use C instead of Python considering the 10-times efficiency gap. @author: chunk chunkplus@gmail.com 2014 Dec """ __author__ = 'hadoop' from mfeat import * from common import * import numpy as np from PIL import Image import cv2 from skimage.feature import hog from skimage import io, color, transform, exposure class FeatHOG(FeatureBase): def __init__(self, size=(64, 64)): FeatureBase.__init__(self) self.size = size def _feat_sk(self, image, size=(64, 64)): """ N.B. image[x][y][z] where x for line and y for column,z(0,1,2) for (R,G,B) """ if size is None: size = (64, 64) img = io.imread(image, as_grey=True) # 0.02s img = transform.resize(img, size) # img = color.rgb2gray(img) fd = hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False, normalise=False) # 0.18s # hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) # # cv2.imshow('processed', ski2cv(hog_image_rescaled)) # cv2.waitKey(0) return fd def _feat_cv(self, image, size=(64, 64)): """ N.B. image[x][y][z] where x for line and y for column,z(0,1,2) for (B,G,R) HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins[, _derivAperture[, _winSigma[, _histogramNormType[, _L2HysThreshold[, _gammaCorrection[, _nlevels]]]]]]) """ if size is None: size = (64, 64) img = cv2.imread(image, cv2.CV_LOAD_IMAGE_GRAYSCALE) # 0.0s img = cv2.resize(img, size, interpolation=cv2.INTER_LINEAR) params = dict( _winSize=size, _blockSize=(16, 16), _blockStride=(8, 8), _cellSize=(8, 8), _nbins=9, _derivAperture=1, _winSigma=-1, _histogramNormType=cv2.HOGDESCRIPTOR_L2HYS, _L2HysThreshold=0.2, _gammaCorrection=False, _nlevels=cv2.HOGDESCRIPTOR_DEFAULT_NLEVELS, ) # feat_size = 1764 = (64/8-1)^2 * 2^2 * 9 hog = cv2.HOGDescriptor(**params) desc = hog.compute(img) # 0.03s return desc[:, 0] def set_size(self, size): self.size = size def feat(self, image): return self._feat_cv(image, self.size)