%%波段计算并提取 clc; clear; % 设置包含遥感影像的文件夹路径 folderPath = 'F:\ZJJ\H盘传输\PML_V2\dx2\chip\2000\SUB2'; outputFolderPath = 'F:\ZJJ\H盘传输\PML_V2\dx2\chip\SUB2'; % 获取文件夹中所有.tif文件的文件名 filePattern = fullfile(folderPath, '*.tif'); fileNames = dir(filePattern); % 遍历文件 for i = 1:length(fileNames) % 构建完整的文件路径 fullPath = fullfile(folderPath, fileNames(i).name); % 读取单个.tif文件 tifData = imread(fullPath); % 检查文件是否包含至少5个波段 if size(tifData, 3) >= 5 % 计算第2、3、4波段的和 sumBands = sum(tifData(:,:,2:4), 3); % 构建输出文件名 outputFileName = fullfile(outputFolderPath, [fileNames(i).name(1:end-4), '_bands234_sum.xlsx']); % 注意:这里我移除了原始文件名的.tif扩展名,并添加了_bands234_sum.xlsx % 将结果转出为excel表格 xlswrite(outputFileName, sumBands); fprintf('已成功计算 %s 的第2、3、4波段之和,并保存为 %s\n', fileNames(i).name, outputFileName); else warning('文件 %s 不包含至少5个波段,已跳过。\n', fileNames(i).name); end end