传统特征提取算法

传统特征提取

直接上代码吧!

from skimage.feature import greycomatrix,greycoprops
from sklearn.metrics import  accuracy_score
import pickle
from sklearn.svm import SVC
with open('/home/shen/Desktop/SVM/train_test.pickle','rb') as f:
    train_features,train_labels,test_features,test_labels = pickle.load(f)
matrix = []
train_x = train_features[:,:,:,0]
test_x = test_features[:,:,:,0]
print(train_x.shape)
print(test_x.shape)
train_y = train_labels.reshape([-1,])
test_y = test_labels.reshape([-1,])
size = train_x.shape[0]
fa1 = np.zeros([size])
fb1 = np.zeros([size])
fc1 = np.zeros([size])
fd1 = np.zeros([size])
tr_1 = time.time()
for i in range(train_x.shape[0]):
    glcm = greycomatrix(train_x[i], [5], [0], 256, symmetric=True, normed=True)## 计算每张图片的灰度共生矩阵
    fa1[i] = skimage.feature.greycoprops(glcm, 'contrast')# 对比度信息
    fb1[i] = skimage.feature.greycoprops(glcm, 'energy')#能量信息
    fc1[i] = skimage.feature.greycoprops(glcm, 'homogeneity')
    fd1[i] = skimage.feature.greycoprops(glcm, 'correlation')#相关性信息
fa1 = fa1[:,np.newaxis]
fb1 = fb1[:,np.newaxis]
fc1 = fc1[:,np.newaxis]
fd1 = fd1[:,np.newaxis]
f1 = np.concatenate([fa1,fb1,fc1,fd1],axis = 1)
model = SVC(kernel = 'rbf',C = 32)
model.fit(f1,train_y)

这个代码主要就是先计算出每一张图片的灰度共生矩阵,然后利用灰度共生矩阵计算每张图片的对比度信息,能量信息,相关性信息等。将提取到地这些信息拼接作为一张图片的特征信息,然后利用分类器对其进行分类。

猜你喜欢

转载自blog.csdn.net/baidu_36161077/article/details/81058241