Skip to content

Commit 515f2f9

Browse files
committed
RFCT Break out feature computation
1 parent e4d95c2 commit 515f2f9

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

ch10/README.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ figure10.py
2929
Just paste two images next to each others
3030
figure13.py
3131
Demonstrate salt&pepper effect
32-
edginess.py
33-
Contains the ``edginess_sobel`` function from the book
32+
features.py
33+
Contains the ``edginess_sobel`` function from the book as well as a simple
34+
wrapper around ``mahotas.texture.haralick``
3435
simple_classification.py
3536
Classify SimpleImageDataset with texture features + sobel feature
3637
figure18.py

ch10/edginess.py renamed to ch10/features.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ def edginess_sobel(image):
2020
edges = mh.sobel(image, just_filter=True)
2121
edges = edges.ravel()
2222
return np.sqrt(np.dot(edges, edges))
23+
24+
def texture(im):
25+
'''Compute features for an image
26+
27+
Parameters
28+
----------
29+
im : ndarray
30+
31+
Returns
32+
-------
33+
fs : ndarray
34+
1-D array of features
35+
'''
36+
im = im.astype(np.uint8)
37+
return mh.features.haralick(im).mean(0)
38+
39+

ch10/simple_classification.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,11 @@
1010
from sklearn.linear_model.logistic import LogisticRegression
1111
import numpy as np
1212
from glob import glob
13-
from edginess import edginess_sobel
13+
from features import texture, edginess_sobel
1414

1515
basedir = '../SimpleImageDataset/'
1616

1717

18-
def features_for(im):
19-
'''Compute features for an image
20-
21-
Parameters
22-
----------
23-
im : str
24-
filepath for image to process
25-
26-
Returns
27-
-------
28-
fs : ndarray
29-
1-D array of features
30-
'''
31-
im = mh.imread(im, as_grey=True).astype(np.uint8)
32-
return mh.features.haralick(im).mean(0)
33-
3418
haralicks = []
3519
sobels = []
3620
labels = []
@@ -40,8 +24,9 @@ def features_for(im):
4024
# Use glob to get all the images
4125
images = glob('{}/*.jpg'.format(basedir))
4226
for fname in images:
43-
haralicks.append(features_for(fname))
44-
sobels.append(edginess_sobel(mh.imread(fname, as_grey=True)))
27+
im = mh.imread(fname, as_grey=True)
28+
haralicks.append(texture(im))
29+
sobels.append(edginess_sobel(im))
4530

4631
# Files are named like building00.jpg, scene23.jpg...
4732
labels.append(fname[:-len('xx.jpg')])

0 commit comments

Comments
 (0)