Skip to content

Commit 17a8fdb

Browse files
committed
add notes
1 parent bf7146d commit 17a8fdb

16 files changed

Lines changed: 99 additions & 57 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
*.png
44
*.zip
55
*.txt
6+
*.rar
67
*.dat
78
/charts

‎ch10/chapter.py‎

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,49 @@
22
import mahotas as mh
33
image = mh.imread('scene00.jpg')
44
from matplotlib import pyplot as plt
5-
plt.imshow(image)
6-
plt.show()
5+
import pylab
6+
import sys
7+
8+
9+
# plt.imshow(image)
10+
# plt.show()
11+
12+
13+
#matplotlib默认将单通道图像转换成了假彩色图像,较高值用红色,较低值用蓝色
714
image = mh.colors.rgb2grey(image, dtype=np.uint8)
815
plt.imshow(image) # Display the image
16+
17+
#现在图像变成灰度图了
918
plt.gray()
19+
#将图像输入给otsu方法,该方法会找到合适的域值
1020
thresh = mh.thresholding.otsu(image)
1121
print('Otsu threshold is {}.'.format(thresh))
1222
# Otsu threshold is 138.
23+
1324
plt.imshow(image > thresh)
1425

15-
im16 = mh.gaussian_filter(image,16)
26+
27+
#高斯滤波器,其中16为滤波器的大小(滤波器的标准差),越大越模糊
28+
im16 = mh.gaussian_filter(image,1)
29+
30+
#换一张新的图片
1631
im = mh.demos.load('lenna')
1732

33+
#加入椒盐噪声
34+
salt = np.random.random(im.shape) > .975
35+
pepper = np.random.random(im.shape) > .975
36+
im_salt = mh.stretch(im)
37+
im_salt = np.maximum(salt*170, im_salt)
38+
im_salt = np.minimum(pepper*30 + im_salt*(~pepper), im_salt)
39+
plt.imshow(im_salt)
40+
# plt.show()
41+
#聚焦中心
1842
r,g,b = im.transpose(2,0,1)
1943
r12 = mh.gaussian_filter(r, 12.)
2044
g12 = mh.gaussian_filter(g, 12.)
2145
b12 = mh.gaussian_filter(b, 12.)
2246
im12 = mh.as_rgb(r12,g12,b12)
47+
2348
h, w = r.shape # height and width
2449
Y, X = np.mgrid[:h,:w]
2550
Y = Y-h/2. # center at h/2
@@ -36,11 +61,18 @@
3661
C = C[:,:,None] # This adds a dummy third dimension to C
3762

3863
ringed = mh.stretch(im*C + (1-C)*im12)
64+
plt.imshow(ringed)
65+
# plt.show()
3966

67+
#计算图像特征,haralick_features是一个4*13数组,第一维代表4个可能的方向(上下左右)
4068
haralick_features = mh.features.haralick(image)
69+
#如果我们对方向不感兴趣,那么可以对方向(即第一维进行算术平均)
4170
haralick_features_mean = np.mean(haralick_features, axis=0)
71+
#将原始向量(4,13)进行拼接,变成一个(52,)的向量
4272
haralick_features_all = np.ravel(haralick_features)
73+
print(haralick_features_all.shape) #(52,)
4374

75+
#有了特征我们就可以去对图像进行分类
4476
from glob import glob
4577
images = glob('../SimpleImageDataset/*.jpg')
4678
features = []
@@ -51,23 +83,23 @@
5183
im = mh.colors.rgb2gray(im, dtype=np.uint8)
5284
features.append(mh.features.haralick(im).ravel())
5385

54-
features = np.array(features)
55-
labels = np.array(labels)
56-
86+
features = np.array(features) #(90,52)
87+
labels = np.array(labels) #(90,)
88+
#训练一个Logistic 回归模型
5789
from sklearn.pipeline import Pipeline
5890
from sklearn.preprocessing import StandardScaler
5991
from sklearn.linear_model import LogisticRegression
6092
clf = Pipeline([('preproc', StandardScaler()),
6193
('classifier', LogisticRegression())])
6294

63-
6495
from sklearn import cross_validation
6596
cv = cross_validation.LeaveOneOut(len(images))
6697
scores = cross_validation.cross_val_score(
6798
clf, features, labels, cv=cv)
6899
print('Accuracy: {:.1%}'.format(scores.mean()))
69100
# Accuracy: 81.1%
70101

102+
#增加特征
71103
def chist(im):
72104
im = im // 64
73105
r,g,b = im.transpose((2,0,1))
@@ -82,8 +114,6 @@ def chist(im):
82114
im = mh.imread(im)
83115
features.append(chist(im))
84116

85-
86-
87117
features = []
88118
for im in images:
89119
imcolor = mh.imread(im)
@@ -93,13 +123,12 @@ def chist(im):
93123
chist(imcolor),
94124
]))
95125

96-
97126
scores = cross_validation.cross_val_score(
98127
clf, features, labels, cv=cv)
99128
print('Accuracy: {:.1%}'.format(scores.mean()))
100129
# Accuracy: 95.6%
101130

102-
131+
#忽略边缘的信息
103132
features = []
104133
for im in images:
105134
imcolor = mh.imread(im)
@@ -110,7 +139,7 @@ def chist(im):
110139
mh.features.haralick(im).ravel(),
111140
chist(imcolor),
112141
]))
113-
142+
#对特征做归一化
114143
sc = StandardScaler()
115144
features = sc.fit_transform(features)
116145
from scipy.spatial import distance
@@ -146,7 +175,6 @@ def chist(im):
146175

147176

148177

149-
150178
from mahotas.features import surf
151179
image = mh.demos.load('lena')
152180
image = mh.colors.rgb2gray(image, dtype=np.uint8)
@@ -161,10 +189,13 @@ def chist(im):
161189
alldescriptors.append(surf.dense(image, spacing=16))
162190
# get all descriptors into a single array
163191
concatenated = np.concatenate(alldescriptors)
192+
print(concatenated.shape)
164193
print('Number of descriptors: {}'.format(
165194
len(concatenated)))
166195
# use only every 64th vector
167196
concatenated = concatenated[::64]
197+
print(concatenated.shape)
198+
168199
from sklearn.cluster import KMeans # FIXME CAPITALIZATION
169200
k = 256
170201
km = KMeans(k)
@@ -178,6 +209,7 @@ def chist(im):
178209
)
179210
# build single array and convert to float
180211
features = np.array(features, dtype=float)
212+
181213
scores = cross_validation.cross_val_score(
182214
clf, features, labels, cv=cv)
183215
print('Accuracy: {:.1%}'.format(scores.mean()))

‎ch10/download.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mkdir -p AnimTransDistr
44
cd AnimTransDistr
5-
curl -O http://vision.stanford.edu/Datasets/AnimTransDistr.rar
5+
#curl -O http://vision.stanford.edu/Datasets/AnimTransDistr.rar
66
unrar x AnimTransDistr.rar
77
# The following file is a weird file:
88
rm Anims/104034.jpg

‎ch10/figure10.jpg‎

136 KB
Loading

‎ch10/large_classification.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def images():
3737
'''
3838
for ci, cl in enumerate(classes):
3939
images = glob('{}/{}/*.jpg'.format(basedir, cl))
40-
for im in sorted(images):
40+
for im in sorted(images[:50]):
4141
yield im, ci
4242

4343
classes = [

‎ch10/lena-ring.jpg‎

24.7 KB
Loading
File renamed without changes.

‎ch10/neighbors.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
print('Computing neighbors...')
3939
sc = StandardScaler()
4040
features = sc.fit_transform(features)
41+
#计算距离矩阵
4142
dists = distance.squareform(distance.pdist(features))
42-
4343
print('Plotting...')
4444
fig, axes = plt.subplots(2, 9, figsize=(16,8))
4545

‎ch11/demo_corr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def plot_correlation_demo():
3434
np.random.seed(1) # to reproduce the data later on
3535
pylab.clf()
3636
pylab.figure(num=None, figsize=(8, 8))
37-
x = np.arange(0, 10, 0.2)
37+
x = np.arange(0, 10, 0.2)
3838

3939
pylab.subplot(221)
4040
y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x))

‎ch11/demo_mds.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def plot_demo_1():
3030
fig = pylab.figure(figsize=(10, 4))
3131

3232
ax = fig.add_subplot(121, projection='3d')
33-
ax.set_facecolor('white')
33+
ax.set_facecolor('white')
3434
# ax.set_axis_bgcolor('white')
3535

3636
mds = manifold.MDS(n_components=3)

0 commit comments

Comments
 (0)