221 lines
8.1 KiB
Python
221 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
高光谱分析工具包使用示例
|
||
展示如何使用统一入口调用各个功能模块
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# 添加当前目录到Python路径
|
||
sys.path.insert(0, os.path.dirname(__file__))
|
||
|
||
|
||
def print_example_header(title: str, description: str):
|
||
"""打印示例头部"""
|
||
print("\n" + "="*70)
|
||
print(f"📋 示例: {title}")
|
||
print("="*70)
|
||
print(description)
|
||
print()
|
||
|
||
|
||
def print_command(cmd: str, explanation: str = ""):
|
||
"""打印命令"""
|
||
print(f"🔧 命令: {cmd}")
|
||
if explanation:
|
||
print(f"📖 说明: {explanation}")
|
||
print()
|
||
|
||
|
||
def run_example_command(cmd: str):
|
||
"""运行示例命令"""
|
||
print(f"执行命令: {cmd}")
|
||
print("-" * 50)
|
||
os.system(cmd)
|
||
print("-" * 50)
|
||
|
||
|
||
def main():
|
||
"""主函数 - 展示所有示例"""
|
||
print("🌟 高光谱分析工具包 - 使用示例")
|
||
print("本示例展示如何使用统一的 main.py 入口调用各个功能模块")
|
||
print("注意: 示例中的文件路径需要根据实际情况修改")
|
||
|
||
# 示例数据路径(占位符)
|
||
sample_hdr = "data/sample.hdr"
|
||
sample_csv = "data/sample.csv"
|
||
sample_roi = "data/roi.xml"
|
||
output_dir = "results"
|
||
|
||
# 确保输出目录存在
|
||
Path(output_dir).mkdir(exist_ok=True)
|
||
|
||
# 示例1: 降维分析
|
||
print_example_header(
|
||
"1. 降维分析 (PCA)",
|
||
"使用主成分分析将高光谱数据降维到3个组件"
|
||
)
|
||
cmd1 = f"python main.py dim-reduction --input {sample_hdr} --method pca --n-components 3 --output-dir {output_dir}"
|
||
print_command(cmd1, "执行PCA降维,输出降维后的数据和可视化结果")
|
||
|
||
# 示例2: 批量降维分析
|
||
print_example_header(
|
||
"2. 批量降维分析",
|
||
"同时运行多种降维方法进行对比分析"
|
||
)
|
||
cmd2 = f"python main.py dim-reduction --input {sample_hdr} --method pca --batch --output-dir {output_dir}/batch_dim"
|
||
print_command(cmd2, "批量执行PCA、ICA、FA等多种降维方法")
|
||
|
||
# 示例3: 图像分割
|
||
print_example_header(
|
||
"3. 图像分割 (Otsu阈值)",
|
||
"使用Otsu方法对指定波段进行阈值分割"
|
||
)
|
||
cmd3 = f"python main.py segmentation --input {sample_hdr} --method otsu --band-index 50 --output-dir {output_dir}"
|
||
print_command(cmd3, "对第50个波段应用Otsu自动阈值分割")
|
||
|
||
# 示例4: 边缘检测
|
||
print_example_header(
|
||
"4. 边缘检测 (Canny算子)",
|
||
"使用Canny算子进行边缘检测"
|
||
)
|
||
cmd4 = f"python main.py edge-detection --input {sample_hdr} --method canny --band-index 30 --output-dir {output_dir}"
|
||
print_command(cmd4, "对第30个波段应用Canny边缘检测")
|
||
|
||
# 示例5: 批量边缘检测
|
||
print_example_header(
|
||
"5. 批量边缘检测",
|
||
"同时运行多种边缘检测算法进行对比"
|
||
)
|
||
cmd5 = f"python main.py edge-detection --input {sample_hdr} --method canny --batch --output-dir {output_dir}/batch_edge"
|
||
print_command(cmd5, "批量执行Sobel、Canny等多种边缘检测方法")
|
||
|
||
# 示例6: 异常检测
|
||
print_example_header(
|
||
"6. 异常检测 (协方差矩阵)",
|
||
"使用协方差矩阵方法检测高光谱异常"
|
||
)
|
||
cmd6 = f"python main.py anomaly-detection --input {sample_hdr} --method covariance --contamination 0.1 --output-dir {output_dir}"
|
||
print_command(cmd6, "检测10%的异常样本")
|
||
|
||
# 示例7: 分类分析
|
||
print_example_header(
|
||
"7. 分类分析 (SVM)",
|
||
"使用SVM对高光谱数据进行监督分类"
|
||
)
|
||
cmd7 = f"python main.py classification --input {sample_hdr} --roi-file {sample_roi} --method svm --output-dir {output_dir}"
|
||
print_command(cmd7, "基于ROI标注数据训练SVM分类器")
|
||
|
||
# 示例8: 聚类分析
|
||
print_example_header(
|
||
"8. 聚类分析 (K-Means)",
|
||
"使用K-Means对高光谱数据进行无监督聚类"
|
||
)
|
||
cmd8 = f"python main.py clustering --input {sample_hdr} --method kmeans --n-clusters 5 --output-dir {output_dir}"
|
||
print_command(cmd8, "将数据聚类为5个类别")
|
||
|
||
# 示例9: 批量聚类分析
|
||
print_example_header(
|
||
"9. 批量聚类分析",
|
||
"同时运行多种聚类算法进行对比"
|
||
)
|
||
cmd9 = f"python main.py clustering --input {sample_hdr} --method kmeans --batch --output-dir {output_dir}/batch_cluster"
|
||
print_command(cmd9, "批量执行K-Means、GMM等多种聚类方法")
|
||
|
||
# 示例10: 特征选择
|
||
print_example_header(
|
||
"10. 特征选择 (SPA)",
|
||
"使用连续投影算法选择最优特征"
|
||
)
|
||
cmd10 = f"python main.py feature-selection --input {sample_csv} --label-column target --method spa --n-features 20 --output-dir {output_dir}"
|
||
print_command(cmd10, "从CSV数据中选择20个最优特征")
|
||
|
||
# 示例11: 光谱指数计算
|
||
print_example_header(
|
||
"11. 光谱指数计算",
|
||
"计算植被指数和其他光谱参数"
|
||
)
|
||
cmd11 = f"python main.py spectral-index --input {sample_hdr} --indices NDVI EVI --png --output-dir {output_dir}"
|
||
print_command(cmd11, "计算NDVI和EVI指数并生成可视化")
|
||
|
||
# 示例12: 批量光谱指数计算
|
||
print_example_header(
|
||
"12. 批量光谱指数计算",
|
||
"计算所有可用光谱指数"
|
||
)
|
||
cmd12 = f"python main.py spectral-index --input {sample_hdr} --batch --png --output-dir {output_dir}/batch_indices"
|
||
print_command(cmd12, "计算所有光谱指数并生成PNG可视化")
|
||
|
||
# 示例13: 数据预处理
|
||
print_example_header(
|
||
"13. 数据预处理 (标准化)",
|
||
"对光谱数据进行标准化处理"
|
||
)
|
||
cmd13 = f"python main.py preprocessing --input {sample_csv} --method standard --handle-outliers --output-dir {output_dir}"
|
||
print_command(cmd13, "执行Z-score标准化并处理异常值")
|
||
|
||
# 示例14: 形状特征分析
|
||
print_example_header(
|
||
"14. 形状特征分析",
|
||
"分析分割结果的形状特征"
|
||
)
|
||
cmd14 = f"python main.py shape-features --input segmented.dat --input-type dat --band-index 0 --min-area 50 --output-dir {output_dir}"
|
||
print_command(cmd14, "分析分割图像的形状特征(面积、周长、圆形度等)")
|
||
|
||
# 示例15: 颜色分析
|
||
print_example_header(
|
||
"15. 颜色分析 (色差计算)",
|
||
"计算颜色样本间的色差"
|
||
)
|
||
cmd15 = f"python main.py color-analysis --input lab_image.hdr --standards-file color_standards.csv --method CIEDE2000 --output-dir {output_dir}"
|
||
print_command(cmd15, "使用CIEDE2000公式计算色差")
|
||
|
||
# 示例16: 图像滤波
|
||
print_example_header(
|
||
"16. 图像滤波 (中值滤波)",
|
||
"对高光谱数据应用空间滤波"
|
||
)
|
||
cmd16 = f"python main.py filtering --input {sample_hdr} --filter-type median --kernel-size 3 --band-index 50 --output-dir {output_dir}"
|
||
print_command(cmd16, "对第50个波段应用3x3中值滤波")
|
||
|
||
# 示例17: 回归分析
|
||
print_example_header(
|
||
"17. 回归分析",
|
||
"使用多种算法进行光谱回归分析"
|
||
)
|
||
cmd17 = f"python main.py regression --input {sample_csv} --label-column chlorophyll --models ridge lasso xgboost --tune-params --output-dir {output_dir}"
|
||
print_command(cmd17, "使用岭回归、LASSO和XGBoost进行叶绿素含量预测")
|
||
|
||
print("\n" + "="*70)
|
||
print("🎯 运行示例")
|
||
print("="*70)
|
||
print("要运行上述示例,请:")
|
||
print("1. 准备相应的数据文件")
|
||
print("2. 修改文件路径为实际路径")
|
||
print("3. 复制粘贴相应的命令到终端执行")
|
||
print()
|
||
print("例如,查看帮助信息:")
|
||
print(" python main.py --help")
|
||
print()
|
||
print("查看特定任务的参数:")
|
||
print(" python main.py dim-reduction --help")
|
||
print()
|
||
print("查看所有可用任务:")
|
||
print(" python main.py list")
|
||
|
||
# 询问用户是否要运行示例
|
||
print("\n❓ 是否要运行一个简单的示例?(y/n): ", end="")
|
||
try:
|
||
response = input().strip().lower()
|
||
if response == 'y':
|
||
print("\n运行基本帮助示例...")
|
||
run_example_command("python main.py --help")
|
||
except KeyboardInterrupt:
|
||
print("\n\n退出示例展示")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|