遥感影像数据光谱特征,植被指数特征,纹理特征,树高特征融合分类代码
时间: 2023-06-27 21:04:44 浏览: 109
由于遥感影像数据的分类是非常复杂的,需要考虑到多种特征,因此融合多种特征进行分类是一个常用的方法。以下是一个简单的融合分类代码,包括光谱特征、植被指数特征、纹理特征和树高特征。代码使用了支持向量机(SVM)分类器。
```python
import numpy as np
from sklearn import svm
from sklearn.metrics import accuracy_score
from skimage.feature import greycomatrix, greycoprops
from skimage.transform import resize
import rasterio
# 读取遥感影像数据
with rasterio.open('data.tif') as src:
img = src.read()
img = np.transpose(img, (1, 2, 0)) # (bands, rows, cols) -> (rows, cols, bands)
# 光谱特征
spectral_features = img.reshape((-1, img.shape[2]))
# 植被指数特征
ndvi = (img[:,:,3] - img[:,:,2]) / (img[:,:,3] + img[:,:,2])
ndvi = resize(ndvi, img.shape[:2], order=0, anti_aliasing=False)
ndvi_features = ndvi.reshape((-1, 1))
# 纹理特征
glcm = greycomatrix(img[:,:,0], distances=[1], angles=[0], levels=256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')
energy = greycoprops(glcm, 'energy')
entropy = greycoprops(glcm, 'entropy')
texture_features = np.concatenate((contrast, energy, entropy), axis=1)
# 树高特征
with rasterio.open('height.tif') as src:
height = src.read(1)
height = resize(height, img.shape[:2], order=0, anti_aliasing=False)
height_features = height.reshape((-1, 1))
# 融合特征
features = np.concatenate((spectral_features, ndvi_features, texture_features, height_features), axis=1)
# 分类标签
with rasterio.open('label.tif') as src:
labels = src.read(1)
labels = labels.reshape((-1,))
# 划分训练集和测试集
train_size = int(features.shape[0] * 0.7)
train_features, train_labels = features[:train_size], labels[:train_size]
test_features, test_labels = features[train_size:], labels[train_size:]
# SVM分类器
clf = svm.SVC(kernel='linear', C=1, gamma='auto')
clf.fit(train_features, train_labels)
pred_labels = clf.predict(test_features)
accuracy = accuracy_score(test_labels, pred_labels)
print('Accuracy:', accuracy)
```
阅读全文