HOG.py
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
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 . 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)