22import mahotas as mh
33image = mh .imread ('scene00.jpg' )
44from 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默认将单通道图像转换成了假彩色图像,较高值用红色,较低值用蓝色
714image = mh .colors .rgb2grey (image , dtype = np .uint8 )
815plt .imshow (image ) # Display the image
16+
17+ #现在图像变成灰度图了
918plt .gray ()
19+ #将图像输入给otsu方法,该方法会找到合适的域值
1020thresh = mh .thresholding .otsu (image )
1121print ('Otsu threshold is {}.' .format (thresh ))
1222# Otsu threshold is 138.
23+
1324plt .imshow (image > thresh )
1425
15- im16 = mh .gaussian_filter (image ,16 )
26+
27+ #高斯滤波器,其中16为滤波器的大小(滤波器的标准差),越大越模糊
28+ im16 = mh .gaussian_filter (image ,1 )
29+
30+ #换一张新的图片
1631im = 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+ #聚焦中心
1842r ,g ,b = im .transpose (2 ,0 ,1 )
1943r12 = mh .gaussian_filter (r , 12. )
2044g12 = mh .gaussian_filter (g , 12. )
2145b12 = mh .gaussian_filter (b , 12. )
2246im12 = mh .as_rgb (r12 ,g12 ,b12 )
47+
2348h , w = r .shape # height and width
2449Y , X = np .mgrid [:h ,:w ]
2550Y = Y - h / 2. # center at h/2
3661C = C [:,:,None ] # This adds a dummy third dimension to C
3762
3863ringed = mh .stretch (im * C + (1 - C )* im12 )
64+ plt .imshow (ringed )
65+ # plt.show()
3966
67+ #计算图像特征,haralick_features是一个4*13数组,第一维代表4个可能的方向(上下左右)
4068haralick_features = mh .features .haralick (image )
69+ #如果我们对方向不感兴趣,那么可以对方向(即第一维进行算术平均)
4170haralick_features_mean = np .mean (haralick_features , axis = 0 )
71+ #将原始向量(4,13)进行拼接,变成一个(52,)的向量
4272haralick_features_all = np .ravel (haralick_features )
73+ print (haralick_features_all .shape ) #(52,)
4374
75+ #有了特征我们就可以去对图像进行分类
4476from glob import glob
4577images = glob ('../SimpleImageDataset/*.jpg' )
4678features = []
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 回归模型
5789from sklearn .pipeline import Pipeline
5890from sklearn .preprocessing import StandardScaler
5991from sklearn .linear_model import LogisticRegression
6092clf = Pipeline ([('preproc' , StandardScaler ()),
6193 ('classifier' , LogisticRegression ())])
6294
63-
6495from sklearn import cross_validation
6596cv = cross_validation .LeaveOneOut (len (images ))
6697scores = cross_validation .cross_val_score (
6798 clf , features , labels , cv = cv )
6899print ('Accuracy: {:.1%}' .format (scores .mean ()))
69100# Accuracy: 81.1%
70101
102+ #增加特征
71103def 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-
87117features = []
88118for im in images :
89119 imcolor = mh .imread (im )
@@ -93,13 +123,12 @@ def chist(im):
93123 chist (imcolor ),
94124 ]))
95125
96-
97126scores = cross_validation .cross_val_score (
98127 clf , features , labels , cv = cv )
99128print ('Accuracy: {:.1%}' .format (scores .mean ()))
100129# Accuracy: 95.6%
101130
102-
131+ #忽略边缘的信息
103132features = []
104133for 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+ #对特征做归一化
114143sc = StandardScaler ()
115144features = sc .fit_transform (features )
116145from scipy .spatial import distance
@@ -146,7 +175,6 @@ def chist(im):
146175
147176
148177
149-
150178from mahotas .features import surf
151179image = mh .demos .load ('lena' )
152180image = 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
163191concatenated = np .concatenate (alldescriptors )
192+ print (concatenated .shape )
164193print ('Number of descriptors: {}' .format (
165194 len (concatenated )))
166195# use only every 64th vector
167196concatenated = concatenated [::64 ]
197+ print (concatenated .shape )
198+
168199from sklearn .cluster import KMeans # FIXME CAPITALIZATION
169200k = 256
170201km = KMeans (k )
@@ -178,6 +209,7 @@ def chist(im):
178209 )
179210# build single array and convert to float
180211features = np .array (features , dtype = float )
212+
181213scores = cross_validation .cross_val_score (
182214 clf , features , labels , cv = cv )
183215print ('Accuracy: {:.1%}' .format (scores .mean ()))
0 commit comments