% 主函数:处理GAF、MTF以及融合
function main()
% 设置输入和输出目录
inputDir = '.'; % 当前目录作为输入目录
gafOutputDir = 'GAF_Images';
mtfOutputDir = 'MTF_Images';
fusionOutputDir = 'Fused_Images';
% 创建输出目录
if ~exist(gafOutputDir, 'dir')
mkdir(gafOutputDir);
end
if ~exist(mtfOutputDir, 'dir')
mkdir(mtfOutputDir);
end
if ~exist(fusionOutputDir, 'dir')
mkdir(fusionOutputDir);
end
% 线路名称列表
line_names = {'L1', 'L2', 'L3', 'L4', 'L5'};
% 设置参数
num_bins = 8; % MTF分箱数量
max_points = 1000; % 最大点数(降采样)
% 启动并行池(加速处理)
if isempty(gcp('nocreate'))
parpool();
end
fprintf('已启动并行池,使用 %d 个工作进程\n', gcp().NumWorkers);
% 使用并行循环处理线路
parfor i = 1:length(line_names)
line_name = line_names{i};
filename = [line_name '.mat'];
% 检查文件是否存在
if ~exist(filename, 'file')
warning('文件不存在: %s', filename);
continue;
end
% 加载数据
data_struct = load(filename);
% 获取数据(假设变量名与文件名相同)
if ~isfield(data_struct, line_name)
warning('文件 %s 中未找到变量 %s', filename, line_name);
continue;
end
ts_obj = data_struct.(line_name);
% 处理timeseries对象
if isa(ts_obj, 'timeseries')
ts_data = ts_obj.Data;
time_vector = ts_obj.Time;
else
ts_data = ts_obj;
time_vector = [];
end
% 确保数据为一维
ts_data = ts_data(:)'; % 转换为行向量
% 降采样(减少计算量)
n = length(ts_data);
if n > max_points
factor = ceil(n / max_points);
ts_data = downsample(ts_data, factor);
if ~isempty(time_vector)
time_vector = downsample(time_vector, factor);
end
fprintf('%s: 降采样 %d -> %d 点\n', line_name, n, length(ts_data));
end
% 数据预处理:归一化到[-1,1]区间
normalized_data = normalizeData(ts_data);
% 计算GAF矩阵
gasf = computeGASF_optimized(normalized_data);
gadf = computeGADF_optimized(normalized_data);
% 计算MTF矩阵
mtf_matrix = computeMTF(normalized_data, num_bins);
% 保存GAF、MTF图像
saveGAFImage(line_name, gasf, gafOutputDir);
saveMTFImage(line_name, mtf_matrix, time_vector, mtfOutputDir);
% 融合GAF和MTF
fused_image = fuseGAFandMTF(gasf, mtf_matrix, 'weighted');
saveFusedImage(line_name, fused_image, fusionOutputDir);
fprintf('已完成: %s\n', line_name);
end
fprintf('\n所有线路处理完成!\n');
fprintf('GAF图像保存在: %s\n', fullfile(pwd, gafOutputDir));
fprintf('MTF图像保存在: %s\n', fullfile(pwd, mtfOutputDir));
fprintf('融合图像保存在: %s\n', fullfile(pwd, fusionOutputDir));
% 尝试打开输出目录
try
if ispc
winopen(gafOutputDir);
winopen(mtfOutputDir);
winopen(fusionOutputDir);
elseif ismac
system(['open "' gafOutputDir '"']);
system(['open "' mtfOutputDir '"']);
system(['open "' fusionOutputDir '"']);
else
system(['xdg-open "' gafOutputDir '"']);
system(['xdg-open "' mtfOutputDir '"']);
system(['xdg-open "' fusionOutputDir '"']);
end
catch
fprintf('请手动访问目录:\n');
fprintf('GAF图像目录: %s\n', gafOutputDir);
fprintf('MTF图像目录: %s\n', mtfOutputDir);
fprintf('融合图像目录: %s\n', fusionOutputDir);
end
end
% 数据归一化函数
function normalized = normalizeData(data)
data = double(data(:)); % 转换为双精度列向量
min_val = min(data);
max_val = max(data);
if min_val == max_val
normalized = zeros(size(data));
else
normalized = (data - min_val) / (max_val - min_val);
end
end
% 计算GASF的优化函数
function gasf = computeGASF_optimized(data)
data = double(data(:));
phi = acos(data);
gasf = cos(bsxfun(@plus, phi, phi'));
end
% 计算GADF的优化函数
function gadf = computeGADF_optimized(data)
data = double(data(:));
phi = acos(data);
gadf = sin(bsxfun(@minus, phi, phi'));
end
% 计算MTF的函数
function mtf = computeMTF(data, num_bins)
% 数据归一化
data = normalizeData(data);
% 计算分位数边界
quantiles = quantile(data, num_bins-1);
bin_edges = [-inf, quantiles, inf];
% 离散化数据
[~, ~, bin_indices] = histcounts(data, bin_edges);
% 计算马尔可夫转移矩阵
n = length(bin_indices);
transition_matrix = zeros(num_bins, num_bins);
for k = 1:(n-1)
i = bin_indices(k);
j = bin_indices(k+1);
transition_matrix(i, j) = transition_matrix(i, j) + 1;
end
% 归一化转移矩阵
row_sums = sum(transition_matrix, 2);
row_sums(row_sums == 0) = 1; % 避免除以零
transition_matrix = transition_matrix ./ row_sums;
% 构建MTF矩阵
mtf = zeros(n);
for i = 1:n
for j = 1:n
state_i = bin_indices(i);
state_j = bin_indices(j);
mtf(i, j) = transition_matrix(state_i, state_j);
end
end
end
% 保存GAF图像的函数(灰度图像)
function saveGAFImage(line_name, gasf, outputDir)
% 数据归一化到[0,1]范围
gasf = normalizeImage(gasf);
% 创建图像
fig = figure('Visible', 'off');
imagesc(gasf);
axis square;
colormap(gray); % 使用灰度色图
colorbar;
title(sprintf('%s - GAF', line_name), 'FontSize', 10);
% 保存图像
outputPath = fullfile(outputDir, sprintf('%s_GAF.png', line_name));
saveas(fig, outputPath);
close(fig);
fprintf('已保存: %s\n', outputPath);
end
% 保存MTF图像的函数(灰度图像)
function saveMTFImage(line_name, mtf_matrix, time_vector, outputDir)
% 数据归一化到[0,1]范围
mtf_matrix = normalizeImage(mtf_matrix);
% 创建图像
fig = figure('Visible', 'off');
if ~isempty(time_vector)
imagesc(time_vector, time_vector, mtf_matrix);
xlabel('Time (s)', 'FontSize', 10);
ylabel('Time (s)', 'FontSize', 10);
else
imagesc(mtf_matrix);
xlabel('Time Index', 'FontSize', 10);
ylabel('Time Index', 'FontSize', 10);
end
axis square;
colormap(gray); % 使用灰度色图
colorbar;
title(sprintf('%s - MTF', line_name), 'FontSize', 10);
% 保存图像
outputPath = fullfile(outputDir, sprintf('%s_MTF.png', line_name));
saveas(fig, outputPath);
close(fig);
fprintf('已保存: %s\n', outputPath);
end
% 融合GAF和MTF的函数
function fused = fuseGAFandMTF(gasf, mtf, method)
% 确保数据在相同范围内
gasf = normalizeImage(gasf);
mtf = normalizeImage(mtf);
% 应用选择的融合方法
switch lower(method)
case 'average'
fused = (gasf + mtf) / 2;
case 'product'
fused = gasf .* mtf;
case 'max'
fused = max(gasf, mtf);
case 'min'
fused = min(gasf, mtf);
case 'absdiff'
fused = abs(gasf - mtf);
case 'weighted'
% 加权融合 - 给GAF更高权重
fused = 0.7 * gasf + 0.3 * mtf;
otherwise
error('未知融合方法: %s', method);
end
% 归一化到[0,1]范围
fused = normalizeImage(fused);
end
% 图像归一化函数
function normalized = normalizeImage(img)
min_val = min(img(:));
max_val = max(img(:));
if max_val == min_val
normalized = zeros(size(img));
else
normalized = (img - min_val) / (max_val - min_val);
end
end
% 保存融合图像的函数(灰度图像)
function saveFusedImage(line_name, fused_image, outputDir)
% 创建灰度图像(单通道)
gray_image = mat2gray(fused_image); % 确保在[0,1]范围
% 将图像数据转换为uint8类型
gray_image = im2uint8(gray_image);
% 保存图像
outputPath = fullfile(outputDir, sprintf('%s_Fused_GAF_MTF.png', line_name));
% 确保文件路径有效
if ~ischar(outputPath) && ~isstring(outputPath)
error('文件路径必须为字符向量或字符串标量。');
end
imwrite(gray_image, outputPath, 'Quality', 100);
fprintf('已保存: %s\n', outputPath);
end
% 运行主函数
main();请分析这段代码
最新发布