Skip to content

Commit 5bdba98

Browse files
committed
ENH Add code for image neighbors
This is a new section in the second edition, but it makes sense to demonstrate the possibilities of images features in some other task than classification.
1 parent 9d3ea50 commit 5bdba98

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

ch10/README.rst

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,15 @@ Running ``download.sh`` will retrieve the other dataset into a directory
1717
Scripts
1818
-------
1919

20-
threshold.py
21-
Threshold building image with both Otsu and Ridley-Calvard threshold
22-
lena-ring.py
23-
Show Lena in a sharp ring around a soft focus image
24-
figure5_6.py
25-
Computes figures 5 and 6 in the book
26-
figure9.py
27-
Compute Sobel images
2820
figure10.py
2921
Just paste two images next to each others
30-
figure13.py
31-
Demonstrate salt&pepper effect
3222
features.py
33-
Contains the ``edginess_sobel`` function from the book as well as a simple
23+
Contains the color histogram function from the book as well as a simple
3424
wrapper around ``mahotas.texture.haralick``
3525
simple_classification.py
3626
Classify SimpleImageDataset with texture features + color histogram features
37-
figure18.py
27+
large_classification.py
3828
Classify ``AnimTransDistr`` with both texture and SURF features.
29+
neighbors.py
30+
Computes image neighbors as well as the neighbor figure from the book.
3931

ch10/neighbors.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This code is supporting material for the book
2+
# Building Machine Learning Systems with Python
3+
# by Willi Richert and Luis Pedro Coelho
4+
# published by PACKT Publishing
5+
6+
import numpy as np
7+
import mahotas as mh
8+
from glob import glob
9+
from features import texture, color_histogram
10+
from matplotlib import pyplot as plt
11+
from sklearn.preprocessing import StandardScaler
12+
from scipy.spatial import distance
13+
14+
basedir = '../SimpleImageDataset/'
15+
16+
17+
haralicks = []
18+
chists = []
19+
20+
print('Computing features...')
21+
# Use glob to get all the images
22+
images = glob('{}/*.jpg'.format(basedir))
23+
# We sort the images to ensure that they are always processed in the same order
24+
# Otherwise, this would introduce some variation just based on the random
25+
# ordering that the filesystem uses
26+
images.sort()
27+
28+
for fname in images:
29+
imc = mh.imread(fname)
30+
imc = imc[200:-200,200:-200]
31+
haralicks.append(texture(mh.colors.rgb2grey(imc)))
32+
chists.append(color_histogram(imc))
33+
34+
haralicks = np.array(haralicks)
35+
chists = np.array(chists)
36+
features = np.hstack([chists, haralicks])
37+
38+
print('Computing neighbors...')
39+
sc = StandardScaler()
40+
dists = distance.squareform(distance.pdist(features))
41+
42+
print('Plotting...')
43+
fig, axes = plt.subplots(2, 9, figsize=(16,8))
44+
45+
# Remove ticks from all subplots
46+
for ax in axes.flat:
47+
ax.set_xticks([])
48+
ax.set_yticks([])
49+
50+
for ci,i in enumerate(range(0,90,10)):
51+
left = images[i]
52+
dists_left = dists[i]
53+
right = dists_left.argsort()
54+
right = right[1]
55+
right = images[right]
56+
left = mh.imread(left)
57+
right = mh.imread(right)
58+
axes[0, ci].imshow(left)
59+
axes[1, ci].imshow(right)
60+
61+
fig.tight_layout()
62+
fig.savefig('figure_neighbors.png', dpi=300)

0 commit comments

Comments
 (0)