-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
features.py
70 lines (52 loc) · 1.52 KB
/
features.py
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
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
import numpy as np
import mahotas as mh
def edginess_sobel(image):
'''Measure the "edginess" of an image
image should be a 2d numpy array (an image)
Returns a floating point value which is higher the "edgier" the image is.
'''
edges = mh.sobel(image, just_filter=True)
edges = edges.ravel()
return np.sqrt(np.dot(edges, edges))
def texture(im):
'''Compute features for an image
Parameters
----------
im : ndarray
Returns
-------
fs : ndarray
1-D array of features
'''
im = im.astype(np.uint8)
return mh.features.haralick(im).ravel()
def color_histogram(im):
'''Compute color histogram of input image
Parameters
----------
im : ndarray
should be an RGB image
Returns
-------
c : ndarray
1-D array of histogram values
'''
# Downsample pixel values:
im = im // 64
# We can also implement the following by using np.histogramdd
# im = im.reshape((-1,3))
# bins = [np.arange(5), np.arange(5), np.arange(5)]
# hist = np.histogramdd(im, bins=bins)[0]
# hist = hist.ravel()
# Separate RGB channels:
r,g,b = im.transpose((2,0,1))
pixels = 1 * r + 4 * g + 16 * b
hist = np.bincount(pixels.ravel(), minlength=64)
hist = hist.astype(float)
return np.log1p(hist)