Skip to content

Commit 940baf5

Browse files
committed
ENH Code that is simpler to explain
1 parent a24de07 commit 940baf5

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

ch12/image-classification.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
basedir = '../SimpleImageDataset/'
2121

2222
@TaskGenerator
23-
def features_for(im):
23+
def compute_texture(im):
2424
'''Compute features for an image
2525
2626
Parameters
@@ -33,8 +33,9 @@ def features_for(im):
3333
fs : ndarray
3434
1-D array of features
3535
'''
36-
im = mh.imread(im, as_grey=True).astype(np.uint8)
37-
return mh.features.haralick(im).ravel()
36+
from features import texture
37+
imc = mh.imread(im)
38+
return texture(mh.colors.rgb2grey(imc))
3839

3940
@TaskGenerator
4041
def chist(fname):
@@ -59,19 +60,16 @@ def accuracy(features, labels):
5960
return scores.mean()
6061

6162

62-
@TaskGenerator
63-
def stack_features(chists, haralicks):
64-
return np.hstack([chists, haralicks])
65-
6663
@TaskGenerator
6764
def print_results(scores):
6865
with open('results.image.txt', 'w') as output:
69-
for k in scores:
66+
for k,v in scores:
7067
output.write('Accuracy (LOO x-val) with Logistic Regression [{}]: {:.1%}\n'.format(
71-
k, scores[k].mean()))
68+
k, v.mean()))
7269

7370

7471
to_array = TaskGenerator(np.array)
72+
hstack = TaskGenerator(np.hstack)
7573

7674
haralicks = []
7775
chists = []
@@ -80,7 +78,7 @@ def print_results(scores):
8078
# Use glob to get all the images
8179
images = glob('{}/*.jpg'.format(basedir))
8280
for fname in sorted(images):
83-
haralicks.append(features_for(fname))
81+
haralicks.append(compute_texture(fname))
8482
chists.append(chist(fname))
8583
labels.append(fname[:-len('00.jpg')]) # The class is encoded in the filename as xxxx00.jpg
8684

@@ -91,12 +89,12 @@ def print_results(scores):
9189
scores_base = accuracy(haralicks, labels)
9290
scores_chist = accuracy(chists, labels)
9391

94-
combined = stack_features(chists, haralicks)
92+
combined = hstack([chists, haralicks])
9593
scores_combined = accuracy(combined, labels)
9694

97-
print_results({
98-
'base': scores_base,
99-
'chists': scores_chist,
100-
'combined' : scores_combined,
101-
})
95+
print_results([
96+
('base', scores_base),
97+
('chists', scores_chist),
98+
('combined' , scores_combined),
99+
])
102100

0 commit comments

Comments
 (0)