├── LICENSE ├── README.md ├── corns images.rar ├── svm image features extraction.py └── svm model building.py /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-classification-SVM-ML 2 | Image classification based on SVM. Use Gray level co-occurrence matrix(GLCM) and Histogram of Oriented Gradient (HOG) for image features extraction. 3 | -------------------------------------------------------------------------------- /corns images.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HealthyBuilder/Image-classification-SVM-ML/284e64d0350eccd980419ff4d103ad2ba3474f84/corns images.rar -------------------------------------------------------------------------------- /svm image features extraction.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import re 3 | import os 4 | import math 5 | import numpy as np 6 | import pandas as pd 7 | 8 | from sklearn import svm 9 | 10 | # 灰度共生矩阵的计算 11 | 12 | # 定义最大灰度级数 13 | gray_level = 16 14 | 15 | def maxGrayLevel(img): 16 | max_gray_level = 0 17 | (height, width) = img.shape 18 | # print(height, width) 19 | for y in range(height): 20 | for x in range(width): 21 | if img[y][x] > max_gray_level: 22 | max_gray_level = img[y][x] 23 | return max_gray_level + 1 24 | 25 | 26 | def getGlcm(input, d_x, d_y): 27 | srcdata = input.copy() 28 | ret = [[0.0 for i in range(gray_level)] for j in range(gray_level)] 29 | (height, width) = input.shape 30 | max_gray_level = maxGrayLevel(input) 31 | # 若灰度级数大于gray_level,则将图像的灰度级缩小至gray_level,减小灰度共生矩阵的大小 32 | if max_gray_level > gray_level: 33 | for j in range(height): 34 | for i in range(width): 35 | srcdata[j][i] = srcdata[j][i] * gray_level / max_gray_level 36 | 37 | for j in range(height - d_y): 38 | for i in range(width - d_x): 39 | rows = srcdata[j][i] 40 | cols = srcdata[j + d_y][i + d_x] 41 | ret[rows][cols] += 1.0 42 | 43 | for i in range(gray_level): 44 | for j in range(gray_level): 45 | ret[i][j] /= float(height * width) 46 | return ret 47 | 48 | def feature_computer(p): 49 | Con = 0.0 50 | Eng = 0.0 51 | Asm = 0.0 52 | Idm = 0.0 53 | for i in range(gray_level): 54 | for j in range(gray_level): 55 | Con += (i - j) * (i - j) * p[i][j] 56 | Asm += p[i][j] * p[i][j] 57 | Idm += p[i][j] / (1 + (i - j) * (i - j)) 58 | if p[i][j] > 0.0: 59 | Eng += p[i][j] * math.log(p[i][j]) 60 | return Asm, Con, -Eng, Idm 61 | 62 | # 返回 4个变量 63 | def testfeature(img): 64 | # img = cv.imread(image_name) 65 | try: 66 | img_shape = img.shape 67 | except: 68 | print('imread error') 69 | return 70 | img = cv.resize(img, (250, 250), interpolation=cv.INTER_AREA) # 500, 400 均划为300×300格式, 双线性插值法 71 | img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 72 | glcm_0 = getGlcm(img_gray, 1, 0) 73 | # glcm_1=getGlcm(src_gray, 0,1) 74 | # glcm_2=getGlcm(src_gray, 1,1) 75 | # glcm_3=getGlcm(src_gray, -1,1) 76 | asm, con, eng, idm = feature_computer(glcm_0) 77 | if (not asm) or (not con) or (not eng) or (not idm): 78 | print('没有返回值!') 79 | 80 | return asm, con, eng, idm # 角二阶矩(能量)、对比度、熵、反差分矩阵(逆方差) 81 | 82 | # 返回R,G,B、H、L、S: N×6的矩阵 83 | def reRGBandHLS(img): 84 | im_B = img[:, :, 0] # 从RGB中读入通道 85 | im_G = img[:, :, 1] 86 | im_R = img[:, :, 2] 87 | # R, G, B 88 | im_R_mean = np.mean(im_R) 89 | im_G_mean = np.mean(im_G) 90 | im_B_mean = np.mean(im_B) 91 | # 转化为HLS颜色空间 92 | img_hls=cv.cvtColor(img, cv.COLOR_BGR2HLS) 93 | im_H = img[:, :, 0] # 从HLS中读入通道 94 | im_L = img[:, :, 1] 95 | im_S = img[:, :, 2] 96 | # H, L, S 97 | im_H_mean = np.mean(im_H) 98 | im_L_mean = np.mean(im_L) 99 | im_S_mean = np.mean(im_S) 100 | return im_R_mean,im_G_mean,im_B_mean,im_H_mean,im_L_mean,im_S_mean 101 | pass 102 | 103 | if __name__ == '__main__': 104 | 105 | ims_path='corn_new_0/'# 图像数据集的路径 106 | ims_list=os.listdir(ims_path) 107 | # print(ims_list) 108 | imgnum=len(ims_list) # 获取总个数 109 | data=np.zeros((imgnum, 11)) # 角二阶矩(能量)、对比度、熵、反差分矩阵(逆方差)、R,G,B、H、L、S、 TAG(0机械损伤,1霉变,2虫蛀) 110 | for i in range(imgnum): 111 | im=ims_list[i] 112 | # 添加标签 113 | if r'hurt' in im: 114 | data[i, 10]=0 115 | elif r'mildew' in im: 116 | data[i, 10] = 1 117 | elif r'worm' in im: 118 | data[i, 10] = 2 119 | img = cv.imread('corn_new_0/'+im) 120 | # print('img', img) 121 | # 0,1,2,3 添加灰度矩阵四类特征 122 | asm, con, eng, idm = testfeature(img) 123 | data[i, 0:4]=asm, con, eng, idm 124 | # 4-9 添加6类图像空间特征 125 | data[i, 4:10]=reRGBandHLS(img) 126 | 127 | print('All data', data) 128 | print(type(data)) # 显示此变量类型 129 | print('data size', data.shape) # 输出此data纬度 130 | data_tosave=pd.DataFrame(data) 131 | data_tosave.to_csv('data/data0.csv') 132 | print('success saving!') 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | # path1='corn_new_0/hurt1.png' 152 | # rgb1=cv.imread(path1) 153 | # print('rgb\n:', rgb1) 154 | # # cv.imshow("rgb", rgb1) 155 | # # cv.waitKey(0) 156 | # # cv.destroyAllWindows() 157 | # hsv1=cv.cvtColor(rgb1, cv.COLOR_BGR2HSV) 158 | # print('hsv\n:', hsv1) 159 | # # cv.imshow("hsv", hsv1) 160 | # # cv.waitKey(0) 161 | # hls1=cv.cvtColor(rgb1, cv.COLOR_BGR2HLS) 162 | # print('hls1\n:', hls1) 163 | # cv.imshow("hls", hls1) 164 | # cv.waitKey(0) 165 | ''' 166 | ims_path='corn_new_0/'# 图像数据集的路径 167 | ims_list=os.listdir(ims_path) 168 | R_means=[] 169 | G_means=[] 170 | B_means=[] 171 | for im_list in ims_list: 172 | im=cv.imread(ims_path+im_list) 173 | #extrect value of diffient channel 174 | im_B=im[:,:,0] # 从RGB中读入通道 175 | im_G=im[:,:,1] 176 | im_R=im[:,:,2] 177 | #count mean for every channel 178 | im_R_mean=np.mean(im_R) 179 | im_G_mean=np.mean(im_G) 180 | im_B_mean=np.mean(im_B) 181 | #save single mean value to a set of means 182 | R_means.append(im_R_mean) 183 | G_means.append(im_G_mean) 184 | B_means.append(im_B_mean) 185 | print('图片:{} 的 RGB平均值为 \n[{},{},{}]'.format(im_list,im_R_mean,im_G_mean,im_B_mean) ) 186 | #three sets into a large set 187 | a=[R_means,G_means,B_means] 188 | mean=[0,0,0] 189 | #count the sum of different channel means 190 | mean[0]=np.mean(a[0]) 191 | mean[1]=np.mean(a[1]) 192 | mean[2]=np.mean(a[2]) 193 | print('数据集的BGR平均值为\n[{},{},{}]'.format( mean[0], mean[1], mean[2])) 194 | # a1, a2, a3, a4 = testfeature("corn_new_0/hurt1.png") 195 | # print(a1, a2, a3, a4) 196 | # # asm 197 | 198 | 199 | asm_train=[] 200 | con_train=[] 201 | eng_train=[] 202 | idm_train=[] 203 | ans_train=[] 204 | 205 | asm_test = [] 206 | con_test = [] 207 | eng_test = [] 208 | idm_test = [] 209 | ans_test = [] 210 | 211 | # 机械损伤1-10 train, 11-14 test, 用0表示 212 | for i in range(1, 11): 213 | # path1='corn_new_0/hurt'+str(i)+'.png' 214 | path_new = 'corn_new_0/' + 'hurt' + str(i) + '.png' 215 | asmt, cont, engt, idmt = testfeature(path_new) 216 | asm_train.append(asmt) 217 | con_train.append(cont) 218 | eng_train.append(engt) 219 | idm_train.append(idmt) 220 | ans_train.append(0) 221 | print('hurt', i, 'finished!') 222 | 223 | for i in range(11, 15): 224 | path1 = r'corn_new_0/hurt'+str(i)+'.png' 225 | asm, con, eng, idm = testfeature(path1) 226 | asm_test.append(asm) 227 | con_test.append(con) 228 | eng_test.append(eng) 229 | idm_test.append(idm) 230 | ans_test.append(0) 231 | print('hurt', i, 'finished!') 232 | 233 | # 霉变,用1表示, 1-10 trian, 11-15 test 234 | for i in range(1, 11): 235 | path1 = r'corn_new_0/mildew' + str(i) + '.png' 236 | asm, con, eng, idm = testfeature(path1) 237 | asm_train.append(asm) 238 | con_train.append(con) 239 | eng_train.append(eng) 240 | idm_train.append(idm) 241 | ans_train.append(1) 242 | print('mildew', i, 'finished!') 243 | 244 | for i in range(11, 16): 245 | path1 = r'corn_new_0/mildew' + str(i) + '.png' 246 | asm, con, eng, idm = testfeature(path1) 247 | asm_test.append(asm) 248 | con_test.append(con) 249 | eng_test.append(eng) 250 | idm_test.append(idm) 251 | ans_test.append(1) 252 | print('mildew', i, 'finished!') 253 | 254 | # 虫蛀,用2表示,1-10 train, 11-20 test 255 | for i in range(1, 11): 256 | path1 = r'corn_new_0/worm' + str(i) + '.png' 257 | asm, con, eng, idm = testfeature(path1) 258 | asm_train.append(asm) 259 | con_train.append(con) 260 | eng_train.append(eng) 261 | idm_train.append(idm) 262 | ans_train.append(2) 263 | print('worm', i, 'finished!') 264 | 265 | for i in range(11, 21): 266 | path1 = r'corn_new_0/worm' + str(i) + '.png' 267 | asm, con, eng, idm = testfeature(path1) 268 | asm_test.append(asm) 269 | con_test.append(con) 270 | eng_test.append(eng) 271 | idm_test.append(idm) 272 | ans_test.append(2) 273 | print('worm', i, 'finished!') 274 | 275 | # 输出数据 276 | print('train:') 277 | print('asm:', asm_train) 278 | print('con:', con_train) 279 | print('eng:', eng_train) 280 | print('idm:', idm_train) 281 | print('ans:', ans_train) 282 | 283 | print('test:') 284 | print('asm:', asm_test) 285 | print('con:', con_test) 286 | print('eng:', eng_test) 287 | print('idm:', idm_test) 288 | print('ans:', ans_test) 289 | 290 | # 291 | ''' 292 | 293 | 294 | -------------------------------------------------------------------------------- /svm model building.py: -------------------------------------------------------------------------------- 1 | from sklearn import svm # svm支持向量机 2 | import matplotlib.pyplot as plt # 可视化绘图 3 | import numpy as np 4 | from sklearn.model_selection import train_test_split 5 | import pandas as pd 6 | from sklearn.externals import joblib 7 | 8 | # 读取数据 9 | data = pd.read_csv('data/data0.csv') # 49×11 10 | data=np.array(data) 11 | # print('data\n', data) 12 | rate=[] 13 | Kern='poly' 14 | 15 | 16 | for k in range(3): 17 | 18 | # 划分训练集测试集 19 | X_train,X_test, y_train, y_test = train_test_split(data[:, 1:11], data[:, 11], test_size=0.3, random_state=k) 20 | # print('x_train\n', X_train) 21 | # print('x_test\n', X_test) 22 | # print('y_train\n', y_train) 23 | # print('y_test\n', y_test) 24 | 25 | # 搭建svm模型 26 | svm_clf = svm.SVC(C=1.0, kernel=Kern, probability=True, tol=0.0001, max_iter=100000, degree=3) # 27 | 28 | # 训练模型 29 | print('train beagin...') 30 | svm_clf.fit(X_train, y_train) 31 | # joblib.dump(svm_clf, "玉米svm1.0.m") 32 | print('Training finished') 33 | 34 | # 测试模型 35 | y_pre = svm_clf.predict(X_test) 36 | print('y_pre', y_pre) 37 | print('y_true', y_test) 38 | 39 | # 计算准确率 40 | sum=0 41 | for i in range(len(y_test)): 42 | if y_pre[i]==y_test[i]: 43 | sum=sum+1 44 | print('len(y_test)', len(y_test)) 45 | print('right sum', sum) 46 | print('rate[', k, '] ', sum/len(y_test)) 47 | rate.append(sum/len(y_test)) 48 | print('') 49 | 50 | print('所用核为:',Kern) 51 | print('\nrate[0]', rate[0], 'rate[1]', rate[1], 'rate[2]', rate[2], 'mean rate', (rate[0]+rate[1]+rate[2])/3) 52 | 53 | 54 | 55 | --------------------------------------------------------------------------------