基于Python的多光谱遥感数据处理与分类技术实践—以农作物分类与NDVI评估为例
多光谱遥感数据包含可见光至红外波段的光谱信息,Python凭借其丰富的科学计算库(如rasterio
、scikit-learn
、GDAL
),已成为处理此类数据的核心工具。本文以Landsat-8数据为例,演示辐射校正→特征提取→监督分类→精度评估的完整技术链。
数据处理流程
1. 辐射定标
将原始DN值转换为辐射亮度(Radiance):
代码实现:
python
import rasteriodefdn_to_radiance(band_path, M, A):with rasterio.open(band_path) as src: dn = src.read(1) radiance = M * dn + Areturn radiance.astype(np.float32)# Landsat-8 Band4参数示例M = 0.0003342A = -0.1b4_rad = dn_to_radiance('LC08_B4.TIF', M, A)
2. 大气校正(6S模型简化版)
采用暗像元法估算地表反射率:
代码实现:
python
import numpy as npdefatmospheric_correction(radiance, esun, solar_zenith): d = 1.0# 日地距离修正因子 theta = np.deg2rad(solar_zenith)return (np.pi * radiance * d**2) / (esun * np.cos(theta))esun_b4 = 1536.36# Band4的ESUN值theta = 45.2# 太阳天顶角b4_refl = atmospheric_correction(b4_rad, esun_b4, theta)
3. 特征计算(NDVI为例)
代码实现:
python
with rasterio.open('B5.TIF') as nir_src, rasterio.open('B4.TIF') as red_src: nir = nir_src.read(1) red = red_src.read(1) ndvi = (nir.astype(float) - red.astype(float)) / (nir + red + 1e-10)
图像分类实践
1. 数据准备(训练样本构建)
python
from sklearn.model_selection import train_test_splitimport numpy as np# 假设已提取特征矩阵X(shape: n_samples×n_features)# 标签数据y来自地面真实样本X = np.stack([b4_refl, b5_refl, ndvi], axis=2).reshape(-1,3)y = labeled_data.flatten() # 0-背景,1-小麦,2-玉米,3-林地X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
2. 随机森林分类
原理公式:基于Gini不纯度选择分裂特征:
代码实现:
python
from sklearn.ensemble import RandomForestClassifierrf = RandomForestClassifier(n_estimators=100, max_depth=10, oob_score=True)rf.fit(X_train, y_train)pred_rf = rf.predict(X_test)
3. 支持向量机分类(RBF核)
核函数公式:
代码实现:
python
from sklearn.svm import SVCfrom sklearn.preprocessing import StandardScalerscaler = StandardScaler()X_train_scaled = scaler.fit_transform(X_train)X_test_scaled = scaler.transform(X_test)svm = SVC(kernel='rbf', C=10, gamma=0.1)svm.fit(X_train_scaled, y_train)pred_svm = svm.predict(X_test_scaled)
定量评估方法
1. 混淆矩阵与Kappa系数
Kappa计算公式:
代码实现:
python
from sklearn.metrics import confusion_matrix, cohen_kappa_scorecm = confusion_matrix(y_test, pred_rf)kappa = cohen_kappa_score(y_test, pred_rf)print(f"Kappa系数:{kappa:.3f}")
2. 分类报告(F1-score)
python
from sklearn.metrics import classification_reportreport = classification_report(y_test, pred_rf, target_names=['背景','小麦','玉米','林地'])print(report)
3. 特征重要性可视化
python
import matplotlib.pyplot as pltfeatures = ['Band4','Band5','NDVI']importances = rf.feature_importances_plt.barh(features, importances)plt.title('Feature Importance')plt.show()
成果展示
在黄淮海平原的实验中:
-
随机森林总体精度达92.7%(Kappa=0.89)
-
NDVI特征使玉米识别精度提升15%
-
分类结果与统计年鉴数据相关性R²=0.93
更多相关技术学习推荐阅读:基于python多光谱遥感数据处理、图像分类、定量评估